473,386 Members | 1,720 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,386 software developers and data experts.

Variable Reference

HB
After trying for days!! I need help:

Is there any way I can refer to a variable with another variable.

I.e.: I have many variables: var1 , var2 , var3 etc etc
I want to use a loop to change these variables, for this example double
their value
instead of writing
var1 = var1 * 2
var2 = var2 * 2
var3 = var3 * 2
var4 = var4 * 2
etc etc
be nice if I could do the following
for each a in array(var1,var2,var3 etc etc):a = a*2:next

Thanks in advance
Jul 17 '05 #1
12 6705
How about something like...

Dim lngVar as Long, lngLastVar as Long

lngVar = 1
' You need to put the number of your highest variable here
lngLastVar = 56

Do until lngVar > lngLastVar
' var1 = var1 * 2
var & lngVar = var & lngVar * 2
' Increment the var number
lngVar = lngVar + 1

Loop

Gary Miller
Sisters, OR

"HB" <fr*****@telus.net> wrote in message
news:Jkfob.86718$EO3.31124@clgrps13...
After trying for days!! I need help:

Is there any way I can refer to a variable with another variable.
I.e.: I have many variables: var1 , var2 , var3 etc etc
I want to use a loop to change these variables, for this example double their value
instead of writing
var1 = var1 * 2
var2 = var2 * 2
var3 = var3 * 2
var4 = var4 * 2
etc etc
be nice if I could do the following
for each a in array(var1,var2,var3 etc etc):a = a*2:next

Thanks in advance

Jul 17 '05 #2
Private Sub ColInts()
Dim var1 As Integer
Dim var2 As Integer
Dim var3 As Integer
Dim var4 As Integer
Dim colVar As New Collection
Dim a As Variant

var1 = 1
var2 = 1
var3 = 1
var4 = 1

colVar.Add var1, "var1"
colVar.Add var2, "var2"
colVar.Add var3, "var3"
colVar.Add var4, "var4"

For Each a In colVar
Debug.Print a
CalcVar a, 2
Debug.Print a
Next a
End Sub
Private Sub CalcVar(ByRef iVar As Variant, ByVal iMultiply As Integer)
iVar = iVar * iMultiply
End Sub

"HB" <fr*****@telus.net> wrote in message news:Jkfob.86718$EO3.31124@clgrps13...
After trying for days!! I need help:

Is there any way I can refer to a variable with another variable.

I.e.: I have many variables: var1 , var2 , var3 etc etc
I want to use a loop to change these variables, for this example double
their value
instead of writing
var1 = var1 * 2
var2 = var2 * 2
var3 = var3 * 2
var4 = var4 * 2
etc etc
be nice if I could do the following
for each a in array(var1,var2,var3 etc etc):a = a*2:next

Thanks in advance

Jul 17 '05 #3
> Is there any way I can refer to a variable with another variable.

I.e.: I have many variables: var1 , var2 , var3 etc etc
I want to use a loop to change these variables, for this example double
their value
instead of writing
var1 = var1 * 2
var2 = var2 * 2
var3 = var3 * 2
var4 = var4 * 2
etc etc
be nice if I could do the following
for each a in array(var1,var2,var3 etc etc):a = a*2:next


So, why not just declare an array and you can refer to the array's index in
your loop:

'************EXAMPLE START
Dim dblMyVar(1 To 4) As Double
.... <assign values to array's rows> ...

Dim iCount As Integer 'To hold array's index values in For ... Next
'Manipulate values in array
For iCount = 1 to Ubound(dblMyVar()) '1 To 4, in this case
dblMyVar(iCount) = dblMyVar(iCount) * 2
Next
'************EXAMPLE END

--
Bruce M. Thompson, Microsoft Access MVP
bt******@mvps.org (See the Access FAQ at http://www.mvps.org/access)
NO Email Please. Keep all communications

within the newsgroups so that all might benefit.<<
Jul 17 '05 #4
HB
Gary
I get an error on

var & lngVar = var & lngVar * 2
thanks anyway!

Brain

"Gary Miller" <gm*******@SPAM.sistersnet.com> wrote in message
news:up**************@TK2MSFTNGP09.phx.gbl...
How about something like...

Dim lngVar as Long, lngLastVar as Long

lngVar = 1
' You need to put the number of your highest variable here
lngLastVar = 56

Do until lngVar > lngLastVar
' var1 = var1 * 2
var & lngVar = var & lngVar * 2
' Increment the var number
lngVar = lngVar + 1

Loop

Gary Miller
Sisters, OR

"HB" <fr*****@telus.net> wrote in message
news:Jkfob.86718$EO3.31124@clgrps13...
After trying for days!! I need help:

Is there any way I can refer to a variable with another

variable.

I.e.: I have many variables: var1 , var2 , var3 etc etc
I want to use a loop to change these variables, for this

example double
their value
instead of writing
var1 = var1 * 2
var2 = var2 * 2
var3 = var3 * 2
var4 = var4 * 2
etc etc
be nice if I could do the following
for each a in array(var1,var2,var3 etc etc):a = a*2:next

Thanks in advance


Jul 17 '05 #5
Eval() function may help.

"HB" <fr*****@telus.net> wrote in message
news:Jkfob.86718$EO3.31124@clgrps13...
After trying for days!! I need help:

Is there any way I can refer to a variable with another variable.

I.e.: I have many variables: var1 , var2 , var3 etc etc
I want to use a loop to change these variables, for this example double
their value
instead of writing
var1 = var1 * 2
var2 = var2 * 2
var3 = var3 * 2
var4 = var4 * 2
etc etc
be nice if I could do the following
for each a in array(var1,var2,var3 etc etc):a = a*2:next

Thanks in advance

Jul 17 '05 #6

"Bruce M. Thompson" <bthmpson@big_NOSPAM_foot.com> wrote in message
news:OP****************@TK2MSFTNGP10.phx.gbl...
Is there any way I can refer to a variable with another variable.

I.e.: I have many variables: var1 , var2 , var3 etc etc
I want to use a loop to change these variables, for this example double
their value
instead of writing
var1 = var1 * 2
var2 = var2 * 2
var3 = var3 * 2
var4 = var4 * 2
etc etc
be nice if I could do the following
for each a in array(var1,var2,var3 etc etc):a = a*2:next
So, why not just declare an array and you can refer to the array's index in
your loop:


Maybe the OP would have occasion to refer to an individual var instead of all of them as a group.
In such a case, the Var Name would be of consequence.
'************EXAMPLE START
Dim dblMyVar(1 To 4) As Double
... <assign values to array's rows> ...

Dim iCount As Integer 'To hold array's index values in For ... Next
'Manipulate values in array
For iCount = 1 to Ubound(dblMyVar()) '1 To 4, in this case
dblMyVar(iCount) = dblMyVar(iCount) * 2
Next
'************EXAMPLE END

--
Bruce M. Thompson, Microsoft Access MVP
bt******@mvps.org (See the Access FAQ at http://www.mvps.org/access)
NO Email Please. Keep all communications

within the newsgroups so that all might benefit.<<

Jul 17 '05 #7
> Maybe the OP would have occasion to refer to an individual var instead of
all of them as a group.
In such a case, the Var Name would be of consequence.


??? Are you saying that referencing "Var(1)", rather than "Var1" would be of
consequence? Please clarify your meaning.

--
Bruce M. Thompson, Microsoft Access MVP
bt******@mvps.org (See the Access FAQ at http://www.mvps.org/access)
NO Email Please. Keep all communications

within the newsgroups so that all might benefit.<<
Jul 17 '05 #8
> Eval() function may help.

Won't work with VBA variable names. It *would* have made things easier, though.

:-)
--
Bruce M. Thompson, Microsoft Access MVP
bt******@mvps.org (See the Access FAQ at http://www.mvps.org/access)
NO Email Please. Keep all communications

within the newsgroups so that all might benefit.<<
Jul 17 '05 #9
d'Oh....
"Bruce M. Thompson" <bthmpson@big_NOSPAM_foot.com> wrote in message
news:ee**************@TK2MSFTNGP12.phx.gbl...
Maybe the OP would have occasion to refer to an individual var instead of

all of them as a group.
In such a case, the Var Name would be of consequence.


??? Are you saying that referencing "Var(1)", rather than "Var1" would be of
consequence? Please clarify your meaning.

--
Bruce M. Thompson, Microsoft Access MVP
bt******@mvps.org (See the Access FAQ at http://www.mvps.org/access)
NO Email Please. Keep all communications

within the newsgroups so that all might benefit.<<

Jul 17 '05 #10
;-)

--
Bruce M. Thompson, Microsoft Access MVP
bt******@mvps.org (See the Access FAQ at http://www.mvps.org/access)
NO Email Please. Keep all communications

within the newsgroups so that all might benefit.<<
Jul 17 '05 #11
> > Maybe the OP would have occasion to refer to an individual var instead
of
all of them as a group.
In such a case, the Var Name would be of consequence.
??? Are you saying that referencing "Var(1)", rather than "Var1" would be

of consequence? Please clarify your meaning.


Actually, I got the impression that Var1, Var2, Var3, etc. were generic
names for the posting example only and that the OP was using more project
specific names that didn't share a common "root" like Var. However, if that
is the case, then I think the OP will have trouble doing what he asked for.

Rick - MVP
Jul 17 '05 #12
> Actually, I got the impression that Var1, Var2, Var3, etc. were generic
names for the posting example only and that the OP was using more project
specific names that didn't share a common "root" like Var. However, if that
is the case, then I think the OP will have trouble doing what he asked for.


Agreed. I provided a solution that would allow for performing the type of
operation the OP suggested within a loop, but it may not fit his particular
requirement. Of course, he could always rewrite his application to fit my
solution. <G>

--
Bruce M. Thompson, Microsoft Access MVP
bt******@mvps.org (See the Access FAQ at http://www.mvps.org/access)
NO Email Please. Keep all communications

within the newsgroups so that all might benefit.<<
Jul 17 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

83
by: Alexander Zatvornitskiy | last post by:
Hello All! I'am novice in python, and I find one very bad thing (from my point of view) in language. There is no keyword or syntax to declare variable, like 'var' in Pascal, or special syntax in...
2
by: Kench | last post by:
I was curious and playing with pointers and references to see what's different between them. Other than the obvious ones involving C++ syntax & things like references cannot be modified with...
7
by: Klaus Johannes Rusch | last post by:
Is the following code valid and supported by current implementations? function somename() { this.show = function () { document.write("somename called") } } var somename = new somename();...
166
by: Graham | last post by:
This has to do with class variables and instances variables. Given the following: <code> class _class: var = 0 #rest of the class
7
by: Doug | last post by:
Is there any harm in passing an object into a method with the 'ref' keyword if the object is already a reference variable? If not, is there any benefit?
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...
20
by: weston | last post by:
I've got a piece of code where, for all the world, it looks like this fails in IE 6: hometab = document.getElementById('hometab'); but this succeeds: hometabemt =...
4
by: siddhu | last post by:
If there is reference member variable in the class, why doesn't default assignment operator work? class A { int& i; public: A( int& ii):i(ii){} //A& operator=(const A& a){i = a.i;} };
7
by: pauldepstein | last post by:
#include <iostream> using namespace std; double & GetWeeklyHours() { double h = 46.50; double &hours = h; return hours; }...
9
by: sulekhasweety | last post by:
dear all, can anyone explain the differences between pointer variable and reference variables ?
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.