我们可以将字符串分配给整数 [英] Can we assign a string to integer number

查看:69
本文介绍了我们可以将字符串分配给整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨专家\

我们可以将字符串值分配给文本框中输入的数字,而不使用任何case,for,switch和case语句。





例如,如果用户在文本框中输入1,则在不使用大小写,循环和切换语句的情况下为其分配星期一



我尝试了什么:



我们可以将字符串值分配给在文本框中输入的数字而不使用任何大小写,因为,开关和案例陈述。





例如,如果用户在文本框中输入1,则在不使用案例,循环和切换的情况下为其分配星期一声明

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

推荐答案

是的,词典 [ ^ ]


为此,对于您的情况,您将需要有一个枚举类型,而不是整数类型。在这种情况下可以使用枚举类型,您可以在编程中使用视觉效果,然后使用它们的值等。例如,以下枚举,

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,
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,

Console.WriteLine((Days) 1); // Monday



但是来自字符串将无法工作,并且永远不会工作,因为您无法从String转换为Days。在从TextBox获取值的情况下,同样的情况适用,您需要首先将值转换为整数,通过解析它,然后您需要将其转换为名称并打印它。



例如,请参阅 .NET小提琴 [ ^ ],你也可以看看遵循MSDN文档, Enum.ToString Method(System) [< a href =https://msdn.microsoft.com/en-us/library/16c1xs4z(v=vs.110).aspx\"target =_ blanktitle =New Window> ^ ]


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)[^]


只想再举几个例子。如果元素集是常量的话,我也喜欢枚举,在我们的例子中是星期几的名字。为了表现明智,我认为词典是候选人。



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.

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


这篇关于我们可以将字符串分配给整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆