?
Solved

USER DEFINE DATA TYPE WITH ARRAY

Posted on 1999-07-26
8
Medium Priority
?
148 Views
Last Modified: 2010-04-30
Can I declare a user data type which has  an array inside?
With the declared data type mentioned just now, can I have an array of that data type ?
And how could I pass this data type with its array component into a function?
Please include example code.
0
Comment
Question by:yuanwuhui
8 Comments
 
LVL 10

Accepted Solution

by:
caraf_g earned 300 total points
ID: 1527435
Yes, absolutely no problem:

(VB6)

For example:

Option Explicit
Private Type JustAUDT
    Anarray() As String
    AnutherString As String
End Type
Private Sub Form_Load()

Dim ThisIsJustAUDT As JustAUDT
ReDim ThisIsJustAUDT.Anarray(10)

With ThisIsJustAUDT
    .Anarray(0) = "Hello"
    .Anarray(2) = "World"
End With

Dim LotsOfJustAUDTs(1 To 5) As JustAUDT

End Sub

0
 
LVL 10

Expert Comment

by:caraf_g
ID: 1527436
Option Explicit
Private Type JustAUDT
    Anarray() As String
    AnutherString As String
End Type
Private Sub Form_Load()

Dim ThisIsJustAUDT As JustAUDT
ReDim ThisIsJustAUDT.Anarray(10)

With ThisIsJustAUDT
    .Anarray(0) = "Hello"
    .Anarray(2) = "World"
End With

Dim LotsOfJustAUDTs(1 To 5) As JustAUDT

ReDim LotsOfJustAUDTs(2).Anarray(5)

LotsOfJustAUDTs(2).Anarray(3) = "See, it works!"

MsgBox LotsOfJustAUDTs(2).Anarray(3)

End Sub


0
 
LVL 10

Expert Comment

by:caraf_g
ID: 1527437
Or, without ReDims

Option Explicit
Private Type JustAUDT
    Anarray(4) As String
    AnutherString As String
End Type
Private Sub Form_Load()

Dim ThisIsJustAUDT As JustAUDT

With ThisIsJustAUDT
    .Anarray(0) = "Hello"
    .Anarray(2) = "World"
End With

Dim LotsOfJustAUDTs(1 To 5) As JustAUDT

LotsOfJustAUDTs(2).Anarray(3) = "See, it works!"

MsgBox LotsOfJustAUDTs(2).Anarray(3)

End Sub


0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
LVL 2

Expert Comment

by:jaywalk
ID: 1527438
Un question...

Why did you post it twice, caraf_g?
0
 
LVL 18

Expert Comment

by:mdougan
ID: 1527439
Arg, caraf_g beat me to the punch again...

Yes, you can declare a user data type which has an array inside.

Yes, you can create an array of that data type.

Here is a code example of a variable dimmed as a user defined type that has two arrays inside, an integer and a string array.  The code under the command1_click fills the array, by passing the user typed variable to a function.  The code under the command2_click displays it by passing it to another function.

In general, if you want to see how to pass a variable as an array to a function, uncomment the comment in the command1_click.

You could easily make "z" in the example below an array, just by dimming it as Dim z(3) as TEST, and then, every where that you have a reference to z, you'd have to put z(index)

Private Type TEST
 x(5) As Integer
 s(5) As String * 10
End Type

Dim z As TEST
Private Sub Command1_Click()
Dim MyArray As Variant

'MyArray = Array(0, 1, 2, 3, 4, 5)
Fillit z, MyArray
End Sub

Private Sub Fillit(z As TEST, MA As Variant)
Dim i As Integer

If IsArray(MA) Then
    For i = 0 To UBound(MA)
       If i < UBound(z.x) Then
          z.x(i) = MA(i)
          z.s(i) = CStr(MA(i))
       Else
          Exit For
       End If
    Next i
Else
    z.x(5) = 0
    z.s(5) = "zero"
    z.x(4) = 1
    z.s(4) = "one"
    z.x(3) = 2
    z.s(3) = "two"
    z.x(2) = 3
    z.s(2) = "three"
    z.x(1) = 4
    z.s(1) = "four"
    z.x(0) = 5
    z.s(0) = "five"
End If
End Sub

Private Sub Showit(z As TEST)
Dim msg As String
msg = z.x(0) & "," & _
      z.x(1) & "," & _
      z.x(2) & "," & _
      z.x(3) & "," & _
      z.x(4) & "," & _
      z.x(5) & vbCrLf & _
      Trim$(z.s(0)) & "," & _
      Trim$(z.s(1)) & "," & _
      Trim$(z.s(2)) & "," & _
      Trim$(z.s(3)) & "," & _
      Trim$(z.s(4)) & "," & _
      Trim$(z.s(5))

MsgBox msg
End Sub

Private Sub Command2_Click()
Showit z
End Sub

0
 
LVL 10

Expert Comment

by:caraf_g
ID: 1527440
Hi jaywalk - the first time I forgot to include the three other example lines after
Dim LotsOfJustAUDTs(1 To 5) As JustAUDT
And then I decided to also show an example that didn't need to ReDim anything. Sorry for the mess ;-)
0
 

Author Comment

by:yuanwuhui
ID: 1527441
Thanks
0
 
LVL 10

Expert Comment

by:caraf_g
ID: 1527442
"B" - what did you want, an essay? :-(
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Question has a verified solution.

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

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

589 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