# Modify User Defined Data in A Function

The code below works to the point MsgBox v.
Using watch I can see the data in the TD() and then data() in the function. Msgbox displays but contains nothing.
My goal is to fill or modify TD by passing to a function. Again using watch I can see the data in each variable. However I donot see the modified data in Main
Thank You
Marlin

``````REM  *****  BASIC  *****
'Test passing Used defined Data types to functions
Public Type TestData
D1 as Long
D2 as String
End Type

Sub Main

Dim TD(5) As New TestData
Dim i, j As Integer
Dim s as String
Dim v as Variant
j = 4

For i = 0 to j
TD(i).D1 = i
TD(i).D2 = Chr(134 + j * 2) & " More Data " & j^3
s = s + "TD(" & i & ")= " & TD(i).D1  & Chr(13) & _
"TD(" & i & ")= " & TD(i).D2  & Chr(13)
Next i
MsgBox s

v = SetTestData(TD())

MsgBox v
End Sub

Function SetTestData( data as Variant) as Variant
Dim i As Integer
For i = LBound(data) to UBound(data)
data(i).D1 = i^2
data(i).D2 = Chr(217 + j) & " Addtional Data " & j^3

Next i
SetTestData = data()
End Function
``````

[erAck: edited to format code as code … see This is the guide - How to use the Ask site? - #6 by erAck ]

@Lupp is correct about User Defined Types. Perhaps here if you know that the size of TD is fixed then just Dim out TD as

`Dim TD(0 To 5, 0 To 1) As Variant`

and use the second dimension for your D1 and D2. For example, TD(4, 1) instead of TD(4).D2.

If you may have to ReDim your TD, then switch dimensions

`Dim TD(0 To 1, 0 To 5) As Variant`

Other hints:

1. Make sure you do use `Option Explicit`
2. Once you use arrays, show the world your intended lower bound with `Option Base 0`.

For example, I do not see where j is coming from in your SetTestData. You don’t pass it, and without Option Explicit you have no way to realize that it’s just a 0.

For the output of the original data you prepare the string `s` by concatenation (as needed).
Why do you expect that `MsgBox` can output the array `v` of data of user defined type?
It can’t. But you can inspect the returned array.in the `Watch` pane.

In addition: Be careful when relying on user defined types. They often don’t behave as expected and needed. They probably are rarely used. A very old bug may have survived therefore for decades.
See https://bugs.documentfoundation.org/show_bug.cgi?id=146082