tags:

views:

1564

answers:

3

Code

Something = new Guid()

is returning

00000000-0000-0000-0000-000000000000

all the time and I can't tell why? so, why?

+17  A: 

You should use Guid.NewGuid()

Will Dean
I can't use it. I don't know why. I have included System namespace but i don't have it.
Ante B.
@Ante: If you have the Guid class then you have the NewGuid method.
Guffa
Well, for some reason i don't. Do u now what could be the problem?
Ante B.
Something = Guid.NewGuid() works.
Dustin Campbell
+13  A: 

Just a quick explanation for why you need to call NewGuid as opposed to using the default constructor... In .NET all structures (value types like int, decimal, Guid, DateTime, etc) must have a default parameterless constructor that initializes all of the fields to their default value. In the case of Guid, the bytes that make up the Guid are all zero. Rather than making a special case for Guid or making it a class, they use the NewGuid method to generate a new "random" Guid.

Josh Einstein
Relevant MSDN link: http://msdn.microsoft.com/en-us/library/83fhsxwc(VS.80).aspx
Jason
+4  A: 

It's in System.Guid.

To dynamically create a GUID in code:

Guid messageId = System.Guid.NewGuid();

To see its value:

string x = messageId.ToString();
DOK
Yes, it should be there (http://msdn.microsoft.com/en-us/library/system.guid_members.aspx) but I can't use it. Why?
Ante B.
"I can't use it" - what happens when you try to use it? Type it out manually (perhaps there's an issue with your intellisense) and try to compile - do you get compilation errors?
Matthew Brindley
What do you mean when you say you "can't use it"? If you type Guid x = System.Guid.NewGuid() and compile, do you get an error? Or do you not like the value you're getting for x?
DOK
I restared my machine and now it works fine.
Ante B.