Using the Type Function with Arrays
Forums
I found this out by playing around with it but you can use the Type function within an Array. For a refresher the type allows you to create a data type containing one or more elements. You then use it as a dot variable, in the example below I have created a type that holds three elements, the last name, first name and middle initial. I have grouped them all together in a type function called FullName. To access the type you would define a varialble such as Dim ClientName() as FullName for an array or Dim ClientName as FullName for a single variable. To set any of the elements I would add a dot after the type name and then add the element, such as if I wanted to add the LastName I would have ClientName.LastName = "My Last Name" and if I want to get what is in there I just reference it in the same way, such as MsgBox ClientName.LastName.
I played around with it and I realized you can create an array based on the type, this is great because you can now use it to hold multiple related information. Such as if you want to get all the field information for a database, you create a type to hold the field name, field type, field description, etc and then you create an array to hold all those elements in one type instead of having multiple single arrays. Below is an example of setting up an array and entering and reading the information. You will notice that the array belongs to the Type name and is positioned prior to the dot to access the type elements. Such as ClientName(1).LastName would access the last name of the array element number 1.
So I am just wondering if any one else has used this in their scripts? I have used it a few times.
'Demo of using the Type with Arrays
Type FullName
' Add the three items in a full name.
LastName As String
FirstName As String
MiddleInitial As String
End Type
Sub Main
Dim ClientName() As FullName
Call typeFunction(ClientName)
End Sub
Function typeFunction(ByRef ClientName() As FullName)
Dim bFirstTime As Boolean
ReDim ClientName(0)
bFirstTime = True
For i = 0 To 5
If bFirstTime Then
ClientName(0).LastName = "My LastName" & i
bFirstTime = False
Else
ReDim preserve ClientName(UBound(ClientName) + 1)
ClientName(UBound(ClientName)).LastName = "My LastName" & i
End If
Next i
For i = 0 To 5
MsgBox ClientName(i).LastName
Next i
End Function
Hi Joma, I have played with
Hi Joma, I have played with it a bit and it looks like you can only send the entire array. If you want a single value then you could do something like this:
call myTypeValue(ClientName(3).LastName)
This would send the last name for item 3 in the array as a string. Not sure if that works for you.
Hi Brian
Hi Brian
Can I send a specific position of the array ClientName as parameter of another Funtion?
Thanks.