Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

USER DEFINE DATA TYPE WITH ARRAY

Posted on 1999-07-26
8
Medium Priority
?
145 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
[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
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
Independent Software Vendors: 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!

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
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…
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…
Suggested Courses

721 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