错误:无法将类型'string'隐式转换为'int?'

错误:无法将类型'string'隐式转换为'int?'

问题描述:

大家好,
我正在尝试构建以下类,因为无法在类this.id = e.id上得到错误,因为无法将类型``string''隐式转换为``int?''.

非常感谢,
车丹.

Hey guys,
I am trying yo build the following class for which i get an error on the line this.id = e.id as cannot implicitly convert type ''string'' to ''int?''.

many many Thanks,
Chetan.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Data.Linq;
using System.Text;
using System.Runtime.Serialization;
using HsInternet.Data.DAL;

namespace HsInternet.Data.BLL
{
    public class Baby
    {
        public int id { get; set; }
        public string name { get; set; }
        public string parents { get; set; }
        public int gender { get; set; }
        public string weight { get; set; }
        public string length { get; set; }
        public DateTime date { get; set; }
        public DateTime time { get; set; }
        public string image { get; set; }
        public string ipa { get; set; }
        public string ipb { get; set; }


        public Baby()
        {
            this.id = -1;
            this.name = string.Empty;
            this.parents = string.Empty;
            this.gender = -1;
            this.weight = string.Empty;
            this.length = string.Empty;
            this.date = DateTime.Now;
            this.time = DateTime.Now;
            this.image = string.Empty;
            this.ipa = string.Empty;
            this.ipb = string.Empty;
        }

        public Baby(Data.DAL.Baby e)
        {
            this.id = e.id;     //getting error on this line
            this.name = e.name;
            this.parents = e.parents;
            this.gender = e.gender;
            this.weight = e.weight;
            this.length = e.length;
            this.date = e.date;
            this.time = e.time;
            this.image = e.image;
            this.ipa = e.ipa;
            this.ipb = e.ipb;
        }

        public static void Parse(DAL.Baby e, Baby t)
        {
            e.id = t.id;



确保"e.id"是整数变量.
发生此错误的原因是,您试图将字符串值"e.id"分配给整数值"this.id".

如果您的"e.id"包含整数值,请尝试,

Hi,

Make sure that the "e.id" is an integer variable.
This error occurs because, you are trying to assign a string value "e.id" to integer value "this.id".

If your "e.id" contains integer value, then try,

this.id = Convert.ToInt32(e.id)



问候,
Suresh



Regards,
Suresh


您提到过,下面的行抛出错误.

You have mentioned, below line is throwing error.

public Baby(Data.DAL.Baby e)
{    this.id = e.id;     //getting error on this line



this.Id,表示当前对象,它是int.现在,Data.DAL.Baby.Id的类型是什么?是琴弦吗? e.id当前拥有什么价值?

放一个点并检查.让我们知道,如果它不能解决您的问题.



this.Id, represent the current object, which is int. Now what is the type of Data.DAL.Baby.Id ? Is it string ? and what value currenly hold by e.id ?

Put a brekpoint and check. Let us know,if it does not solve your issue.


请澄清您遇到的错误.

无法将类型"string"隐式转换为"int?".

无法将类型"string"隐式转换为"int".

因为第一个错误与Nullable Int有关.

和第二个错误似乎像数据转换问题.

谢谢
Please Clarify Which Error you are Getting.

cannot implicitly convert type ''string'' to ''int?''.
or
cannot implicitly convert type ''string'' to ''int''.

Because the First Error relates to Nullable Int.

and Second Error Seems Like Data Conversion problem.

Thanks,