简体   繁体   中英

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.

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".

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.

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. This is why "EMS" is not a short.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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