dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: Arrays inside class

  1. #1
    Join Date
    Nov 2003
    Posts
    12

    Arrays inside class

    I need to build a VB class module having a dynamic array as a property. As it is not possible, I use a variant inside the class and then I assign this variant to an array at run time, as in the following code snippet:

    Code:
    Private Sub Command1_Click()
        Dim matrix() As String
        Dim i As Integer, j As Integer
    
        ReDim matrix(1 To 10, 1 To 10)
        For i = 1 To 10
            For j = 1 To 10
                matrix(i, j) = (i - 1) * 10 + j
            Next j
        Next i
        
        Dim data As New C_data
        data.table = matrix()
    End Sub
    where C_data is a simple class defined as follows:

    Code:
    Public table As Variant
    if now I try to modify the content of data.table inside Command1_click procedure, with a command like this:

    Code:
        data.table(1, 1) = "pino"
    it does not work, I mean that I have no errors but data.table(1,1) still contains string "1".

    Someone have any suggestions?

    Thanks!

  2. #2
    Join Date
    Oct 2005
    Location
    England
    Posts
    803

    Re: Arrays inside class

    You can have it as a sort of property you just have to make your own accessor function. Place your array into the class then have a get function and a set function which takes the apropriate parameters. It works fine like this as im using it in my current project.

    Rich

  3. #3
    Join Date
    Nov 2003
    Posts
    12

    Re: Arrays inside class

    thank you but.. do you now why this code is not working without having any error? where is the problem?
    Paolo

  4. #4
    Join Date
    Dec 2003
    Location
    Northern Ireland
    Posts
    1,362

    Re: Arrays inside class

    I guess the array inside your class is meant to be dynamic?

    If so, maybe you should be creating it like this:
    Code:
    Public table() As Variant
    and use ReDim Preserve to make the array bigger to add items.
    Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rich Cook


    0100 1101 0110 1001 0110 0011 0110 1000 0110 0001 0110 0101 0110 1100 0010 0000 0100 0101 0110 1100 0110 1100 0110 0101 0111 0010

  5. #5
    Join Date
    Nov 2003
    Posts
    12

    Re: Arrays inside class

    it should be good, but unfortunately it is not possible to use an array as a public member of an object module..

  6. #6
    Join Date
    Dec 2001
    Posts
    6,332

    Re: Arrays inside class

    You can certainly have an array as a return from a property or function.

    Here's one simple way:
    Code:
    Public Property Get MyData() As String()
    MyData = Split("A,B,C,D", ",")
    End Property
    However, for more complex array structures, you'd have to do something different. What sort of data do you need it to return?
    Please remember to rate the posts and threads that you find useful.
    How can something be both new and improved at the same time?

  7. #7
    Join Date
    Nov 2003
    Posts
    12

    Re: Arrays inside class

    I have to work with large amount of data, so very big arrays.. I've to be able to modify a single element, but also to set and get the overall arrays, to speed up the computational time..

  8. #8
    Join Date
    Dec 2001
    Posts
    6,332

    Re: Arrays inside class

    Quote Originally Posted by faggio
    I have to work with large amount of data, so very big arrays.. I've to be able to modify a single element, but also to set and get the overall arrays, to speed up the computational time.
    You can do all that easily. Here's how:
    Code:
    'set the entire array:
    Public Property Let MyArray(Data() As String)
    TheArray = Data
    End Property
    
    'get a single element:
    Public Property Get MyElement(ByVal Index As Long) As String
    If Index < 0 Or Index > UBound(TheArray) Then Exit Property
    MyElement = TheArray(Index)
    End Property
    
    'set a single element:
    Public Property Let MyElement(ByVal Index As Long, ByVal Data As String)
    If Index < 0 Or Index > UBound(TheArray) Then Exit Property
    TheArray(Index) = Data
    End Property
    Then you can use it from a form like this:
    Code:
    Private Sub Command1_Click()
    MyClass.MyElement(2) = Text1
    End Sub
    
    Private Sub Command2_Click()
    Text1 = MyClass.MyElement(2)
    End Sub
    Please remember to rate the posts and threads that you find useful.
    How can something be both new and improved at the same time?

  9. #9
    Join Date
    Nov 2003
    Posts
    12

    Re: Arrays inside class

    Thank you, this approach works fine, I've also added a function to Get the entire array.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)