PDA

View Full Version : Solved: User defined data type - array of an array!



agarwaldvk
02-20-2007, 04:43 PM
Hi everybody

I have a need to declare a variable - essentially has to be a 7 element array of variants . However, the need is that each element of this array needs to be an array itself, the size of which preferably needs to be dynamically deteremined during the running of the program. The size of the array for each the elements in the parent array is the same.

Essentially, is it possible to define an array of user defined type variable, each element of which is an array with dynamically determined array size?

Any suggestions please?


Best regards



Deepak Agarwal

GreenTree
02-20-2007, 08:31 PM
If I'm understanding what you're asking, I'll post a couple of snippets that I'm using in a project that work well.

Type BType
X as String
Y as Integer
'etc
End Type

Type CType
Z as String
Z1 as integer
Z2 as integer
'etc
End Type

Type AType
TheBs() As BType
TheCs() As CType
End Type

Public A(1 to 10) as AType
Public MaxB(1 to 10) as Integer
Public MaxC(1 to 10) as Integer

' assign the MaxB and MaxC variables to whatever they need to be, then

For I = 1 to 10
ReDim A(I).TheBs(1 to MaxB(I))
ReDim A(I).TheCs(1 to MaxC(I))
Next I
What's going on here? With 10 cities, each one an A, each city can have some number of B's, represented by MaxB(), and some number of C's, represented as MaxC(). So A(1) might have 52 B's and 58 C's, which would be A(1).TheBs(J) and A(1).TheCs(K) where J is 1 to 52 (i.e. 1 to MaxB(1)) and K is 1 to 58 (ie 1 to MaxC(1)). A(2) could have a different # of each, and so on up to A(10). Is this answering what you are asking, or did I just answer a different question?

Apologies up front if the code above seems a little disjointed; when I got done renaming things from the indecipherable jargon of my project & stripping out all the extraneous stuff, it was just easier to retype the whole thing. The main idea, however, hopefully is intact.

Regards,

GT

agarwaldvk
02-20-2007, 08:46 PM
Dear GreenTree

Mate, thanks for your response. Its a bit much for me to understand from a glance - I am slow learner!

I will take this home with me tonight and respond to see if this is what I asked for - perhaps I may not have asked the right question - a prerequisite to getting the right answer!

Shall let you know tomorrow!


Best regards


Deepak Agarwal

GreenTree
02-20-2007, 09:03 PM
Glad to (attempt to) be of help.

See if this helps make sense of what my project does. Let's say that I'm looking at 10 cities, with each A is one of them. Each city as some number of busses and some number of vans that I'm dealing with. The busses are B's, so what I call BType would contain all the information that I want to track for a single bus... manufacture date, last oil change, whatever. The vans would be the C's, and the CType would have the info that I want for the vans... not necessarily the same things as I'm tracking for the busses. So what I want to do is to be able to refer to city # I, and bus #J of that city. Thus A(I).TheBs(J).Y might become, in less generic terms, City(I).Bus(J).OilChange

The salient point was that you CAN, in fact ReDim a different # of busses for each city... city 1 can have 7 while city 2 can have 23 while city 3 can have 16 etc etc. Apologies for going so generic; the stuff in my app is rather laden with the jargon of my "real life" job and it would have taken too long to try to explain what each term means; hope the city/bus/van example helps.

GT

Bob Phillips
02-21-2007, 03:52 AM
This might help you to understand



Dim aryPrimary
Dim arySub1
Dim arySub2

arySub1 = ActiveSheet.Evaluate("{1,2,3}")
arySub2 = ActiveSheet.Evaluate("{""a"",""b"",""c"",""d""}")

aryPrimary = Array(arySub1, arySub2)
MsgBox aryPrimary(0)(2)
MsgBox aryPrimary(1)(4)
MsgBox aryPrimary(0)(4)

agarwaldvk
02-21-2007, 02:53 PM
Dear GreenTree

Eventually, I did understand what you said in your response. That was exactly what I was trying to do and needed. Fantastic!

From your explanation this morning, it was great know that I can have different sized component arrays of a user defined type variable - gives you a lot of flexibility. Whilst in this particular instance, I don't need that degree of flexibility but it certainly is great to be able to have that as an option should I have a need for the same in the future.

Greatly appreciated! You are a legend mate!


Best regards



Deepak Agarwal

lucas
02-21-2007, 03:17 PM
Dear GreenTree
You are a legend mate!

Please don't encourage our Texas friends. Okies have enough trouble with them and the only thing we have between us is the Red River.:devil2: Go big Red! Nice job GT