locked
Will this class create ID RRS feed

  • Question

  • User1989204087 posted

    I may ask something stupid, but I need some help.  Say, I have this class:

    public class PointOfInterest {  

    public int Id { get; set;}

     public string Name { get; set; }  

    public string Description { get; set; }  

    public string Address { get; set; }

     public double? Latitude { get; set; }  

    public double? Longitude { get; set; }

    }

    I create obj base on this class:

    PointOfInterest newPOI = new PointOfInterest ();

    newPOI.Name = "New POI"; newPOI.Description = "test for creating a new POI"; newPOI.Address = "100 Main Street\n Anywhere, Nyc 75069";

    Question:

    1.  After creating this obj, how to get the Id ?  will it be null? if it is not null, when this Id gets its value from?

    Thanks

    Thursday, July 16, 2015 12:07 AM

Answers

  • User177399542 posted

    Hi SkySky

    You are not assigning any value to Id. So Id will be null 0. You can access it like:

    Int myId=newPOI.Id;

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 16, 2015 12:48 AM
  • User197322208 posted

    it is not null - it is 0 ( default)

    public int Id { get; set;}

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 16, 2015 12:49 AM
  • User-434868552 posted

    @SkySky

    The answer depends on when.  You can test this in debug mode.

    public class PointOfInterest
    {  
        public int Id { get; set;}
        public string Name { get; set; }  
        public string Description { get; set; }  
        public string Address { get; set; }
        public double? Latitude { get; set; }  
        public double? Longitude { get; set; }
    }
    void Main()
    {
        PointOfInterest newPOI = new PointOfInterest ();
        Console.WriteLine (newPOI.Id);
    }

    output:

    0

    HOWEVER, for example, if your object is used to add a row to a database table, and Id is the index that is set by that action, the value of  Id will be the database key to that row.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 16, 2015 2:38 AM
  • User753101303 posted

    Hi,

    It seems to relate to something you saw with Entity Framework (please be always explicit about your context)?

    Client side the default value is 0, you'll "register" this object (and maybe others) to your EF context and at some point you'll save all changes. At this time, EF takes the values for those objects, send them to the db using the corresponding SQL statements and read back the server side generated identity value which is then used to populate this client side property.

    So you should have a value populated for you after the "SaveChanges" call (once agin my guess is that this is for use with Entity Framework?)

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 16, 2015 7:36 AM

All replies

  • User177399542 posted

    Hi SkySky

    You are not assigning any value to Id. So Id will be null 0. You can access it like:

    Int myId=newPOI.Id;

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 16, 2015 12:48 AM
  • User197322208 posted

    it is not null - it is 0 ( default)

    public int Id { get; set;}

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 16, 2015 12:49 AM
  • User-434868552 posted

    @SkySky

    The answer depends on when.  You can test this in debug mode.

    public class PointOfInterest
    {  
        public int Id { get; set;}
        public string Name { get; set; }  
        public string Description { get; set; }  
        public string Address { get; set; }
        public double? Latitude { get; set; }  
        public double? Longitude { get; set; }
    }
    void Main()
    {
        PointOfInterest newPOI = new PointOfInterest ();
        Console.WriteLine (newPOI.Id);
    }

    output:

    0

    HOWEVER, for example, if your object is used to add a row to a database table, and Id is the index that is set by that action, the value of  Id will be the database key to that row.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 16, 2015 2:38 AM
  • User753101303 posted

    Hi,

    It seems to relate to something you saw with Entity Framework (please be always explicit about your context)?

    Client side the default value is 0, you'll "register" this object (and maybe others) to your EF context and at some point you'll save all changes. At this time, EF takes the values for those objects, send them to the db using the corresponding SQL statements and read back the server side generated identity value which is then used to populate this client side property.

    So you should have a value populated for you after the "SaveChanges" call (once agin my guess is that this is for use with Entity Framework?)

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 16, 2015 7:36 AM
  • User1989204087 posted

    Thank u all,

    I was not sure when I look at the code how the Id get its value when the object is created without insert a value for the id.  With u all help, I finally understand

    the value for the Id was created somewhere.

    Thanks

    Thursday, July 16, 2015 8:49 AM