Go Back   Forum Care Forums > Development Reference Area > Visual Basic

Reply
 
LinkBack Thread Tools Display Modes
Variable in variable?
Old
  (#1)
Gord
Guest
 
Posts: n/a
Default Variable in variable? - 06-04-2007, 09:46 AM

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


   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Re: Variable in variable?
Old
  (#2)
Rick Rothstein
Guest
 
Posts: n/a
Default Re: Variable in variable? - 06-04-2007, 09:46 AM

> 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), p*** 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

   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Re: Variable in variable?
Old
  (#3)
Mike
Guest
 
Posts: n/a
Default Re: Variable in variable? - 06-04-2007, 09:47 AM

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 cl***. In the cl***
have a property "VarPointer" and set it to what ever member of the UDT to
want. The access a "Value" property from the cl*** 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 ***ignment of the
UDTs.

You could also just use arrays inside of the cl*** 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" <EMAIL REMOVED> 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
>
>



   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Re: Variable in variable?
Old
  (#4)
Steve Gerrard
Guest
 
Posts: n/a
Default Re: Variable in variable? - 06-04-2007, 09:47 AM


"Gord" <EMAIL REMOVED> 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.


   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Re: Variable in variable? Forgot This
Old
  (#5)
Mike
Guest
 
Posts: n/a
Default Re: Variable in variable? Forgot This - 06-04-2007, 09:47 AM


"Mike" <EMAIL REMOVED> wrote in message
news:e_dVc.30323$EMAIL REMOVED. ..
> 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 cl***. In the cl***
> have a property "VarPointer" and set it to what ever member of the UDT to
> want. The access a "Value" property from the cl*** 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 ***ignment of the
> UDTs.
>
> You could also just use arrays inside of the cl*** 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" <EMAIL REMOVED> 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
> >
> >

>
>



   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On





Contact Us - Forum Care Forums - Archive - Top