Click here to Skip to main content
15,888,351 members
Home / Discussions / C#
   

C#

 
QuestionDisposing a custom class Pin
Vodstok25-Aug-08 11:02
Vodstok25-Aug-08 11:02 
AnswerRe: Disposing a custom class Pin
Colin Angus Mackay25-Aug-08 11:07
Colin Angus Mackay25-Aug-08 11:07 
GeneralRe: Disposing a custom class Pin
Vodstok25-Aug-08 11:36
Vodstok25-Aug-08 11:36 
GeneralRe: Disposing a custom class Pin
Colin Angus Mackay25-Aug-08 11:55
Colin Angus Mackay25-Aug-08 11:55 
GeneralRe: Disposing a custom class Pin
Vodstok25-Aug-08 12:05
Vodstok25-Aug-08 12:05 
GeneralRe: Disposing a custom class Pin
Colin Angus Mackay25-Aug-08 12:35
Colin Angus Mackay25-Aug-08 12:35 
GeneralRe: Disposing a custom class Pin
Vodstok25-Aug-08 12:54
Vodstok25-Aug-08 12:54 
GeneralRe: Disposing a custom class Pin
Colin Angus Mackay25-Aug-08 13:40
Colin Angus Mackay25-Aug-08 13:40 
Vodstok wrote:
I use the method Category.Instance(1) to create an instance of an object, then go back and create Category.Instance(4), I now have 2 distinct objects that should not overlap in any way shape or form?


No, you still only have one object. I've annotated your code below:
public static Category Instance(int id)
{
    // Only create a new Category when there isn't one already
    if (cat == null)
        cat = new Category();

    return cat;
}

The logic prevents the creation of a new instance. cat is a static field, which means there will only be one regardless of how ever many instances of the class you create.

If you want to create new distinct objects through a method you may want to look at the Factory Pattern instead - the code above is part of the Singleton Pattern. (Are you familiar with design patterns? Or have you just muddled through and stumbled upon them without realising? - Both scenarios are good in different ways).

The Factory Pattern is a design pattern that permits a method (or class) to be responsible for the creation of objects. It can be a lot more flexible than using basic constructors.

The Singleton Patterns is a design pattern that constrains a class so that there will only ever be one instance of the class. In C# this is more commonly achieved by using a static class (i.e. the pattern is built in to the language). In other languages you can do this by controlling all instantiation through a single method (this is what you are doing in your code)


GeneralRe: Disposing a custom class Pin
Vodstok25-Aug-08 13:29
Vodstok25-Aug-08 13:29 
GeneralRe: Disposing a custom class Pin
Colin Angus Mackay25-Aug-08 13:41
Colin Angus Mackay25-Aug-08 13:41 
GeneralRe: Disposing a custom class Pin
Vodstok25-Aug-08 13:48
Vodstok25-Aug-08 13:48 
GeneralRe: Disposing a custom class Pin
Colin Angus Mackay25-Aug-08 13:56
Colin Angus Mackay25-Aug-08 13:56 
GeneralRe: Disposing a custom class Pin
Vodstok25-Aug-08 14:13
Vodstok25-Aug-08 14:13 
QuestionCan an object cast itself to a derived class? Pin
Togakangaroo25-Aug-08 10:32
Togakangaroo25-Aug-08 10:32 
AnswerRe: Can an object cast itself to a derived class? Pin
Colin Angus Mackay25-Aug-08 10:48
Colin Angus Mackay25-Aug-08 10:48 
GeneralRe: Can an object cast itself to a derived class? Pin
Togakangaroo25-Aug-08 11:00
Togakangaroo25-Aug-08 11:00 
AnswerRe: Can an object cast itself to a derived class? Pin
chaiguy133725-Aug-08 14:50
chaiguy133725-Aug-08 14:50 
QuestionHow can I add a button here ? Pin
Mohammad Dayyan25-Aug-08 10:01
Mohammad Dayyan25-Aug-08 10:01 
AnswerRe: How can I add a button here ? Pin
DaveyM6925-Aug-08 10:19
professionalDaveyM6925-Aug-08 10:19 
AnswerRe: How can I add a button here ? Pin
Thomas Stockwell26-Aug-08 2:16
professionalThomas Stockwell26-Aug-08 2:16 
QuestionPassing array to operator overload Pin
DaveyM6925-Aug-08 9:48
professionalDaveyM6925-Aug-08 9:48 
AnswerRe: Passing array to operator overload Pin
DaveyM6925-Aug-08 10:21
professionalDaveyM6925-Aug-08 10:21 
GeneralRe: Passing array to operator overload Pin
chaiguy133725-Aug-08 14:53
chaiguy133725-Aug-08 14:53 
QuestionEnum / Cast Question Pin
Kevin Marois25-Aug-08 7:56
professionalKevin Marois25-Aug-08 7:56 
AnswerRe: Enum / Cast Question Pin
User 665825-Aug-08 8:12
User 665825-Aug-08 8:12 

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.