By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,182 Members | 1,539 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,182 IT Pros & Developers. It's quick & easy.

Variable in variable?

P: n/a
Hello,

I think that what I'm trying to do is impossible, but before I give up I
thought I'd try and pick a few more knowledgeable brains than my own.

I have any array of user defined type variables. I need to loop through the
array (doing certain calculations) on a particular member variable. I then
want to loop through the array again on a different member variable doing
very similar calculations. I only need to change a couple things in the
loop, otherwise the loop is identical from one member to the other. In a
nutshell, what I'm trying to do is use a variable, that will change its
contents accordingly, that I will use to rename the member within the array.
This seems harder to explain than perhaps showing an example:

ArryName(index).MemberName '----standard syntax for refering to a
varibale within an array of user defined type.

'I declare another variable to hold the changing member name that will
be used on repeating iterations through the same loop (using nested loops)

Dim ChangingMemberName as String
ChangingMemberName = ".MemberName1"

'1st iteration

ArryName(index).ChangingMemberName

'2nd iteration

ChangingMemberName = ".MemberName2"

ArryName(index).ChangingMemberName

This code does not work and only generates errors. It would appear that
trying to rename a member within an array with another variable is not
possible but the efficiencies to be gained are too great to give up on too
quickly. I've fiddled with variations on this (using brackets, quotations
marks, different variable types, with/without the dot etc.) with no luck.

Hope this seems clear. I realize that the above is not complete code but I
hope it gets the idea across.

Any advise appreciated.

Gord
Jul 17 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
> I have any array of user defined type variables. I need to loop
through the
array (doing certain calculations) on a particular member variable. I then want to loop through the array again on a different member variable doing very similar calculations. I only need to change a couple things in the loop, otherwise the loop is identical from one member to the other.


I'm not completely clear on what you are trying to do, but the above
leads me to suggest this... create a Function or Sub (depends on whether
you are going to return anything or not), pass the member names into it
and let the Function (or Sub) code do whatever action you want. If this
approach doesn't work for you, I think you might have to explain what
you are trying to do differently than before.

Rick - MVP

Jul 17 '05 #2

P: n/a
You cannot use that approach with a UDT - accessing elements using a
variable name for a member of the UDT.

What you could do is encapsulate the UDT inside of a class. In the class
have a property "VarPointer" and set it to what ever member of the UDT to
want. The access a "Value" property from the class to get what ever value
you are pointing at.

Example:

Private Const APointer = 1
Private Const BPointer = 2
Private Const CPointer = 3

Private mVarPointer as Byte

Private Type MyUDT
A as Integer
B as Integer
C as Integer
End Type

Private mUDT() as MyUDT

Public Property Get VarPointer() as Byte
VarPointer = mVarPointer
End Property

Public Property Let VarPointer(rVarPointer as Byte)
mVarPointer = rVarPointer
End Property

Public Property Get Value(Index as Long) as Integer
Dim iInRtValue as Integer

Select Case mVarPointer
Case APointer
iInRtValue = mUDT(Index).A
Case BPointer
iInRtValue = mUDT(Index).B
Case CPointer
iInRtValue = mUDT(Index).C
End Select
End Property

Of course you would have to add more Properties for the assignment of the
UDTs.

You could also just use arrays inside of the class and eliminate the UDTs.
You could also try accessing the UDT elements directly from memory but that
gets a bit confusing with UDTs.

Hope that helps

Mike

"Gord" <x1******@telus.net> wrote in message
news:OsWUc.29176$X12.21383@edtnps84...
Hello,

I think that what I'm trying to do is impossible, but before I give up I
thought I'd try and pick a few more knowledgeable brains than my own.

I have any array of user defined type variables. I need to loop through the array (doing certain calculations) on a particular member variable. I then want to loop through the array again on a different member variable doing
very similar calculations. I only need to change a couple things in the
loop, otherwise the loop is identical from one member to the other. In a
nutshell, what I'm trying to do is use a variable, that will change its
contents accordingly, that I will use to rename the member within the array. This seems harder to explain than perhaps showing an example:

ArryName(index).MemberName '----standard syntax for refering to a
varibale within an array of user defined type.

'I declare another variable to hold the changing member name that will
be used on repeating iterations through the same loop (using nested loops)

Dim ChangingMemberName as String
ChangingMemberName = ".MemberName1"

'1st iteration

ArryName(index).ChangingMemberName

'2nd iteration

ChangingMemberName = ".MemberName2"

ArryName(index).ChangingMemberName

This code does not work and only generates errors. It would appear that
trying to rename a member within an array with another variable is not
possible but the efficiencies to be gained are too great to give up on too
quickly. I've fiddled with variations on this (using brackets, quotations
marks, different variable types, with/without the dot etc.) with no luck.

Hope this seems clear. I realize that the above is not complete code but I hope it gets the idea across.

Any advise appreciated.

Gord

Jul 17 '05 #3

P: n/a

"Gord" <x1******@telus.net> wrote in message
news:OsWUc.29176$X12.21383@edtnps84...
| Hello,
|
| I think that what I'm trying to do is impossible, but before I give up
I
| thought I'd try and pick a few more knowledgeable brains than my own.
|
| I have any array of user defined type variables. I need to loop
through the
| array (doing certain calculations) on a particular member variable. I
then
| want to loop through the array again on a different member variable
doing
| very similar calculations. I only need to change a couple things in
the
| loop, otherwise the loop is identical from one member to the other.
In a
| nutshell, what I'm trying to do is use a variable, that will change
its
| contents accordingly, that I will use to rename the member within the
array.
| This seems harder to explain than perhaps showing an example:
|
| ArryName(index).MemberName '----standard syntax for refering to
a
| varibale within an array of user defined type.
|
| 'I declare another variable to hold the changing member name that
will
| be used on repeating iterations through the same loop (using nested
loops)
|
| Dim ChangingMemberName as String
| ChangingMemberName = ".MemberName1"
|
| '1st iteration
|
| ArryName(index).ChangingMemberName
|
| '2nd iteration
|
| ChangingMemberName = ".MemberName2"
|
| ArryName(index).ChangingMemberName
|
| This code does not work and only generates errors. It would appear
that
| trying to rename a member within an array with another variable is not
| possible but the efficiencies to be gained are too great to give up on
too
| quickly. I've fiddled with variations on this (using brackets,
quotations
| marks, different variable types, with/without the dot etc.) with no
luck.
|
| Hope this seems clear. I realize that the above is not complete code
but I
| hope it gets the idea across.
|
| Any advise appreciated.
|
| Gord
|

If I understand this, the short answer is no, you can't have a variable
for the member name of a user defined type - or for any variable name,
for that matter.

Since the code is apparently similar for different members, it would
seem they are of the same data type. Perhaps using an array for them
would be better. For instance, you can put an array of doubles within a
user type, and then have an array of that user type. Then you would just
need nested for loops. If you want your code to be more readable, you
could use an Enum to define constants for use in place of index values:

MemberName1 = 1
MemberName2 = 2

etc.
Jul 17 '05 #4

P: n/a

"Mike" <em***@noserver.com> wrote in message
news:e_********************@news20.bellglobal.com. ..
You cannot use that approach with a UDT - accessing elements using a
variable name for a member of the UDT.

What you could do is encapsulate the UDT inside of a class. In the class
have a property "VarPointer" and set it to what ever member of the UDT to
want. The access a "Value" property from the class to get what ever value
you are pointing at.

Example:

Private Const APointer = 1
Private Const BPointer = 2
Private Const CPointer = 3

Private mVarPointer as Byte

Private Type MyUDT
A as Integer
B as Integer
C as Integer
End Type

Private mUDT() as MyUDT

Public Property Get VarPointer() as Byte
VarPointer = mVarPointer
End Property

Public Property Let VarPointer(rVarPointer as Byte)
mVarPointer = rVarPointer
End Property

Public Property Get Value(Index as Long) as Integer
Dim iInRtValue as Integer

Select Case mVarPointer
Case APointer
iInRtValue = mUDT(Index).A
Case BPointer
iInRtValue = mUDT(Index).B
Case CPointer
iInRtValue = mUDT(Index).C
End Select
** Forgot This HERE

Value = iInRtValue

** Oops
End Property

Of course you would have to add more Properties for the assignment of the
UDTs.

You could also just use arrays inside of the class and eliminate the UDTs.
You could also try accessing the UDT elements directly from memory but that gets a bit confusing with UDTs.

Hope that helps

Mike

"Gord" <x1******@telus.net> wrote in message
news:OsWUc.29176$X12.21383@edtnps84...
Hello,

I think that what I'm trying to do is impossible, but before I give up I
thought I'd try and pick a few more knowledgeable brains than my own.

I have any array of user defined type variables. I need to loop through the
array (doing certain calculations) on a particular member variable. I

then
want to loop through the array again on a different member variable doing very similar calculations. I only need to change a couple things in the
loop, otherwise the loop is identical from one member to the other. In a nutshell, what I'm trying to do is use a variable, that will change its
contents accordingly, that I will use to rename the member within the

array.
This seems harder to explain than perhaps showing an example:

ArryName(index).MemberName '----standard syntax for refering to a
varibale within an array of user defined type.

'I declare another variable to hold the changing member name that will be used on repeating iterations through the same loop (using nested loops)
Dim ChangingMemberName as String
ChangingMemberName = ".MemberName1"

'1st iteration

ArryName(index).ChangingMemberName

'2nd iteration

ChangingMemberName = ".MemberName2"

ArryName(index).ChangingMemberName

This code does not work and only generates errors. It would appear that
trying to rename a member within an array with another variable is not
possible but the efficiencies to be gained are too great to give up on too quickly. I've fiddled with variations on this (using brackets, quotations marks, different variable types, with/without the dot etc.) with no luck.
Hope this seems clear. I realize that the above is not complete code

but I
hope it gets the idea across.

Any advise appreciated.

Gord


Jul 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.