Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / December 2005

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Guy - 23 Dec 2005 19:41 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?
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]
>  }
> }

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.