Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I have a class I think I'd like to setup as a singleton since I never want another object to be created for it. (That is, assuming I'm understand what a "singleton" is, which I think means a one and only instance of the class that can be created as a second object?) I've read about, but I'm not understanding how to go about it. For simplicity, say this is my class...

 

 

Public Class clsPrinter

   Public Enum Status
       Printing
       Waiting
       Unknown
   End Enum

   Public Function Printing(oDocument As Object)
       ' Code to print object
   End Function

End Class

 

 

How do I change this to a singleton class of just "Printer"? Or does it actually create an object called "Printer"? Like I said, the singleton thing is confusing to me. Any help would be very appreciated.

Posted

Public Class Printer

Private Shared _instance As Printer

 

Public Shared ReadOnly Property Instance() As Printer

Get

 

If _instance Is Nothing Then

_instance = New Printer

End If

 

Return _instance

End Get

End Property

 

Public Enum Status As Integer

Printing = 0

Waiting

Unknown

End Enum

 

Public Function Printing(ByVal oDocument As Object) As Boolean

' Code to print object

Return False

End Function

 

End Class

 

 

 

clsPrinter? eww. M$ recommends leaving off prefixes these days, since you can easily find out the type during debug by mousing over.

Posted (edited)

public class Printer
{
   static Printer instance = new Printer();

   Printer() { ... }

   public static Printer Instance { get { return instance; } }
}

 

The keys are a private constructor and a static property.

 

In .NET 2.0 you could also use a static class.

 

BTW: MS also recommends not using underscores. :p

Edited by IngisKahn
"Who is John Galt?"
Posted

I created Diesel's example. So now from some calling program, I typed "Printer." and the intellisense didn't show the Printing method, but did show the word "Instance". Only after selecting Instance and then hitting "." did the Intellisense show "Printing". So I have to use the word "Instance" every time, rather than just able to type "Printer"? That seems cumbersome. But is it the only way?

 

Also, I noticed , I can still declare any old object as a "Printer" type. I thought singleton classes guaranteed it to be a "one and only" kind of class/object hybrid? Or am I incorrect?

Posted
Declaring a private constructor would ensure that you can not create any additional Printer objects. You can still declare a variable of the type Printer but the only way to use it is to set it to the object returned by the static Printer.Instance property. You can use this method to have easy access to Printer's members. Your other option would be to use a static class.
"Who is John Galt?"
Posted

According to Microsoft P&P:

class Example
{
   int someMember;

   public int SomeMember { get { ... } set { ... } }

   public Example(int someMember)
   {
       // only in the constructor do you need to use 'this.'
       this.someMember = someMember;
   }
}

 

Of course this won't work in VB and even though MS doesn't target P&P for VB the consensus is to prefix with 'm' or 'my' sans underscore.

"Who is John Galt?"
Posted

or

class Example
{
 static int _someMember;

 static public int SomeMember 
 { 
   get 
   { 
     return _someMember;
   } 
   set 
   {
     _someMember = value;
   } 
 }
}

 

Usage:

 

Example.SomeMember = 0;

Joe Mamma

Amendment 4: The right of the people to be secure in their persons, houses, papers, and effects, against unreasonable searches and seizures, shall not be violated, and no warrants shall issue, but upon probable cause, supported by oath or affirmation, and particularly describing the place to be searched, and the persons or things to be seized.

Amendment 9: The enumeration in the Constitution, of certain rights, shall not be construed to deny or disparage others retained by the people.

Posted
Ya, but the whole point is that you're advised not to use underscores in names (esp. as a prefix) due to readability/flow issues. Then again, if you're the only person working on the code then you can do whatever you want privately so long as you follow P&P for the public interface.
"Who is John Galt?"
Posted
If you look at the (private) fields of frameworks classes, such as the ArrayList, they have an underscore. I personally like IngisKahn's version better (less typing), but apparently ms is still into the '_'.
"For every complex problem, there is a solution that is simple, neat, and wrong." - H. L. Mencken
Posted
you're advised not to use underscores in names (esp. as a prefix)
huh??? who advises that? internals are suggested to be either prefixed as _variable, or mVariable or m_Variable.

 

the "_" implies its attachment to current class.

Joe Mamma

Amendment 4: The right of the people to be secure in their persons, houses, papers, and effects, against unreasonable searches and seizures, shall not be violated, and no warrants shall issue, but upon probable cause, supported by oath or affirmation, and particularly describing the place to be searched, and the persons or things to be seized.

Amendment 9: The enumeration in the Constitution, of certain rights, shall not be construed to deny or disparage others retained by the people.

Posted
Hmm, that's a bit disturbing. More so since the guideline originated from an internal MS memo. Perhaps it was implemented before the edict was handed down. :(
"Who is John Galt?"
Posted

Joe Mamma

Amendment 4: The right of the people to be secure in their persons, houses, papers, and effects, against unreasonable searches and seizures, shall not be violated, and no warrants shall issue, but upon probable cause, supported by oath or affirmation, and particularly describing the place to be searched, and the persons or things to be seized.

Amendment 9: The enumeration in the Constitution, of certain rights, shall not be construed to deny or disparage others retained by the people.

Posted

It was posted by a few people on blogs.msdn.com, but a quick web search didn't find it...

 

The recommendation is that no variable name decoration should be used (Hungarian or underscore). Any slight benefit gained from decorations is superceded by IDE functionality. (Especially in VS2005; it�s like night and day compared to VS2003)

"Who is John Galt?"
  • Administrators
Posted

I've seen several different recomendations from various MS sources (samples, blogs etc.) as a rule I would stick to the MS naming conventions for any externally visible members (Public or Protected) and not worry quite so much about exactly how internal naming conventions fit in as long as they are applied consistently and improve readability.

I personally do tend to use the _ prefix for member variables.

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted

:) If Plausibly uses it, it's good enough for me.

 

Besides...

 

private int someMember;

 

public int SomeMember { get; set; }

 

isn't CLS Compliant.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...