Visual Basic Object Arrays?

Started by
6 comments, last by Nanven 19 years, 10 months ago
Is there such a thing? I'm trying to create an array on a class I created doing this: Dim myArray(9) As Profession Then trying to insert data like: myArray(0) = New Profession(True, True, False, False) However, no data exists. What am I overlooking? Or is it even possible to create arrays on custom made classes? I don't really know a lot about Visual Basic, but i'm trying to learn it for versatility reasons, this would be so easy in C++ or C#, heh. [edited by - Nanven on June 7, 2004 8:16:17 PM]
-Going to Westwood College for my Associates in Computer Programming
Advertisement
When you are using objects you have to use the keyword ''Set'' to assign the value you want.
For example:
myArray(0) = New Profession(True, True, False, False)
should be Set myArray(0)= New Profession

In that way you are assigning the object to the array index you want to use.

The default initialization you are using I think it''s not valid.
You should use the Initialize event of the class to assign the default values you want to use.
------------------------------I prefer to be the worst of the best than the best of the worst------------------------------
That's odd, whenever I change it to

Set myArray(0) = New Profession(False, False, False, True)

and move off the line it auto corrects it back to:

myArray(0) = New Profession(False, False, False, True)

I'm using Visual Basic .NET, so the New is the new way of doing a constructor, the Initialize is VB6 I believe, if we're talking about the samething.

[edited by - Nanven on June 7, 2004 8:46:26 PM]
-Going to Westwood College for my Associates in Computer Programming
quote:Original post by Nanven
...this would be so easy in C++ or C#, heh.

It''s easy in VB.NET as well. What you posted should work. You must be doing something else somewhere. Where is the Dim statement and where are you assigning the elements of the array?

Former Microsoft XNA and Xbox MVP | Check out my blog for random ramblings on game development

In VB .Net you don''t have to use the set - what you''re doing should work fine - what might be causing you problems is either your array declaration or where you initialize it (I''m not sure what you mean by no data exists)
Dim in a class means that that member is going to be private, and I don''t know where you are initializing the items - the following code will work correctly:

Class YourClass
public myArray(9) As Profession

sub new
myArray(0) = New Profession(True, True, False, False)
(...)
end sub
End Class

Let us know if this helps.
Actually, it is public and global, I was retyping it to give a generic idea, and messed up. What I mean by no data is if I try to do an if statement and try to compare the values i set, it doesnt enter the if statement. Such as:

Public myArray(9) As Profession

myArray(0) = New Profession(True, True, False, False)

If myArray(0).attribute1 Then
MsgBox("YAY")
End If

and im not getting any message box at all.

EDIT: I just thought of something that could be messing up.
I am setting the values when the form loads. Could this be bad?

[edited by - Nanven on June 7, 2004 10:27:12 PM]
-Going to Westwood College for my Associates in Computer Programming
Going to bump this in hopes of getting an answer.
-Going to Westwood College for my Associates in Computer Programming
I''ve created a simple form and copied your code there (I had to create the profession class)
For me the message box pops up with YAY!... could your profession class have some problem in the constructor?

Public Class Form1
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

''This call is required by the Windows Form Designer.
InitializeComponent()

''Add any initialization after the InitializeComponent() call

End Sub

''Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

''Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

''NOTE: The following procedure is required by the Windows Form Designer
''It can be modified using the Windows Form Designer.
''Do not modify it using the code editor.
Private Sub InitializeComponent()
''
''Form1
''
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 266)
Me.Name = "Form1"
Me.Text = "Form1"

End Sub

#End Region

Public myArray(9) As Profession
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
myArray(0) = New Profession(True, True, False, False)

If myArray(0).attribute1 Then
MsgBox("YAY")
End If
End Sub

Class profession
Public Sub New(ByVal arg1 As Boolean, ByVal arg2 As Boolean, ByVal arg3 As Boolean, ByVal arg4 As Boolean)
attribute1 = arg1
End Sub

Public attribute1 As Boolean
End Class
End Class

This topic is closed to new replies.

Advertisement