|
-
Oct 13th, 2002, 12:10 AM
#1
Thread Starter
Addicted Member
Arrays
Hi,
I have a class like this
Code:
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
-
Oct 13th, 2002, 01:44 AM
#2
You have to call new on the individual elements of the array before you use them or assign them all in the delcaration:
VB Code:
'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:
VB Code:
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")}
Last edited by Edneeis; Oct 13th, 2002 at 07:02 PM.
-
Oct 13th, 2002, 03:22 AM
#3
PowerPoster
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
-
Oct 13th, 2002, 05:17 AM
#4
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.
-
Oct 13th, 2002, 04:32 PM
#5
Thread Starter
Addicted Member
See my problem is that when I type: Dim A() As New UserInfo
it says: arrays cannot be declared with 'New'
any ideas?
Jeremy
-
Oct 13th, 2002, 06:00 PM
#6
PowerPoster
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:
Code:
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())
-
Oct 13th, 2002, 07:01 PM
#7
Sorry I cut and pasted and forgot to remove the NEW keyword in the first example. I'll correct it.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|