[RESOLVED] Using dynamic, user-defined type arrays as a function return type
This is a pretty frustrating problem with VB6, and I'm hoping someone knows the solution...
I have a user-defined type declared in a module file:
Code:
Public Type T_PRODUCT_STEP
StepId As Long
End Type
And in the same module, a function:
Code:
Public Function ProductStep(MyVar As String) as T_PRODUCT_STEP()
Dim StepListRet() As T_PRODUCT_STEP
ReDim StepListRet(3)
StepListRet(0).StepId = 1
StepListRet(1).StepId = 2
StepListRet(2).StepId = 3
StepListRet(3).StepId = 4
ProductStepList = StepListRet
End Function
Now, no matter what I do I can't get the function to return the array or read it on the other side. The above code returns an error on "ProductStepList = StepListRet" (can't assign to array). If I try:
Code:
Dim StepListRet() As T_PRODUCT_STEP
ReDim ProductStepList(3)
ProductStepList(0).StepId = 1
ProductStepList(1).StepId = 2
ProductStepList(2).StepId = 3
ProductStepList(3).StepId = 4
I get an error on the first StepId assignment (argument not optional, looks like it's trying to call the function instead of assigning to an array).
If I comment everything except the ReDim ProductStepList(3) the program executes, and I can read the array of empty type elements, ie.:
Code:
Dim test() as T_PRODUCT_STEP
test = ProductStepList("dfd")
Msgbox test(2).StepId
I just can't populate the newly created array with values. Anyone have any ideas?
Easiest solution would be to use Variants everywhere, but shouldn't have to...
Thanks,
Brian
Re: Using dynamic, user-defined type arrays as a function return type
Seems to me you're calling the wrong function. Shouldn't:
VB Code:
Dim test() as T_PRODUCT_STEP
test = [b]ProductStepList[/b]("dfd")
Msgbox test(2).StepId
...be...
VB Code:
Dim test() as T_PRODUCT_STEP
test = [b]ProductStep[/b]("dfd")
Msgbox test(2).StepId
?
chem
Re: Using dynamic, user-defined type arrays as a function return type
Yes, it should be ProjectStepList in the function definition... I typed it out since I'm flipping between Linux web browser and VMWare. Hopefully there's no other obvious typos...
Thanks,
Brian
Re: Using dynamic, user-defined type arrays as a function return type
Well, the most obvious typo I saw, was that "ProductStep" returns as "ProductStepList". When I fixed that, and used this code, it worked fine for me..
VB Code:
Private Type T_PRODUCT_STEP
StepId As Long
End Type
Private Function ProductStepList(MyVar As String) As T_PRODUCT_STEP()
Dim StepListRet() As T_PRODUCT_STEP
ReDim StepListRet(3)
StepListRet(0).StepId = 1
StepListRet(1).StepId = 2
StepListRet(2).StepId = 3
StepListRet(3).StepId = 4
ProductStepList = StepListRet
End Function
Private Sub Command1_Click()
Dim test() As T_PRODUCT_STEP
test = ProductStepList("dfd")
MsgBox test(2).StepId
End Sub
chem
Re: Using dynamic, user-defined type arrays as a function return type
That is strange. Previously I was receiving the "Cannot assign to array" error on the last line of ProductStepList(). It is now working with the code you supplied, but with Public type/function in a module instead of Private. No idea why it didn't before.
Thanks again for the quick reply... glad it does work and I'm not going completely crazy.
Brian
Re: Using dynamic, user-defined type arrays as a function return type
No problem. Remember to mark your thread as resolved when you get your answer :p
You can either use the forum tools menu above or edit your original post :)
chem