Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Defining User defined Guid

Tags:

c#

.net

guid

I want to define User Define Guid in C#. I want to insert this in my Guid object:

dddddddddddddddddddddddddddddddd.

When I do this:

Guid user = "dddddddddddddddddddddddddddddddd";

I get the err: System cannot convert from String to System.Guid. What should I do here?

like image 610
RG-3 Avatar asked Aug 05 '11 16:08

RG-3


4 Answers

It sounds like you want:

Guid user = Guid.Parse("dddddddddddddddddddddddddddddddd");

Note that when you print the guid out again, it will be formatted differently:

// Prints dddddddd-dddd-dddd-dddd-dddddddddddd
Console.WriteLine(user);

You could call the Guid(string) constructor instead, but personally I prefer calling the Parse method - it's more descriptive of what's going on, and it follows the same convention as int.Parse etc. On the other hand, Guid.Parse was only introduced in .NET 4 - if you're on an older version of .NET, you'll need to use the constructor. I believe there are some differences in terms of which values will be accepted by the different calls, but I don't know the details.

like image 174
Jon Skeet Avatar answered Sep 25 '22 08:09

Jon Skeet


a GUID must be 32 characters formated properly and it would also be called like this

Guid user = new Guid("aa4e075f-3504-4aab-9b06-9a4104a91cf0");

you could also have one generated

Guid user = Guid.NewGuid();
like image 35
Patrick Kafka Avatar answered Sep 24 '22 08:09

Patrick Kafka


You want to use Guid.Parse for this:

Guid user = Guid.Parse("dddddddddddddddddddddddddddddddd");
like image 25
Rion Williams Avatar answered Sep 24 '22 08:09

Rion Williams


Try:

Guid user = new Guid("dddddddddddddddddddddddddddddddd");

Hope this helps!
N.S.

like image 38
Jonathan Pitre Avatar answered Sep 25 '22 08:09

Jonathan Pitre