Click here to Skip to main content
15,887,267 members
Home / Discussions / C#
   

C#

 
QuestionRe: Returning Instance Issue - Object Reference Not Set Pin
Eddy Vluggen16-Jul-12 23:30
professionalEddy Vluggen16-Jul-12 23:30 
AnswerRe: Returning Instance Issue - Object Reference Not Set Pin
AmbiguousName16-Jul-12 23:37
AmbiguousName16-Jul-12 23:37 
AnswerRe: Returning Instance Issue - Object Reference Not Set Pin
Eddy Vluggen16-Jul-12 23:52
professionalEddy Vluggen16-Jul-12 23:52 
AnswerRe: Returning Instance Issue - Object Reference Not Set Pin
DaveyM6917-Jul-12 1:46
professionalDaveyM6917-Jul-12 1:46 
AnswerRe: Returning Instance Issue - Object Reference Not Set Pin
AmbiguousName17-Jul-12 20:02
AmbiguousName17-Jul-12 20:02 
GeneralRe: Returning Instance Issue - Object Reference Not Set Pin
BobJanova17-Jul-12 4:01
BobJanova17-Jul-12 4:01 
GeneralRe: Returning Instance Issue - Object Reference Not Set Pin
DaveyM6917-Jul-12 10:31
professionalDaveyM6917-Jul-12 10:31 
GeneralRe: Returning Instance Issue - Object Reference Not Set Pin
BobJanova18-Jul-12 2:26
BobJanova18-Jul-12 2:26 
Sure. Access to a true singleton is via a static method (the getter of an Instance property in C#, but it's still a method). By definition, you can only get one instance, so there's only one available area for state storage, and the type of that instance is known at instantiation time.

The only one of those things that is not directly repeatable by a static class is the last, if you have some sort of factory that returns an instance for a singleton based on something else. Even then, you can delegate the methods which have actually changed from static methods. And such a method would normally be indicative that you should be using dependency management instead of manually coded singletons, anyway.

Let's look at a simple example, in pseudo:

singleton class MyClass {
 public static singleton Instance { create { return new MyClass() } };
 
 private int nextID = 100;
 public int NextValue() { return nextID++; }
}


This can be directly replaced with

static class MyClass {
 private static int nextID = 100; 
 public static int NextValue() { return nextID++; }
}


... that is, by making all methods and state static, and removing the singleton code. This also means that you aren't wasting memory for an object instance that is there simply to wrap methods and application-wide (i.e. static) state.
GeneralRe: Returning Instance Issue - Object Reference Not Set Pin
DaveyM6918-Jul-12 4:52
professionalDaveyM6918-Jul-12 4:52 
GeneralRe: Returning Instance Issue - Object Reference Not Set Pin
BobJanova18-Jul-12 6:31
BobJanova18-Jul-12 6:31 
GeneralRe: Returning Instance Issue - Object Reference Not Set Pin
BobJanova17-Jul-12 4:05
BobJanova17-Jul-12 4:05 
GeneralRe: Returning Instance Issue - Object Reference Not Set Pin
Matt T Heffron17-Jul-12 8:07
professionalMatt T Heffron17-Jul-12 8:07 
GeneralRe: Returning Instance Issue - Object Reference Not Set Pin
BobJanova18-Jul-12 2:19
BobJanova18-Jul-12 2:19 
GeneralRe: Returning Instance Issue - Object Reference Not Set Pin
Matt T Heffron24-Jul-12 8:04
professionalMatt T Heffron24-Jul-12 8:04 
QuestionHow to find API's to generate barcodes in C# Pin
Akshay_8816-Jul-12 21:10
Akshay_8816-Jul-12 21:10 
AnswerRe: How to find API's to generate barcodes in C# Pin
Richard MacCutchan16-Jul-12 21:31
mveRichard MacCutchan16-Jul-12 21:31 
AnswerRe: How to find API's to generate barcodes in C# Pin
Peter_in_278016-Jul-12 21:36
professionalPeter_in_278016-Jul-12 21:36 
GeneralRe: How to find API's to generate barcodes in C# Pin
Akshay_8816-Jul-12 23:37
Akshay_8816-Jul-12 23:37 
GeneralRe: How to find API's to generate barcodes in C# Pin
Peter_in_278017-Jul-12 1:22
professionalPeter_in_278017-Jul-12 1:22 
GeneralRe: How to find API's to generate barcodes in C# Pin
Akshay_8817-Jul-12 1:29
Akshay_8817-Jul-12 1:29 
GeneralRe: How to find API's to generate barcodes in C# Pin
Dave Kreskowiak17-Jul-12 1:33
mveDave Kreskowiak17-Jul-12 1:33 
GeneralRe: How to find API's to generate barcodes in C# Pin
Akshay_8817-Jul-12 1:41
Akshay_8817-Jul-12 1:41 
GeneralRe: How to find API's to generate barcodes in C# Pin
Dave Kreskowiak17-Jul-12 2:11
mveDave Kreskowiak17-Jul-12 2:11 
GeneralRe: How to find API's to generate barcodes in C# Pin
Akshay_8817-Jul-12 2:19
Akshay_8817-Jul-12 2:19 
QuestionRe: How to find API's to generate barcodes in C# Pin
Richard MacCutchan17-Jul-12 2:44
mveRichard MacCutchan17-Jul-12 2:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.