• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 282
  • Last Modified:

Passing User Defined Type Arrays to and from functions

Hi,
I'm doing this in VBA (MS access). Been going around in circles this
afternoon, trying to suss out quite how arrays
and UDTs work. Could any kind souls out there point me in the right
direction, as this is driving me mad!


The first problem is how does one pass an array back to the function that
called it? For example:

Function TestArray() As Variant
    Dim varArray As Variant
    Dim intA(3) As Integer

    'using variant for int
    intA(0) = 1
    intA(1) = 2
    intA(2) = 3

    varArray = intA
    varArray = ShowArray3(varArray)

    'intA = varArray        'It doesn't like this when I compile! Says Can't
assign to Array

    'Contents not changed - the contents of varArray have however.
    MsgBox intA(0) & ", " & intA(1) & ", " & intA(2)
End Function


Function ShowArray3(varA As Variant) As Variant
    MsgBox varA(0) & " : " & varA(1) & " : " & varA(2)

    varA(0) = 4
    varA(1) = 5
    varA(2) = 6
    ShowArray3 = varA
End Function

The second (related) issue is that I'm trying to pass an array of UDTs to a
function, doing stuff to them and then passing them
back. An example:


Public Type Alph
    Name As String
    Value As Integer
End Type


Function TestArray() As Variant
    Dim udtArray() As Alph
    Dim varArray As Variant

    ReDim Preserve udtArray(1)
    udtArray(0).Name = "one"
    udtArray(0).Value = 1
    ReDim Preserve udtArray(2)
    udtArray(1).Name = "two"
    udtArray(1).Value = 2

    'varArray = udtArray            'bit that its unhappy about :(
    'udtArray = ShowArray(udtArray)

    MsgBox udtArray(0).Name & " : " & udtArray(0).Value & " : " &
udtArray(1).Name & " : " & udtArray(1).Value
End Function

'Can pass udt ok but having problems with arrays of udts
Function ShowArray(udtA As Alph) As Alph
    'MsgBox udtA(0).Name & " : " & udtA(1).Name
    'udtA(0).Name = "three"
    'udtA(0).Value = 3
    'udtA(1).Name = "four"
    'udtA(1).Value = 4
    'ShowArray = udtA
End Function

Many many thanks to anyone who can pull the wool off my eyes, Thanks

Tom Roberts, puzzled of Waterloo

p.s. just been nosing around classes, is that the way one should do it
really?



0
TomRoberts
Asked:
TomRoberts
1 Solution
 
MicrosoftCommented:
try defining INTA as a var first

you are trying to assign a function to a var without defining the var first.

dim inta as string

or something

this should sort out the compile error
0
 
bobbit31Commented:
This is for the second part:

instead of passing the array back and forth why not just use the reference as shown below (vb default passes parameters ByRef)

Function TestArray() As Variant
   Dim udtArray() As Alph
   Dim varArray() As Alph

   ReDim Preserve udtArray(1)
   udtArray(0).Name = "one"
   udtArray(0).Value = 1
   ReDim Preserve udtArray(2)
   udtArray(1).Name = "two"
   udtArray(1).Value = 2

   varArray = udtArray            'bit that its unhappy about :(
   ShowArray udtArray

   MsgBox udtArray(0).Name & " : " & udtArray(0).Value & " : " & udtArray(1).Name & " : " & udtArray(1).Value

End Function

'Can pass udt ok but having problems with arrays of udts
Private Function ShowArray(udtA() As Alph)
   MsgBox udtA(0).Name & " : " & udtA(1).Name
   udtA(0).Name = "three"
   udtA(0).Value = 3
   udtA(1).Name = "four"
   udtA(1).Value = 4
End Function

Private Sub Command1_Click()

    TestArray

End Sub
0
 
sharmonCommented:
If you are just using the UDT's in a standard project you do not need to make classes out of them.  If you need your UDT's to follow certain business rules, putting them in classes will allow you to deal with that.  However when making a dll that passes these UDT's between the client application and itself you must make them classes.  I have updated your code to work, although I am not sure what you are trying to do, it looks more like you are just testing how things work in the routines.  This may give you some ideas.  Although there are multiple ways one could go about doing what you did to make it work.


Option Explicit

Public Type Alph
   Name As String
   Value As Integer
End Type

Function TestArray() As Variant
   Dim intA(3) As Integer

   intA(0) = 1
   intA(1) = 2
   intA(2) = 3

   ShowArray intA

   MsgBox intA(0) & ", " & intA(1) & ", " & intA(2)
   
   TestArray = intA
End Function


Sub ShowArray(varA() As Integer)
   MsgBox varA(0) & " : " & varA(1) & " : " & varA(2)
   
   varA(0) = 4
   varA(1) = 5
   varA(2) = 6
End Sub

Function TestArray2() As Alph()
   Dim udtArray() As Alph

   ReDim Preserve udtArray(1)
   udtArray(0).Name = "one"
   udtArray(0).Value = 1
   
   ReDim Preserve udtArray(2)
   udtArray(1).Name = "two"
   udtArray(1).Value = 2

   ShowArray2 udtArray

   MsgBox udtArray(0).Name & " : " & udtArray(0).Value & " : " & _
   udtArray(1).Name & " : " & udtArray(1).Value
   
   TestArray2 = udtArray()
End Function

Sub ShowArray2(udtA() As Alph)
   MsgBox udtA(0).Name & " : " & udtA(1).Name
   udtA(0).Name = "three"
   udtA(0).Value = 3
   udtA(1).Name = "four"
   udtA(1).Value = 4
End Sub
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
TomRobertsAuthor Commented:
Thanks.

What I was trying to do (cos I read somewhere that it is more efficient to pass arrays as variants (is this true?)
was use the variant to pass the array to the function.

I can see that varA = intA would work (as varA is a variant) but intA = varA doesn't.

So:
Dim intA(3) as variant would compile.

Anyhow, first bit solved now - I forgot about byRef! (oh dear).

ShowArray3 intA

to ShowArray3, where that is a sub does the trick.
Now to see if that works with udts...
0
 
bobbit31Commented:
as for the first part, i don't understand why you are doing intA = varArray...

why not just use varArray?

and again, pass varArray ByRef to ShowArray3

ie:

Sub TestArray()
   Dim varArray(3) as Integer

   ShowArray3 varArray

   msgbox varArray(0) " : " & varArray(1) & " : " & varArray(2)

End Sub

Sub ShowArray3(varA() as Integer)

   varA(0) = 1
   varA(1) = 2
   varA(2) = 3

End Sub

0
 
TomRobertsAuthor Commented:
Thanks all of you for your help. Thats made my day.

Brief background is that I'm writing an error handler at the moment. What I'd like to do is pass additional criteria to it (such as userID, the call stack etc) without having 7 or so parameters.

I was playing around with something simple first, to see if I understood clearly what was going on.

Cheers
Tom
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now