Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I want to pass an array of user defined types but can't

Status
Not open for further replies.

christheprogrammer

Programmer
Jul 10, 2000
258
0
0
CA
Hi all,
I want to return an array of my Line_Segments from a function with the types as follows:

Private Type Coord
e As Double 'Easting
n As Double 'Northing
End Type

Private Type Line_Segment
start As Coord
end As Coord
mid As Coord
length As Double
End Type

Function get_line_segments() as Variant
'Build array here
End Function

Problem: Apparently I cannot return a Variant which is an array of user defined types!!! So how do I do this?
I appreciate the help
Thanks
Chris


Later
 
Your best bet may be to pass the array by reference to the function. I do this with the POINTAPI coodinate type. In a module I have the TypeDef and the Sub.
In the declarations section of the module, define the type.
Code:
Public Type POINTAPI
    tLng_Xloc  As Long
    tLng_YLoc  As Long
End Type
and in the same module is the function. Notice that the function is accepting as a parameter the array of the specific type. Although not in this example, I would recomment that you provide range checks using the LBound and UBound functions.
Code:
Public Sub GetAllPoints(TheArray() As POINTAPI)
   
   TheArray(0).tLng_Xloc = 1
   TheArray(0).tLng_YLoc = 2
   TheArray(1).tLng_Xloc = 3
   TheArray(1).tLng_YLoc = 4
   TheArray(2).tLng_Xloc = 5
   TheArray(2).tLng_YLoc = 6
   TheArray(3).tLng_Xloc = 7
   TheArray(3).tLng_YLoc = 8
   TheArray(4).tLng_Xloc = 9
   TheArray(4).tLng_YLoc = 10
   TheArray(5).tLng_Xloc = 11
   TheArray(5).tLng_YLoc = 12

End Sub
Inside of a form, specifically a command button, I have the following code:
[/code]
Private Sub cmdMaintenance_Click()

Dim MyArray(5) As POINTAPI

GetAllPoints MyArray()

End Sub
[/code]
Upon return, the MyArray is populated with the values assigned inside the GetAllPoints sub.


Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
Back to the original question, "How do I do this?"

You're not trying to return a variant, but an array:

Code:
Private Type Coord
    e As Double  'Easting
    n As Double  'Northing
End Type

Private Type Line_Segment
  start As Coord
  end As Coord
  mid As Coord
  length As Double
End Type

Function get_line_segments(ByVal intTotal As Integer) As Line_Segment()

  Dim mySegments() As Line_Segment
  Dim m As Coord
  Dim e As Coord
  Dim s As Coord
  Dim i As Integer
  
  If intTotal > 0 Then
    ReDim mySegments(intTotal)
  Else
    Exit Function
  End If
  
  m.e = 10
  m.n = 20
  
  e.e = 30
  e.n = 50
  
  s.e = 50
  s.n = 70
  
  For i = 0 To UBound(mySegments)
    With mySegments(i)
      .length = 200
      .start = s
      .mid = m
      .end = e
    End With
  Next i
  
  get_line_segments = mySegments
  
End Function


Sub TestStructure()
  Dim segs() As Line_Segment
  Dim i As Integer
  
  segs = get_line_segments(20)
  
  For i = 0 To UBound(segs)
    With segs(i)
      Debug.Print " **** Index " & i & " ****"
      Debug.Print "Start:", "e=" & .start.e, "n=" & .start.n
      Debug.Print "Mid:", "e=" & .mid.e, "n=" & .mid.n
      Debug.Print "End:", "e=" & .end.e, "n=" & .end.n
    End With
  Next i
    
End Sub



VBSlammer
redinvader3walking.gif

[sleeping]Unemployed in Houston, Texas
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top