Results 1 to 7 of 7

Thread: Arrays

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jul 1999
    Posts
    207

    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

  2. #2
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    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:
    1. 'either
    2. Dim A() As UserInfo
    3. A(0)=New User()
    4. A(0).UserName = "ME"
    5. A(1)=New User()
    6. A(1).UserName = "You"
    7.  
    8. 'or
    9. Dim A() As New UserInfo={New User(), New User(), New User()}
    10. A(0).UserName = "ME"
    11. A(1).UserName = "You"

    Or if you want to add a constructor to the class:
    VB Code:
    1. Public Class UserInfo
    2.     Public UserName As String
    3.     Public Password As String
    4.     Public Premissions As String
    5.     Public FirstName As String
    6.     Public LastName As String
    7.     Public Comment As String
    8.  
    9.  Public Sub New()
    10.     mybase.New()
    11.  End Sub
    12.  
    13.  Public Sub New(username as string)
    14.     Me.UserName=username
    15.  End Sub
    16.  
    17. End Class
    18.  
    19. 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.

  3. #3
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    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

  4. #4
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    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.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Jul 1999
    Posts
    207
    See my problem is that when I type: Dim A() As New UserInfo
    it says: arrays cannot be declared with 'New'

    any ideas?

    Jeremy

  6. #6
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    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())

  7. #7
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    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
  •  



Click Here to Expand Forum to Full Width