Click here to Skip to main content
15,867,594 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
hi expert \
can we assign a string value to a number enter in text box without using any case,for,switch and case statement.


for example if user input 1 in text box it assign monday to it without using case , loop and switch statement

What I have tried:

can we assign a string value to a number enter in text box without using any case,for,switch and case statement.


for example if user input 1 in text box it assign monday to it without using case , loop and switch statement
Posted
Updated 3-Feb-17 18:26pm
Comments
Jochen Arndt 3-Feb-17 7:34am    
I don't know if I got you right but you can use a lookup table after verifying that the number (index) is within the supported range.
Pete O'Hanlon 3-Feb-17 9:00am    
This sounds suspiciously like homework to me.
Philippe Mori 3-Feb-17 10:09am    
Don't repeat the same thing twice. If instead of writing the same thing twice, you would provide some extra information, it would be more useful. Don't expect much help if you don't make effort in writing a good question. By the way, where is your source code? And an example won't hurt either.

Yes with Dictionary[^]
 
Share this answer
 
v2
For that, for your case, you will need to have an enum type, not integer type. Enum types can be used in such cases, where you can have visuals in the programming and then use their values and so on. For example, the following enum,
C#
enum Days {
   Monday = 1,
   Tuesday = 2,
   Wednesday = 3,
   Thursday = 4, 
   Friday = 5,
   Saturday = 6,
   Sunday = 7
}

Now, when you get the user input, you can simply cast it to this enum value, but here the casting from string to integer will be required,
C#
Console.WriteLine((Days) 1); // Monday

But the casting from string will not work, and will never work because you cannot cast from String to Days. In the case of fetching the value from TextBox, same case applies, you need to first convert the value to integer, by parsing it, then you need to convert it to the name and print it.

For an example, please see, Home | .NET Fiddle[^], you may also have a look at the following MSDN documentation, Enum.ToString Method (System)[^]
 
Share this answer
 
v2
Comments
ZurdoDev 3-Feb-17 10:36am    
There is also Enum.Parse() and can use Enum.IsDefined().
Just want to throw in a few more example. I like enum too if the set of element is constant, in our case Names of the days of the week. For performance wise, I think Dictionary is the candidate.

C#
IList<string> days = new List<string>()
{
    "", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"
};

var dayList1 = days.ElementAt(1); //Monday
var dayList2 = days.ElementAt(3); //Wednesday

Hashtable hashtable = new Hashtable();
hashtable[1] = "Monday";
hashtable[2] = "Tuesday";
hashtable[3] = "Wednesday";
hashtable[4] = "Thursday";
hashtable[5] = "Friday";
hashtable[6] = "Saturday";
hashtable[7] = "Sunday";

var dayHash1 = hashtable[1]; //Monday
var dayHash2 = hashtable[6]; //Saturday
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900