Click to See Complete Forum and Search --> : Arrays
Jeremy Martin
Oct 13th, 2002, 12:10 AM
Hi,
I have a class like this
Public Class UserInfo
Public UserName As String
Public Password As String
Public Premissions As String
Public FirstName As String
Public LastName As String
Public Comment As String
End Class
What I would like to do is have an array made of this class on my form.
example.
Dim A() As New UserInfo
A(0).UserName = "ME"
A(1).UserName = "You"
...
I find my problem being that I can not use the keyword New with an array. And if I don't use the keyword New, it doesn't work with this error message: Object reference not set to an instance of an object.
Any help would be appreciated.
Jeremy
Edneeis
Oct 13th, 2002, 01:44 AM
You have to call new on the individual elements of the array before you use them or assign them all in the delcaration:
'either
Dim A() As UserInfo
A(0)=New User()
A(0).UserName = "ME"
A(1)=New User()
A(1).UserName = "You"
'or
Dim A() As New UserInfo={New User(), New User(), New User()}
A(0).UserName = "ME"
A(1).UserName = "You"
Or if you want to add a constructor to the class:
Public Class UserInfo
Public UserName As String
Public Password As String
Public Premissions As String
Public FirstName As String
Public LastName As String
Public Comment As String
Public Sub New()
mybase.New()
End Sub
Public Sub New(username as string)
Me.UserName=username
End Sub
End Class
Dim A() As New UserInfo={New User("Me"), New User("You"), New User("Them")}
Lethal
Oct 13th, 2002, 03:22 AM
Edneeis, You seem to have picked up .NET very well. I need to start cracking on learning .NET more. What books did u read?
Thanks
Edneeis
Oct 13th, 2002, 05:17 AM
Really the only book I have used is 'Programming Visual Basic.NET' by Francesco Balena it is the Core Reference book and covers quite a lot of info.
I also have 'Advanced Remoting in VB.NET' but that only covers the Remoting topic. Also all the examples carry over from one another and I'm a skip around guy so that kind of bugs me. I prefer more reference style books like the first one mentioned.
Also since everything is object based it makes things easier to learn you just go to the namespace you need and poke around a bit.
Jeremy Martin
Oct 13th, 2002, 04:32 PM
See my problem is that when I type: Dim A() As New UserInfo
it says: arrays cannot be declared with 'New'
any ideas?
Jeremy
hellswraith
Oct 13th, 2002, 06:00 PM
You are trying to declare and initialize an unknown value. You just can't do that. When you use the new keyword, you are actually creating it in memory. When you do this:
Dim A() As New UserInfo
The compiler has no idea how many instances of UserInfo to create in memory.
Here is what I would do:
Dim i As Integer
'First declare your array.
Dim A(5) As UserInfo
'Now you need to initialize all the objects of the array.
'This will actually create them in memory.
For i = 0 To 4
A(i) = New UserInfo()
Next
A(0).UserName = "ME"
A(1).UserName = "You"
MessageBox.Show(A(1).UserName.ToString())
Edneeis
Oct 13th, 2002, 07:01 PM
Sorry I cut and pasted and forgot to remove the NEW keyword in the first example. I'll correct it.
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.