Solved

Passing User Defined Type Arrays to and from functions

Posted on 2001-06-07
6
258 Views
Last Modified: 2010-08-05
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
Comment
Question by:TomRoberts
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 2

Expert Comment

by:Microsoft
ID: 6164369
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
 
LVL 18

Accepted Solution

by:
bobbit31 earned 200 total points
ID: 6164417
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
 
LVL 6

Expert Comment

by:sharmon
ID: 6164442
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:TomRoberts
ID: 6164453
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
 
LVL 18

Expert Comment

by:bobbit31
ID: 6164469
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
 

Author Comment

by:TomRoberts
ID: 6164486
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question