简体   繁体   English

输入字符串的格式不正确。 将字符串转换为int

[英]Input string was not in a correct format. Converting string to int

Input string was not in correct form. 输入字符串格式不正确。 I'm getting an exception on runtime as "System.FormatException". 我在运行时获得了一个异常“System.FormatException”。

Follwing lines shows exception- Follwing线显示异常 -

public int Task 
{
    get
    { 
        return Int32.Parse(TaskText.Text);
    }
    set
    { 
        TaskText.Text = value.ToString(); 
    }
}

public int Project 
{
    get
    { 
        return Int32.Parse(ProjectText.Text);
    }
    set
    { 
        ProjectText.Text = value.ToString(); 
    }
}

I also tried - 我也尝试过 -

Convert.ToInt32(TaskText.Text)
Convert.ToInt32(ProjectText.Text)

I need to pass these to following constructor, 我需要将这些传递给以下构造函数,

Harvest_TimeSheetEntry entry = new Harvest_TimeSheetEntry(client,starttime,stoptime,task,project);

this constructor is stored in some class with task and project as integer parameters. 此构造函数存储在某个类中,其中task和project作为整数参数。 And I can't change it because if i changed, it affects other code. 我无法改变它,因为如果我改变了,它会影响其他代码。

It looks as though you're getting your input from controls accepting user input, which is just asking for failure, since a user can potentially enter something that doesn't represent an integer value. 看起来好像是从接受用户输入的控件获取输入,这只是要求失败,因为用户可能会输入不表示整数值的内容。 You can use TryParse to avoid this: 您可以使用TryParse来避免这种情况:

var result = 0;
if (int.TryParse(TaskText.Text, out result)) {
  return result;
}
return 0;

So, if the value of TaskText.Text == "1" , this will succeed; 所以,如果TaskText.Text == "1"的值,这将成功; if the value of TaskText.Text == "aaaa" , this will fail - and return zero. 如果TaskText.Text == "aaaa"的值,这将失败 - 并返回零。 You example would raise the appropriate exception, as experienced. 您的示例会像经验那样引发相应的异常。

However, an exception might be the right thing to happen here, if you can't handle a bad value, don't have an alternative, and the application relies on the input to move forward. 但是,异常可能是正确的事情,如果您无法处理错误的值,没有替代方案,并且应用程序依赖于输入来继续前进。 More likely, you could do with some validation on your input fields to prevent bad data being submitted. 更有可能的是,您可以对输入字段进行一些验证,以防止提交错误数据。

Since your Harvest_TimeSheetEntry constructor expects task and project to be integers, you must have a list of integers that correspond to the different tasks and projects. 由于您的Harvest_TimeSheetEntry构造函数需要taskproject为整数,因此您必须具有与不同任务和项目对应的整数列表。 Now you can't expect Int32 to know which task corresponds to which number, can you? 现在你不能指望Int32知道哪个任务对应哪个号码,可以吗?

I would suggest you use ComboBoxes for TaskText and ProjectText . 我建议你使用ComboBoxes for TaskTextProjectText Then, you can assign the correct corresponding integer to each ComboBoxItem.Tag . 然后,您可以为每个ComboBoxItem.Tag分配正确的相应整数。

Please note that this goes far beyond the kind of answers you should expect from SO. 请注意,这远远超出了您应该从SO中得到的答案。

if you do not use MVVM or binding you can simply do the check before your need it. 如果您不使用MVVM或绑定,您可以在需要之前进行检查。 t Ť

 int task;
 int project;

 if(!Int32.TryParse(TaskText.Text, out task))
 {}       //errorhandling here

 if(!Int32.TryParse(ProjectText.Text, out project))
 {}//errorhandling here

 //all fine
 var entry = new Harvest_TimeSheetEntry(client,starttime,stoptime,task,project);

You must check if you can parse it into Integer 您必须检查是否可以将其解析为Integer

try 尝试

Int32 foo =0;
if (Int32.TryParse(TaskText.Text, out foo))
{
    return foo;
}

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

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