0

I have a Class (TestClass) with the following three properties:

Private myArr1() As String
Private myArr2() As String
Private myFinalArray() As String

Private Sub Class_Initialize()
    ReDim myArr1(0 To 3)
    ReDim myArr2(0 To 2)
    ReDim myFinalArray(0 To 6)
End Sub
Public Property Get arr1(ByVal index As Long) As Double
    arr1 = myArr1(index)
End Property
Public Property Let arr1(ByVal index As Long, ByVal myvalue As Double)
    myArr1(index) = myvalue
End Property

Public Property Get arr2(ByVal index As Long) As Double
   ...
Public Property Let arr2(ByVal index As Long, ByVal myvalue As Double)
    ...

Public Property Get FinalArray(ByVal index As Long) As Double
    ...
Public Property Let FinalArray(ByVal index As Long, ByVal myvalue As Double)
    ...

Here we have just two arrays that I fill with data:

Sub test()
Dim t As TestClass
Set t = New TestClass
For i = 0 To 3
    t.arr1(i) = i
Next
For i = 0 To 2
    t.arr2(i) = i
Next
t.GetFinalValues (t)
End Sub

My Problem now is that these array elements must be rearranged according to a confused pattern I want to write a property for that but it is not working. My idea was to add the following function to my class:

Public Function GetFinalValues(ByRef t As TestClass) As Double()
'Imput parameter arrX can ben the Value as well as the Bench arrays.
Dim arr1(2) As Double
Dim arr2(3) As Double
Dim i As Integer
Dim arrCollection(6) As Double

    arrCollection(0) = t.arr1(0)
    arrCollection(1) = t.arr2(0)
    arrCollection(2) = t.arr2(1)
    arrCollection(3) = t.arr1(1)
    arrCollection(4) = t.arr2(2)
    arrCollection(6) = t.arr1(2)
    arrCollection(5) = t.arr2(3)

'Assign return object
For i = 0 To 6
    FinalArray(i) = arrCollection(i)
Next i
GetFinalValues
End Function

If I run this the code stops at t.GetFinalValues(t) giving me the Errormessage: Object doesent support property or method. Anyone who can help me get this working? Or has a rebuild idea for a even better solution to that problem?

EDIT: I added vb.net since this might be a construction problem that is not specified to vba

3
  • It's pretty simple - your class doesn't have a GetFinalValues property or method, hence the error. Commented Oct 21, 2014 at 9:06
  • I thought that method in that ErrMsg also covers function. The GetFinalValues Function obviously exist in my Class. If not the way I do then how to invoke the function? Commented Oct 21, 2014 at 9:26
  • Sorry - ignore that comment, having a senior moment. See the answer I just posted. Commented Oct 21, 2014 at 10:08

1 Answer 1

1

You have two issues:

First you should remove the parentheses from this line:

t.GetFinalValues (t)

so it's just:

t.GetFinalValues t

Then your function needs to return a String array not Double, since that's the type of the private variable. Your class code becomes something like this:

Private Sub Class_Initialize()
    ReDim myArr1(0 To 3)
    ReDim myArr2(0 To 2)
    ReDim myFinalArray(0 To 6)
End Sub
Public Property Get arr1(ByVal index As Long) As Double
    arr1 = myArr1(index)
End Property
Public Property Let arr1(ByVal index As Long, ByVal myvalue As Double)
    myArr1(index) = myvalue
End Property

Public Property Get arr2(ByVal index As Long) As Double
    arr2 = myArr1(index)
End Property
Public Property Let arr2(ByVal index As Long, ByVal myvalue As Double)
    myArr2(index) = myvalue
End Property
Public Property Get FinalArray(ByVal index As Long) As Double
    FinalArray = myArr1(index)
End Property
Public Property Let FinalArray(ByVal index As Long, ByVal myvalue As Double)
    myFinalArray(index) = myvalue
End Property
Public Function GetFinalValues(ByRef t As TestClass) As String()
'Imput parameter arrX can ben the Value as well as the Bench arrays.
Dim arr1(2) As Double
Dim arr2(3) As Double
Dim i As Integer
Dim arrCollection(6) As Double

    arrCollection(0) = t.arr1(0)
    arrCollection(1) = t.arr2(0)
    arrCollection(2) = t.arr2(1)
    arrCollection(3) = t.arr1(1)
    arrCollection(4) = t.arr2(2)
    arrCollection(6) = t.arr1(2)
    arrCollection(5) = t.arr1(3)

'Assign return object
For i = 0 To 6
    FinalArray(i) = arrCollection(i)
Next i
GetFinalValues = myFinalArray
End Function

Note that you were also trying to use t.arr2(3) which exceeds the number of elements in arr2 so I assumed you meant t.arr1(3)

Sign up to request clarification or add additional context in comments.

1 Comment

I could get my problem solved with your solution. Thanks a lot!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.