Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.

C#
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;
Posted
Updated 13-Sep-10 4:02am
v2

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)


Regards,
Suresh
 
Share this answer
 
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, 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.
 
Share this answer
 
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,
 
Share this answer
 
Comments
cnjadhav 13-Sep-10 10:02am    
Hey Hiren,
the first one is my error, cannot implicitly convert type 'string' to 'int?'.

thanks
Chetan.
Hiren solanki 13-Sep-10 10:04am    
Check the value is null of id variable ?
cnjadhav 16-Sep-10 9:49am    
Reason for my vote of 5
Automatic vote of 5 for accepting answer.
Well, you can ensure that the id is really an int. However, if you just want a quick and dirty way to remove the error. Try this out!

this.id = int.Parse( e.id.ToString());


If it's already a String, the ToString() call should not matter.
 
Share this answer
 
Comments
Abhijit Jana 13-Sep-10 10:12am    
int.Parse() will throw an exception, if the input is not valid, like int.Parse("a12") will throw an exception. So, it always better to use TryParse(), which will throw bool based on seccess and failure.
Ruselo Riva Asentista 13-Sep-10 11:09am    
I think I should throw an exception because otherwise, you could get an object without an id but with a name, etc. This could be a security threat. It's always better to terminate gracefully than to continue executing out of control. That said, I also use TryParse but only when I have default values like quantities. I never use it on ids or any other navigation / identification properties.
Abhijit Jana 13-Sep-10 11:21am    
Yeah.
Member 14482462 21-Jun-19 10:04am    
Cannot implicitly convert type `string' to `int'
CapitalTooll Tools = new CapitalTooll();
var lastentry = db.CapitalToolls.OrderByDescending(c => c.ItemMasterNo).FirstOrDefault();
if (id != 0)
{
Tools = db.CapitalToolls.Where(x => x.ItemMasterNo == id).FirstOrDefault<capitaltooll>();
}
else if (lastentry == null)
{
Tools.ItemMasterNo = "CAP 0001";
}
else
{
Tools.ItemMasterNo = "CAP" + (Convert.ToInt32(lastentry.ItemMasterNo.ToString(9, lastentry.ItemMasterNo.Length - 9)) + 1).ToString(D3);
}
return View(Tools);

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900