简体   繁体   English

将字符串转换为短字符串

[英]Converting a String to a Short

Current code:当前代码:

short s;

s = short.Parse(this.txtFields3.Text);

I've gone through with debugging, and can confirm that the txtField3.Text returns an actual value from the form.我已经完成了调试,并且可以确认 txtField3.Text 从表单返回一个实际值。

Also tried:也试过:

s = short.Parse(this.txtFields3.Text, CultureInfo.InvariantCulture);

and,和,

s = Convert.toInt16(this.textFields3.Text);    

EDIT: The value of the variable I'm trying to put into 's' here is "EMS".编辑:我试图放入“s”的变量的值是“EMS”。

and the value is something that fits into a short?并且价值是适合短片的东西?

How about:怎么样:

short s;
if (!short.TryParse(this.txtFields3.Text, out s)){
    s = 0;
}

"EMS" is not a short, so the code will always fail. “EMS”不是一个缩写,所以代码总是会失败。

Are you sure you understand what you are trying to do?你确定你明白你在做什么吗? Give us what you really need to do, not what you think you want to do and you will surely be helped.给我们你真正需要做的,而不是你认为你想做的,你一定会得到帮助。

Update更新

A short is a data type that represents a number. short 是一种表示数字的数据类型。 This is why "EMS" is not a short.这就是为什么“EMS”不是简称的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM