I want to create a new value type for hiding the creation of Guid's that can
be used in my business classes. I suppose I have to achieve this by creating
a struct (ID)
The problem is I'm completely stuck as how to develop my struct. Anyone can
help?
Thanks.
e.g.
class Person
{
ID PersonID; // ID is the struct
string FirstName;
string LastName;
Person()
{
this.ID = ID.new(); // Calling the static method of the struct ID, with
the purpose of getting a Guid
...
}
...
}
struct ID
{
...
public static ??? new()
{
???
}
}
Jon Skeet [C# MVP] - 23 Dec 2005 20:27 GMT
> I want to create a new value type for hiding the creation of Guid's that can
> be used in my business classes. I suppose I have to achieve this by creating
> a struct (ID)
> The problem is I'm completely stuck as how to develop my struct. Anyone can
> help?
Well, you can't create a method called "new" in C# (you could use @new,
but it's really not a good idea).
Instead, you should create a factory method which calls a constructor
and does whatever is required. For instance:
using System;
struct ID
{
Guid guid;
public Guid Guid
{
get { return guid; }
}
public static ID CreateID()
{
ID id = new ID();
id.guid = Guid.NewGuid();
return id;
}
}
public class Test
{
static void Main()
{
ID id = ID.CreateID();
Console.WriteLine (id.Guid);
id = ID.CreateID();
Console.WriteLine (id.Guid);
}
}

Signature
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Ignacio Machin ( .NET/ C# MVP ) - 27 Dec 2005 15:36 GMT
Hi,
Why not using directly a Guid?

Signature
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
>I want to create a new value type for hiding the creation of Guid's that
>can
[quoted text clipped - 30 lines]
> }
> }