473,320 Members | 1,872 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Variable in variable?

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
4 3719
> 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
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

"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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Scott | last post by:
I have an XML Document in a format like: <Variable name="Bob">ABCDEFG</Variable> <Variable name="Steve">QWERTYUI</Variable> <Variable name="John">POIUYTR</Variable> <Variable...
4
by: Frederik Sørensen | last post by:
I include a xslt stylesheet with variables for all the error messages in my system. <xsl:variable name="Banner_error_1"> errormessage 1 for banner </xsl:variable> <xsl:variable...
134
by: James A. Donald | last post by:
I am contemplating getting into Python, which is used by engineers I admire - google and Bram Cohen, but was horrified to read "no variable or argument declarations are necessary." Surely that...
10
by: Blaxer | last post by:
There is probably a really easy way to do this, so please forgive me but I would like to set the value of a variable from a variable, an example would be... function Calculate_Something(ByVal...
23
by: Russ Chinoy | last post by:
Hi, This may be a totally newbie question, but I'm stumped. If I have a function such as: function DoSomething(strVarName) { ..... }
3
by: rls03 | last post by:
I have the following which creates a variable containing a relative path where <xsl:value-of select="."/returns a portion of the filename: <xsl:variable...
1
pbmods
by: pbmods | last post by:
VARIABLE SCOPE IN JAVASCRIPT LEVEL: BEGINNER/INTERMEDIATE (INTERMEDIATE STUFF IN ) PREREQS: VARIABLES First off, what the heck is 'scope' (the kind that doesn't help kill the germs that cause...
2
by: Florian Loitsch | last post by:
hi, What should be the output of the following code-snippet? === var x = "global"; function f() { var x = 0; eval("function x() { return false; }"); delete x; alert(x); }
37
by: minkoo.seo | last post by:
Hi. I've got a question on the differences and how to define static and class variables. AFAIK, class methods are the ones which receives the class itself as an argument, while static methods...
112
by: istillshine | last post by:
When I control if I print messages, I usually use a global variable "int silent". When I set "-silent" flag in my command line parameters, I set silent = 1 in my main.c. I have many functions...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.