Results 1 to 8 of 8

Thread: [RESOLVED] Redim array of objects

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 2002
    Posts
    146

    Resolved [RESOLVED] Redim array of objects

    I'm trying to create a dynamic array of objects


    Code:
    Private var() As New Entity
    
    
    
    Private Sub Form_Load()
    ReDim var(1 To 5) As Entity
    End Sub
    Compile error:


    Can't change data types of array elements
    Last edited by winterslam; Nov 12th, 2008 at 03:03 AM.

  2. #2
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,123

    Re: Redim array of objects

    Remove the bold line...
    Code:
    Private var() As New Entity
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  3. #3
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: Redim array of objects

    You're supposed to declare an array of references to objects (which can be Nothing) so don't instantiate with new, Dim var() As Entity. Set each array element with reference to class instance accordingly later on, Set var(0) = New Entity. When resizing with Redim just change upper and lower bounds, don't specify type which has already been declared (hence your error message).

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Sep 2002
    Posts
    146

    Re: Redim array of objects

    Code:
    Private var() As Entity
    
    
    
    Private Sub Form_Load()
    ReDim var(1 To 5) As Entity
    var(1).x = 5
    
    End Sub
    Run-time error '91':


    Object varialbe or With block variable not set

  5. #5
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,123

    Re: Redim array of objects

    The following also works...
    Code:
    Private var() As New Entity
    
    Private Sub Form_Load()
        ReDim var(1 To 5) As New Entity
    End Sub
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Sep 2002
    Posts
    146

    Re: Redim array of objects

    Quote Originally Posted by dee-u
    The following also works...
    Code:
    Private var() As New Entity
    
    Private Sub Form_Load()
        ReDim var(1 To 5) As New Entity
    End Sub
    thanks
    it works

  7. #7
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,123

    Re: Redim array of objects

    Quote Originally Posted by winterslam
    Code:
    Private var() As Entity
    
    
    
    Private Sub Form_Load()
    ReDim var(1 To 5) As Entity
    var(1).x = 5
    
    End Sub
    Run-time error '91':


    Object varialbe or With block variable not set
    You have to instantiate them first before you could use them...
    Code:
    Private Sub Form_Load()
        Dim a As Long
        ReDim Var(1 To 5) As Entity
        
        For a = LBound(Var) To UBound(Var)
            Set Var(a) = New Entity
        Next
        Var(1).x = 5
    
    End Sub
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  8. #8
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: [RESOLVED] Redim array of objects

    Your misconception is that in creating an array declared as Entity you have an array of objects/instances. What you have initially are just place holders. You only have an array of references (memory locations) that are initially set to Nothing (is zero or doesn't reference a memory location which implies that instance of class has not yet been created).

    I don't see the point in declaring dynamic array As New since array elements don't exist.
    Last edited by leinad31; Nov 12th, 2008 at 03:16 AM.

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