Is this possible

M

Michelle

I'm curious if this is posible.

I've some string variables declared as const.
public const string number_1 = "This is number 1";
public const string number_2 = "This is number 2";
[. . .]

Later in my code i've a int variable iResult containing the value '1'.
When i assign the variable number_1 to a new string variable newVar using:
string newVar = "number_" + iResult

newVar contains 'number_1'.

But is it possible to ensure that newVar contains 'This is number 1'.
(make the string act like a variable name)

Thanks in advance,

Michelle
 
B

Bob Powell [C# MVP]

The answer to this depends on what you're really asking.

You could for example simply format the string in the manner:
string newvar=string.Format("This is number {0}",iResult);
This provides the format you suggest.

If you really need the strings to be formatted differently for each return
value you could return a string from an array or hash table.
string newvar=myHashTable[iResult];
 
M

Michelle

Bob,
If you really need the strings to be formatted differently for each return
value you could return a string from an array or hash table.
string newvar=myHashTable[iResult];

In my example all the strings are nearly equal, except the number.
But actually they're all different.

So instead of using 'newVar = number_1', I'm curious if it's possible to use
' "number_" + iResult ' and act like the variable number_1
The string must be interpreted as variable and not as a string. I'm looking
for an alternative using 'select case'.


Regards,

Michelle
 
P

Peter Duniho

In my example all the strings are nearly equal, except the number.
But actually they're all different.

Okay. So simply using a format string isn't going to work.
So instead of using 'newVar = number_1', I'm curious if it's possible to
use
' "number_" + iResult ' and act like the variable number_1
The string must be interpreted as variable and not as a string.

Why? Why are these named constants in the first place? Why must you
refer to them by the name of the constant rather than by some other means?
I'm looking for an alternative using 'select case'.

What's wrong with "select" and "case"?

It seems to me that Bob offered a very good solution. Simply use the
numeric value as an index into a collection. If the indices are "dense"
-- that is, they vary strictly from a minimum number to a maximum number
with no or very few gaps -- then just use an array and index the array
with your number. If the indices are "sparse" -- that is, lots of gaps
within the range of valid values -- then a hash-based structure such as
Dictionary<int, string> would be more appropriate.

But even that begs the question of why you feel you need to avoid the
"select" statement. The compiler will essentially convert a select
statement to an array or dictionary lookup when that's beneficial anyway,
and some people find that a more readable way to represent the association
(I don't in most situations, but that's just me).

There are actually lots of other possibilities as well, and worse comes to
worse you could even write code that _literally_ generated the name of a
constant and then used that name to look up a string from within the
assembly. But you aren't going to get the advice you need unless you can
explain the specific need better.

Tell us what you _really_ want to do, rather than just the particular
solution you've already decided on.

Pete
 
P

Patrice

Or an array. What Bob means is that given the index of the message you can
retrieve whatever string you wish from a data structure...

string[] number=new string[]{"A","B","C"};
for(int i=0;i<number.Length;i++) Debug.WriteLine(number);

--
Patrice



Michelle said:
Bob,
If you really need the strings to be formatted differently for each
return
value you could return a string from an array or hash table.
string newvar=myHashTable[iResult];

In my example all the strings are nearly equal, except the number.
But actually they're all different.

So instead of using 'newVar = number_1', I'm curious if it's possible to
use ' "number_" + iResult ' and act like the variable number_1
The string must be interpreted as variable and not as a string. I'm
looking for an alternative using 'select case'.


Regards,

Michelle
 
M

Michelle

Peter,

[ . . .]
But even that begs the question of why you feel you need to avoid the
"select" statement.

There is no need to avoid that. It's just curiosity.
(a fool can ask more than ten wise men can answer )
Tell us what you _really_ want to do, rather than just the particular
solution you've already decided on.

It's not that difficult or mysterious.
I've a return value (e.g. 1) that has a meaning. (e.g. "This is the meaning
of number 1")
So the only thing I've to do is use the string "This is the meaning of
number 1" instead of using "1", because "1" says not much to the user.
I'd decided nothing so far. But after reading this there're two options.
Using the "switch" statement (previously erroneously named "select statement
") or creating a array (the numbers are "dense" )

Michelle
 
J

Julia M

How about using a kind of dictionary list ?

e.g.

The "Dictionary":

public class MyFriendlyClass {
public int MyMeaninglessIndex {get;set;}
public string MyFriendlyText {get;set;}
public MyFriendlyClass (int index, string text){
MyMeaninglessIndex =index;
MyFriendlyText =text;
}
}

Fill it:

public List<MyFriendlyClass> MyFriendlyList=new List<MyFriendlyClass>
();
MyFriendlyList.Add(new MyFriendlyClass (1,"This is number 1") );
MyFriendlyList.Add(new MyFriendlyClass (2,"This is number 2") );

Use it:
int iResult=2;
Console.WriteLine( MyFriendlyList.Find
(entry=>entry.MyMeaninglessIndex==iResult).MyFriendlyText);
 
M

Michelle

string[] number=new string[]{"A","B","C"};
for(int i=0;i<number.Length;i++) Debug.WriteLine(number);


Works fine !
Much more compact then the "switch - Case" statement .

Warmest thanks,

Michelle
 
A

Arne Vajhøj

Michelle said:
I'm curious if this is posible.

I've some string variables declared as const.
public const string number_1 = "This is number 1";
public const string number_2 = "This is number 2";
[. . .]

Later in my code i've a int variable iResult containing the value '1'.
When i assign the variable number_1 to a new string variable newVar using:
string newVar = "number_" + iResult

newVar contains 'number_1'.

But is it possible to ensure that newVar contains 'This is number 1'.
(make the string act like a variable name)

String can not be used directly as variable names.

You can use reflection. Bad idea.

Or you can store the strings in a dictionay. Good idea.

See example code below.

Arne

============================

using System;
using System.Reflection;

namespace E
{
public class BadWay
{
public static readonly string number_1 = "This is number 1";
public static readonly string number_2 = "This is number 2";
public static void Main(string[] args)
{
int iResult = 1;
string newVar = "number_" + iResult;
string val = (string)typeof(BadWay).GetField(newVar,
BindingFlags.Static | BindingFlags.Public).GetValue(null);
Console.WriteLine(val);
Console.ReadKey();
}
}
}


using System;
using System.Collections.Generic;

namespace E
{
public class GoodWay
{
private static Dictionary<string, string> numbers;
static GoodWay()
{
numbers = new Dictionary<string, string>();
numbers.Add("number_1", "This is number 1");
numbers.Add("number_2", "This is number 2");
}
public static void Main(string[] args)
{
int iResult = 1;
string newVar = "number_" + iResult;
string val = numbers[newVar];
Console.WriteLine(val);
Console.ReadKey();
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top