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

set variable value when variable name is stored in array

dsatino
100+
P: 393
I'm sure I've actually done this before, so why I can't figure this out now is annoying. I'm hoping someone can spark my memory here.

Basically, I have 54 variable names stored in an array. I want to set the values of the variable values using the array as the name reference.

In other words, instead of:
x=1

I want to set x by referring to it's location in the array.
I tried:
myarray(0,0)=1

Unfortunately, this just changes the variable name that I have stored in the array to 1. Ha! Not quite what I want.

Any help would be greatly appreciated.
Aug 9 '10 #1
Share this Question
Share on Google+
9 Replies


ADezii
Expert 5K+
P: 8,701
I cannot understand why you would use a Multi-Dimensional Array, but in any case, in the following example, the Value of X would be equal to whatever is stored in the 3rd and 6th Elements at the intersection of the 1st and 2nd Dimension of the Array MyArray (assuming the Array is dimensioned starting at 0):
Expand|Select|Wrap|Line Numbers
  1. X = MyArray(2, 5)
Aug 9 '10 #2

NeoPa
Expert Mod 15k+
P: 31,768
I looked at using Eval(), but that doesn't seem to do the business.

As variables are not generally objects either, even populating your array with object references beforehand (in place of the simple names of the variables) would not work for you. Sorry. I'm stumped.

Much easier in C derivative languages of course.
Aug 10 '10 #3

ADezii
Expert 5K+
P: 8,701
Sorry that I misread this Thread. Why not simply have a Hidden Lookup Table with Strings representing Variable Names along with/without associated Values? Setting/Retrieving Values would be a simple task. If you don't mind me asking, what is the purpose of this?
Aug 10 '10 #4

dsatino
100+
P: 393
The purpose, originally, was to set variables without having to type them. There are about 60 of them that get set at various stages of 3 nested loops. Depending on user inputs some may not even be used. The game plan to have it all neat and organized so that each variable would have a set location in the array that would align with the loop variables and hence, set the correct variable at the correct time.

However, after coming in this morning with a fresh cup of coffee and a few hours of not trying to force this to work, it occurred to me that the array could just as easily store the variable values. (isn't that what arrays are for anyway? duh.)

In any case, thanks all for your time and efforts as usual.

P.S. I did find a previous project where I thought I was doing this and it turns out that I was calling functions. I also found documentation that stated that what I was trying to do was not possible with the VBA version of eval().
Aug 10 '10 #5

NeoPa
Expert Mod 15k+
P: 31,768
This may seem somewhat radical, but have you considered using the array instead of the variables completely? It may not be an appropriate solution across the board, so only you can determine how much sense is in that.
Aug 10 '10 #6

dsatino
100+
P: 393
Yup, that's exactly what I meant when I said I would use the array to store the variable values instead of the variable names. So the variables themselves will cease to exist and the array will store all values with each specific location in the array relating to a certain point in the nested loops.
Aug 10 '10 #7

NeoPa
Expert Mod 15k+
P: 31,768
Oh I see. Sorry. I misunderstood you.

Good idea though :D
Aug 10 '10 #8

agbb0cg
P: 8
Is this what you need to do?

Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2.   var a = "test"
  3.   var arr = new Array("a");
  4.   alert(window[arr [0]]);
  5.   window[arr [0]] = 'value changed';
  6.   alert(window[arr [0]]);
  7. </script>
  8.  
Aug 10 '10 #9

NeoPa
Expert Mod 15k+
P: 31,768
I'm afraid Access uses VBA exclusively. Javascript couldn't even run within Access.
Aug 12 '10 #10

Post your reply

Sign in to post your reply or Sign up for a free account.