简体   繁体   English

在C#中将null分配给整数

[英]Assign null to integer in C#

I have a winforms application and i am trying to pass 5 parameters inside the tableadapter in order to execute a query but i get the error "Cannot convert from 'int?' 我有一个winforms应用程序,我试图在tableadapter内部传递5个参数以执行查询,但出现错误“无法从'int转换?” to 'int'. All of them can be nulls. 设为'int'。所有的都可以为null。

private int? c_id ;
private string c_cn;
private int? c_afm;
private string c_job;
private string c_city;

private void btnView_Click(object sender, EventArgs e) {
c_id = (txtBox_id.Text != string.Empty) ? c_id = Convert.ToInt32(txtBox_id.Text) : c_id = null;
c_afm = (txtBox_afm.Text != string.Empty) ? c_afm = Convert.ToInt32(txtBox_afm.Text) : c_afm = null;
c_cn = (txtBox_cn.Text != string.Empty) ? c_cn = txtBox_cn.Text : c_cn = null;
c_job = (txtBox_job.Text != string.Empty) ? c_job = txtBox_job.Text : c_job = null;
c_city = (txtBox_job.Text != string.Empty) ? c_city = txtBox_city.Text : c_city = null;

this.clientBrowserTableAdapter.Fill(erpDataSet.PelatesBrowser,c_id,c_cn,c_afm, c_job,c_city);
}

The problem is at c_id and c_afm parameters. 问题出在c_id和c_afm参数上。 Any ideas? 有任何想法吗?

You can create a dedicated method for converting string to int? 您可以创建将字符串转换为int?的专用方法int?

private int? ConvertToDbInt(string text)
{
    if (string.IsNullOrEmpty(text))
    {
        return null;
    }

    return int.Parse(text);
}

Then reuse it for different variables 然后将其重用于不同的变量

c_id = ConvertToDbInt(txtBox_id.Text);
c_afm = ConvertToDbInt(txtBox_afm.Text);

Your actual problem is because conditional operator ?: expects that both positive and negative expressions produce value of same type, where in your case it is int - for positive and int? 您的实际问题是因为条件运算符?:期望正和负表达式都产生相同类型的值,在您的情况下,它是int对于正和int? for negative. 否定的。

Either the type of first_expression and second_expression must be the same , or an implicit conversion must exist from one type to the other. first_expression和second_expression的类型必须相同 ,或者必须存在从一种类型到另一种类型的隐式转换。

If you big fan of conditional operator ?: then just make sure that both expression returns same type, int? 如果您是条件运算符?:忠实拥护者,则只需确保两个表达式都返回相同的类型,就可以了int? in your case. 在你的情况下。

c_id = (txtBox_id.Text != string.Empty) ? (int?)Convert.ToInt32(txtBox_id.Text) : null;

When exception thrown on line this.clientBrowserTableAdapter.Fill(... , that mean that Fill method expects parameter of type int , you can easily convert nullable int to int by calling .GetValueOrDefault() this.clientBrowserTableAdapter.Fill(...上引发异常时,这意味着Fill方法需要int类型的参数,您可以通过调用.GetValueOrDefault()轻松地将可空int转换为int

this.clientBrowserTableAdapter.Fill(
    erpDataSet.PelatesBrowser,
    c_id.GetValueOrDefault(),
    c_cn,
    c_afm.GetValueOrDefault(), 
    c_job,
    c_city);

clientBrowserTableAdapter.Fill is expecting an int . clientBrowserTableAdapter.Fill需要一个int You are passing a Nullable<int> , they are not the same and there is no implicit conversion happening. 您正在传递Nullable<int> ,它们不一样,并且没有隐式转换发生。 You either need to cast it to int ie (int)c_id or use .Value property of Nullable<T> . 您需要将其(int)c_idint(int)c_id或使用Nullable<T> .Value属性。 If c_id is null, then you will have to deal with that case otherwise you will get exception when casting. 如果c_id为null,那么您将不得不处理这种情况,否则在投射时会出现异常。

As a side note, your assignment is slightly off, you can just do : 附带一提,您的作业略有减少,您可以执行以下操作:

c_id = (txtBox_id.Text != string.Empty) ? Convert.ToInt32(txtBox_id.Text) : (int?)null;;

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

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