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

Assigning to dynamic variable?

P: n/a
I have methods such as the following:

void GetA20(somevar)
{
A20 = ONE
}

void GetA30(somevar)
{
A30 = ONE
}

void GetA40(somevar)
{
A40 = ONE
}

I need the three (A20, A30, A40) variables since they are used in XML,
per the receiver's guidelines. I like to create one method, send in
another variable containing the string 20, 30, or 40 and assign to A.
For example:

timeRange = 20;

void GetA(somevar, timeRange)
{
(A + timeRange) = ONE
}

which would mean:
A20 = ONE

The above is an extremely simple scenario. I can use case or if
conditionals but would rather avoid them. Is there a clean way to do
this?

Thanks,
Brett

Sep 26 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Brett,

Assuming you have fields named A20, A30 and so on, you could use
reflection to get the field and then set the value.

You could also just use a dictionary which is keyed on the string values
"A20", "A30" and so on to store your values.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Brett Romero" <ac*****@cygen.comwrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...
>I have methods such as the following:

void GetA20(somevar)
{
A20 = ONE
}

void GetA30(somevar)
{
A30 = ONE
}

void GetA40(somevar)
{
A40 = ONE
}

I need the three (A20, A30, A40) variables since they are used in XML,
per the receiver's guidelines. I like to create one method, send in
another variable containing the string 20, 30, or 40 and assign to A.
For example:

timeRange = 20;

void GetA(somevar, timeRange)
{
(A + timeRange) = ONE
}

which would mean:
A20 = ONE

The above is an extremely simple scenario. I can use case or if
conditionals but would rather avoid them. Is there a clean way to do
this?

Thanks,
Brett

Sep 26 '06 #2

P: n/a
Thanks Nicholas. In the end, I did go with the Dictionary<>. I
thought about using a ref out parameter but it didn't seem as flexible.

Brett

Sep 26 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.