-4

I need a unique ID each time I create a new product.

My code creates same Guid (00000000-0000-0000-0000-000000000000) each item I add it to the list, and its not unique.

Can you tell me if I'm doing something wrong in this code please. Here is my code.

class Product
{

    private Guid _ID ;
    private String _Name = string.Empty;
    private String _Price = string.Empty;

    public Guid ID
    {
        get { return _ID; }
        set { _ID = value; }
    }


    public string Name
    {
        get { return (_Name); }
        set { _Name = value; }
    }

    public string Price
    {
        get { return (_Price); }
        set { _Price = value; }
    }


    public Product(Guid ID, string Name, string Price)
   {
   _ID = ID;
   _Name = Name;
   _Price = Price;
   }

    public Product()
    {
        // TODO: Complete member initialization
    }


public static void Main()

{

    List<Product> productList = new List<Product>();
    productList.Add(new Product() { Name = "shoe", Price = "80"});
    productList.Add(new Product() { Name = "Hand Bag", Price = "180" });
    productList.Add(new Product() { Name = "Pen", Price = "8" });

    var cheapestItem = (from i in productList orderby i.Price descending select i.Name).Take(1);
    Console.WriteLine(cheapestItem);
    Console.ReadLine();

 }    
Liam
  • 27,717
  • 28
  • 128
  • 190
Diana
  • 283
  • 1
  • 7
  • 13
  • 3
    It is a default value of a `GUID`. This is `Guid.Empty`. From it's [documentation](http://msdn.microsoft.com/en-us/library/system.guid.aspx); _A read-only instance of the Guid structure whose value is all zeros._ – Soner Gönül May 15 '14 at 13:01
  • where you initialize new GUID? – Hassan May 15 '14 at 13:03
  • 1
    Why all the down votes? The topic of the question is worthy of a novice, so what? The question is clearly presented, so +1. – phoog May 15 '14 at 13:05
  • It was already answered in your own questions some months ago: http://stackoverflow.com/questions/21433670/auto-generate-unique-id-within-the-constructor – sergiogarciadev May 15 '14 at 13:05
  • Note that since `Guid` is a `struct`, it should be expected that `new Guid()` gives a zero value. This will be true in general for any struct. – Dave Cousineau Apr 11 '17 at 18:07

3 Answers3

15

You need to call Guid.NewGuid() to create a new unique GUID every time.

The default value of Guid is Guid.Empty, which is what you're seeing (all 0's).

Joe
  • 1,214
  • 3
  • 15
  • 33
3

You are not passing the Guid to your class constructor OR through object initializer.

Do:

 productList.Add(new Product() { ID = Guid.NewGuid(), Name = "shoe", Price = "80"});

Fields are assigned default value in the class, 00000000-0000-0000-0000-000000000000 is the default value for Guid.

Since you have a class constructor that takes three parameters including Guid, you can use that like:

productList.Add(new Product(Guid.NewGuid, "shoe", "80"));
Habib
  • 219,104
  • 29
  • 407
  • 436
  • Thank you for your answer. Can you show me how to add Guid to my class constructor please? – Diana May 15 '14 at 13:12
  • You have an overloaded constructor for taking Guid, Name and Price, use that, I have updated the answer with the code. – Habib May 15 '14 at 13:15
2

You didn't even generate a Guid, so it gets the default value every time.Use Guid.NewGuid() method to generate a Guid.

Selman Genç
  • 100,147
  • 13
  • 119
  • 184