none
error: cannot implicitly convert type 'string' to 'int?'

    Question

  • 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;
    Monday, September 13, 2010 1:37 PM

Answers

  • i'm not sure if this work: e.id=(Integer)t.id

    but i'm sure this work: e.id=Convert.ToInt32(t.id)

    BTW, ur t.id should be numeric, and not null

    Monday, September 13, 2010 1:46 PM
  • Also, you can use int.Parse or int.TryParse

    Regards

    Monday, September 13, 2010 2:27 PM
  • Hi anilnarayan,

     

    Excuse me, but can you tell me if the id field in Data.DAL.Baby is int type or string? This is important. If it is a string type then you just use the method from deadManN and Serguey123 to convert it, and after this you can use it like the code above.

     

    If there’s anything unclear, please feel free to let me know, and I’m glad to help you.

     

    Have a nice day!

    Mike

    -------------------------------------------------------------------

    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to the others community members reading the thread.

    • Marked as answer by cnjadhav Thursday, September 16, 2010 2:35 PM
    Wednesday, September 15, 2010 6:57 AM

All replies

  • i'm not sure if this work: e.id=(Integer)t.id

    but i'm sure this work: e.id=Convert.ToInt32(t.id)

    BTW, ur t.id should be numeric, and not null

    Monday, September 13, 2010 1:46 PM
  • Also, you can use int.Parse or int.TryParse

    Regards

    Monday, September 13, 2010 2:27 PM
  • Hi anilnarayan,

     

    Excuse me, but can you tell me if the id field in Data.DAL.Baby is int type or string? This is important. If it is a string type then you just use the method from deadManN and Serguey123 to convert it, and after this you can use it like the code above.

     

    If there’s anything unclear, please feel free to let me know, and I’m glad to help you.

     

    Have a nice day!

    Mike

    -------------------------------------------------------------------

    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to the others community members reading the thread.

    • Marked as answer by cnjadhav Thursday, September 16, 2010 2:35 PM
    Wednesday, September 15, 2010 6:57 AM