RG-3
RG-3

Reputation: 6188

Defining User defined 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?

Upvotes: 5

Views: 1569

Answers (4)

Jonathan Pitre
Jonathan Pitre

Reputation: 2425

Try:

Guid user = new Guid("dddddddddddddddddddddddddddddddd");

Hope this helps!
N.S.

Upvotes: 2

Rion Williams
Rion Williams

Reputation: 76547

You want to use Guid.Parse for this:

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

Upvotes: 4

Jon Skeet
Jon Skeet

Reputation: 1499770

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.

Upvotes: 11

Patrick Kafka
Patrick Kafka

Reputation: 9892

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();

Upvotes: 5

Related Questions