Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamic Variables

Status
Not open for further replies.

shauns1

Programmer
Oct 21, 2005
53
AU
Hi

Is it possible to target a variable, whose name you create from the content of other variables? Please stay with me on this one as I'm getting little feedback from the ASP.NET world. I'm assuming I'm not explaining it well enough.

Example:
I have some variables, for arguments sake lets call them strVar1, strVar2 and strVar3. I also have an integer variable named intVarNumber.

I want to set the value of my strVar variables above but my script doesn't know the name of the variable to target.

What the script does have is a number between 1 and 3 wrapped up in the variable intVarNumber.

So, I would like to find a way to get the script to target the variable named: "strVar" & intVarNumber. Thus targeting either strVar1, strVar2 or strVar3 depending on the contents of intVarNumber.

Please please can someone ask me what the hell I'm talking about if they don't understand and I'll try to give more examples. So far on other forums I'm just getting a complete lack of response. I'm sure this must be a common problem. I just don't know the correct words/buzz words to describe it!!

Many many thanks for any help on this now major headache!

Shaun
 
I understand what you mean, but there may be an easier way to do what you want depending on the actual situation (as you've no doubt "dumbed it down" in order to explain it).

One method would be to use an array, for example.

If you decide that you want to go with the method that you originally wanted, you could always use a Case statement e.g.
Code:
        Dim i As Integer = 1

        Select Case i
            Case 1
                strVal1 = i
            Case 2
                strVal2 = i
        End Select


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Hi, and many thanks for your interest.

OK, my exact needs are as follows.

I have a DataReader that contains the number of sessions for every

day of the week thats had at least one session within the last 7

days. Typical example might be:

Sessions DayOfWeek
190 1
45 2
345 4
65 7

I have 7 variables named intDay1, intDay2, intDay3... intDay7 which

will hold the value of Sessions for that particular day.

I need to loop through the DataReader and using the value of

DayOfWeek, populate the correct variable.

Something like:

While objReader.Read
"intDay" & objReader("StrDay")) = objReader("Sessions")
End While
objReader.Close

Notice my lame attempt at trying to construct the variable name using

dynamic data!! Which of course didn't work. Assuming that we are on

the first record, the code above needs to be an the equivelant of

writing:
intDay1 = 190

Is this possible? Note that the reader does not contain a value for

every day of the week.

Many thanks

Shaun
 
In that situation, I'd just use the Case statement example that I posted above.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Hi

Yes, I think the case statement would work, although I can't see exactly how to implement it?

But I have been able to put together this work around(with a lot of help), using a HashTable rather than individual variables:

Dim objReader as OleDbDataReader = objStats.GetLast7DaysSessionsByDevID(Session("mDevelopmentID"))

While objReader.Read

statsHashTable.Add(Ctype(objReader("StrDay"), String), objReader("Sessions"))

End While
objReader.Close

I am then able to taget the HashTable using a variable later on. I think this is very close to your idea of using an array??

Thank you very much for all your help.

Shaun
 
Yes, the idea of the hash table goes along with my thinking when I mentioned arrays (I could have said a hash table or a collection as well).

To implement the case statement, you would simply use something like:
Code:
        While objReader.Read
            Select Case objReader("StrDay"))
                Case 1
                    intDay1 = objReader("Sessions")
                Case 2
                    intDay2 = objReader("Sessions")
                ...etc
            End Select
        End While


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Ah, I see!

Excellent! Thanks for everything!

Greatly appreciated.

Shaun
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top