Results 1 to 6 of 6

Thread: Redim'ing an Array of Objects

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Feb 2001
    Location
    Adelaide, Australia
    Posts
    24

    Angry

    I have a big problem Redimming an array of objects.

    I have declared a class called clsSite detailing all the info pertaining to a site.

    I have declared a class called clsSiteList which as Logic should show, represents a list of sites. I am doing this with an array of clsSite objects.
    eg Private mvararrSite() As clsSite 'local copy

    It allows this declaration but,
    1) When I go to redim with a
    Preserve Redim mvararrSite(Ubound(mvararrSite))
    I have trouble

    2) When I go to redim with
    Redim mvararrSite(1)
    I have trouble

    So I started thinking that it could be the problem that I am adding new classes rather than objects, thus I made the following change to the delclaration.
    eg Private mvararrSite() As New clsSite 'local copy

    Again it allows the declaration but dies with a redim.


    Help please, this is really getting to me.

  2. #2
    Frenzied Member
    Join Date
    Jan 2000
    Location
    Bellevue, WA, USA
    Posts
    1,357
    Some ideas:

    1. Can you be more specific about your "trouble"? What errors do you encounter?

    2. It's probably a typo, but "Preserve Redim" should be "Redim Preserve"

    3. Try declaring your array as type "Object". I don't know if this will help or not (probably not), but you could try it.

    4. Have you thought of using a collection instead of an array to hold your classes? Collections of classes are generally much easier to work with, and you dont have to redim anything. Just use the built in .Add and .Remove properties.
    ~seaweed

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Feb 2001
    Location
    Adelaide, Australia
    Posts
    24

    Angry Reply to Redim'ing Array of Class Objects

    I have tried declaring object in the array dec but have problems latter setting the object to the specific class I want

    Sorry, my mistake here, I actually have Redim Preserve in my code

    Trouble=
    Err Num: -2147211505
    Err Comment: "An unkown error has occured!"



    Top part of my function is as follows,

    Public Function AddSite(ByVal SiteName As String, ByVal SiteCode As String, ByVal BR As Integer, ByVal Zone As Integer) As Boolean

    On Error GoTo AddSiteErr
    MsgBox "starting addsite "
    DoEvents
    MsgBox "still starting addsite"



    If intSiteCount = 0 Then
    * ReDim mvararrSite(1) ' As New clsSite
    Else
    ReDim Preserve mvararrSite(UBound(mvararrSite) + 1)
    End If



    MsgBox "Just redimmed "
    mvararrSite(UBound(mvararrSite)).strSiteName = SiteName
    mvararrSite(UBound(mvararrSite)).strSiteCode = SiteCode
    MsgBox "About to increment mvarintsite"
    DoEvents
    etc etc

    It dies at *
    The msgbox's and doevents were there to try and see where the hickup is, they don't work! Even putting breakpoints at these msgbox s the error is raised at a line of code below them. Is this because VB is interpreted at "Run" rather than Build.

    Also as said before and can be seen in this code (as it has been gradually butchered) I have tried using "As New" and putting the clase name there thinking it may be an object problem. Doesn't work

  4. #4
    Frenzied Member
    Join Date
    Jan 2000
    Location
    Bellevue, WA, USA
    Posts
    1,357
    You didn't show how you declared your array. I assume it was in the GeneralDeclarations section or in a Module like this:
    Code:
    Public MyArray() As clsSite
    When you redim the array, you don't specify the datatype (that was done when the array was declared). You just do this:
    Code:
    Redim MyArray(1)
    When you assign members to the array, you must first create an instance of the class. Then when you add it to your array, you use the Set command like this:
    Code:
    Dim MyClassInstance As New Class1
    Set MyArray(1) = MyClassInstance
    Now you can access the class members of the items in the array like this:
    Code:
    MyArray(UBound(MyArray)).strSiteName = SiteName 
    MyArray(UBound(MyArray)).strSiteCode = SiteCode
    Try these things and see if that helps any.
    ~seaweed

  5. #5
    Frenzied Member
    Join Date
    Jan 2000
    Location
    Bellevue, WA, USA
    Posts
    1,357
    Something like this may be your solution:
    Code:
    ' In the General Declarations section:
    Private m_varSiteArray() As clsSite
    
    Public Function AddSite(ByVal SiteName As String, ByVal SiteCode As String, _
                            ByVal BR As Integer, ByVal Zone As Integer) As Boolean
        
        ' Create a temporary variable to hold the class info
        Dim MyTempClass As New clsSite
        
        ' Populate class properties
        MyTempClass.strSiteName = SiteName
        MyTempClass.strSiteCode = SiteCode
        
        ' Redim the array to hold another class item
        If m_intSiteCount = 0 Then
            ReDim m_varSiteArray(1)
        Else
            ReDim Preserve m_varSiteArray(UBound(m_varSiteArray) + 1)
        End If
        
        ' Update the sitecount variable
        m_intSiteCount = m_intSiteCount + 1
    
        ' Add the temporary item to the array
        Set m_varSiteArray(UBound(m_varSiteArray)) = MyTempClass
        
        ' Clean up memory (we don't need the temporary item anymore)
        Set MyTempClass = Nothing
        
    End Function
    ~seaweed

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Feb 2001
    Location
    Adelaide, Australia
    Posts
    24

    Thumbs up It worked

    Thanks a million. It worked a charm.

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