[英]C# Error: Cannot implicitly convert type 'int' to 'short'
I am learning C# and am using Visual Studio 2010 to build a payroll program. 我正在学习C#,并且正在使用Visual Studio 2010构建薪资程序。 I get an error "Cannot implicitly convert type 'int' to 'short'".
我收到一个错误“无法将类型'int'隐式转换为'short'”。
My code is : 我的代码是:
private void btn_add_Click(object sender, EventArgs e)
{
try
{
// Get service instance
var employerPeriodService = Program.Kernel.Get<IEmployerPeriodService>();
// Get new code
var newCode = employerPeriodService.GenerateSAPCode();
// Create object
var employerPeriodAdd =
new EmployerPeriod
{
Code = newCode,
Name = newCode,
U_Tax_year = int.Parse(cb_tax_year.Text),
//U_Day_hrs = cb_number_hours.Text,
//U_Week_days = cb_number_days_week.Text,
//U_Week_hrs = txt_number_hours_week.Text,
//U_Month_days = cb_number_days_month.Text,
//U_Month_hrs = txt_number_hours_month.Text,
//U_Fortnight_days = cb_number_days_fortnight.Text,
//U_Fortnight_hrs = txt_number_hours_fortnight.Text,
//U_Weeks_in_month = cb_avg_weeks_month.Text,
//U_No_of_Fortnights = cb_avg_fortnights_month.Text,
U_Starting_period = Convert.ToDateTime(dateTimePicker1.Text),
U_Ending_period = Convert.ToDateTime(dateTimePicker2.Text)
//U_Comments = txt_comments.Text
};
// Save record
employerPeriodService.AddEmployerPeriod(employerPeriodAdd);
MessageBox.Show("Employer Payroll Period Added Successfully. Intake Ref: " + newCode.ToString(), "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
catch (RulesException ex)
{
MessageBox.Show(ex.GetErrorMessages(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
In Microsoft SQL Server where my database is, U_Tax_year is defined as smallint. 在我的数据库所在的Microsoft SQL Server中,U_Tax_year被定义为smallint。 What do I need to do to solve this error.
我需要怎么做才能解决此错误。 Do I convert what I get from the combo box 'cb_tax_year.Text' to int and how do I achieve this.
我是否将从组合框“ cb_tax_year.Text”中得到的内容转换为int以及如何实现?
Any help appreciated. 任何帮助表示赞赏。
I suspect it's as simple as changing this: 我怀疑这很简单:
U_Tax_year = int.Parse(cb_tax_year.Text),
to this: 对此:
U_Tax_year = short.Parse(cb_tax_year.Text),
Basically you should check the type of the U_Tax_year
property and make sure you parse the text appropriately so that the result of the RHS of the = sign is assignment-compatible with the property type. 基本上,您应该检查
U_Tax_year
属性的类型,并确保正确解析文本,以便=符号的RHS结果与属性类型兼容。
使用short.Parse而不是int.Parse
SQLServer's smallint
is the equivalent of c#'s short
. SQLServer的
smallint
等效于c#的short
。 so try short.Parse()
instead of int.Parse()
所以尝试用
short.Parse()
代替int.Parse()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.