c# 多次尝试捕获文本框值
c# multiple try and catch for textbox values
我正在尝试将 try 和 catch 分组为 6 个文本框值。在 class 中我描述了类似的 6 种方法:
public void Setpajamos(int newValue)
{
if (newValue >= 0 && newValue <= 30)
pajamos = newValue;
else
throw new Exception("Patikrinkite duomenis");
}
在主窗体中,我尝试捕获代码:
try
{
BustoKreditas.Setvaikusk(newvaikusk);
BustoKreditas.Setpajamos(newpajamos);
BustoKreditas.Setisipareigojimai(newisipareigojimai);
BustoKreditas.SetPaskolosSuma(newpaskolosSuma);
BustoKreditas.Setlaikotarpis(newlaikotarpis);
BustoKreditas.Setpastatoamzius(newpastatoamzius);
}
catch
{
MessageBox.Show("value to big");
}
问题是 try and catch 仅适用于第一个文本框。对于所有其他人,我可以输入我想要的任何数字,它不会显示任何消息。
我假设您正在尝试将所有错误集中在一起。
(在您的代码中,第一个异常将导致代码跳转到您的 catch
,因此不会调用任何其他方法。)
在这种情况下,您可以尝试这样的操作:
public void Setpajamos(int newValue, List<string> errors)
{
if (newValue >= 0 && newValue <= 30)
{
pajamos = newValue;
}
else
{
errors.Add("Patikrinkite duomenis");
}
}
在你的主要形式中:
var errors = new List<string>()
BustoKreditas.Setvaikusk(newvaikusk, errors);
BustoKreditas.Setpajamos(newpajamos, errors);
BustoKreditas.Setisipareigojimai(newisipareigojimai, errors);
BustoKreditas.SetPaskolosSuma(newpaskolosSuma, errors);
BustoKreditas.Setlaikotarpis(newlaikotarpis, errors);
BustoKreditas.Setpastatoamzius(newpastatoamzius, errors);
if (errors.Count > 0)
{
MessageBox.Show(string.Join(Environment.NewLine, errors));
}
我正在尝试将 try 和 catch 分组为 6 个文本框值。在 class 中我描述了类似的 6 种方法:
public void Setpajamos(int newValue)
{
if (newValue >= 0 && newValue <= 30)
pajamos = newValue;
else
throw new Exception("Patikrinkite duomenis");
}
在主窗体中,我尝试捕获代码:
try
{
BustoKreditas.Setvaikusk(newvaikusk);
BustoKreditas.Setpajamos(newpajamos);
BustoKreditas.Setisipareigojimai(newisipareigojimai);
BustoKreditas.SetPaskolosSuma(newpaskolosSuma);
BustoKreditas.Setlaikotarpis(newlaikotarpis);
BustoKreditas.Setpastatoamzius(newpastatoamzius);
}
catch
{
MessageBox.Show("value to big");
}
问题是 try and catch 仅适用于第一个文本框。对于所有其他人,我可以输入我想要的任何数字,它不会显示任何消息。
我假设您正在尝试将所有错误集中在一起。
(在您的代码中,第一个异常将导致代码跳转到您的 catch
,因此不会调用任何其他方法。)
在这种情况下,您可以尝试这样的操作:
public void Setpajamos(int newValue, List<string> errors)
{
if (newValue >= 0 && newValue <= 30)
{
pajamos = newValue;
}
else
{
errors.Add("Patikrinkite duomenis");
}
}
在你的主要形式中:
var errors = new List<string>()
BustoKreditas.Setvaikusk(newvaikusk, errors);
BustoKreditas.Setpajamos(newpajamos, errors);
BustoKreditas.Setisipareigojimai(newisipareigojimai, errors);
BustoKreditas.SetPaskolosSuma(newpaskolosSuma, errors);
BustoKreditas.Setlaikotarpis(newlaikotarpis, errors);
BustoKreditas.Setpastatoamzius(newpastatoamzius, errors);
if (errors.Count > 0)
{
MessageBox.Show(string.Join(Environment.NewLine, errors));
}