Input string incorrect format
I need help, I'm getting error: Input string had 开发者_运维问答an incorrect format. Thanks in advance.
FORM2.CS:
public void loadClient(object source, System.Timers.ElapsedEventArgs e)
{
Form1 f1 = new Form1();
client = Client.GetClients()[0];
short port = short.Parse(f1.returnTBOX4().Text);
client.Login.SetOT(f1.returnTBOX3().Text, port);
}
FORM1.CS:
public TextBox returnTBOX1()
{
return textBox1;
}
public TextBox returnTBOX2()
{
return textBox2;
}
public TextBox returnTBOX3()
{
return textBox3;
}
public TextBox returnTBOX4()
{
return textBox4;
}
Since you didn't tell us where the error was this might not be the right place:
change this:
short.Parse(f1.returnTBOX4().Text)
to this:
short my_val;
if(short.TryParse(f1.returnTBOX4().Text, out my_val)){
Do stuff
}
else{
log exception and display to use that information was in incorrect format.
}
This won't solve your problem with getting a bad value, but it will allow you to check the value and not have the parse method throw an exception.
This is probably because of mismatch between what you type and what is interpreted (current culture settings are in use). If you want to always provide this number in an invariant form, use following code:
using System.Globalization;
short.Parse(f1.returnTBOX4().Text, CultureInfo.InvariantCulture);
Form1 f1 = new Form1();
You are creating a new instance of the form. That form is not going to have anything entered in the textBox4 control, the Parse() method will of course complain about it. You have to use the existing instance of the form, the one that the user is looking at. Pass a reference to it through the Form2 constructor. Or use a property. Or use Application.OpenForms if you really have to.
精彩评论