Convert string to short

Current Code:

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

I went through debugging and can confirm that txtField3.Text returns the 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 enter in the 's' here is "EMS".

+7
source share
2 answers

"EMS" is not short, so the code always fails.

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 they will certainly help you.

Update

Short is a data type that represents a number. That is why "EMS" is not short.

+5
source

and meaning is that that fits in short?

What about:

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

All Articles