471,603 Members | 1,439 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,603 software developers and data experts.

Creating a new Value Type for hiding creation of Guid's

Guy
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()
{
???
}
}
Dec 23 '05 #1
2 2698
Guy <Gu*@discussions.microsoft.com> wrote:
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);
}
}

--
Jon Skeet - <sk***@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
Dec 23 '05 #2
Hi,

Why not using directly a Guid?
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Guy" <Gu*@discussions.microsoft.com> wrote in message
news:E8**********************************@microsof t.com...
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()
{
???
}
}

Dec 27 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Steve | last post: by
12 posts views Thread by Mats Lycken | last post: by
3 posts views Thread by sheldonlg | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by MichaelMortimer | last post: by
reply views Thread by CCCYYYY | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.