Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Create Instance Of Class Through Method Call Only?
Message
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Miscellaneous
Thread ID:
01464302
Message ID:
01464349
Views:
41
>>>You don't need a Factory (at least for this bit):
public class SomeClass
>>> {
>>>     private SomeClass()
>>>      {
>>>      }
>>>      public static SomeClass GetInstance()
>>>      {
>>>        return new SomeClass();
>>>      }
>>> }
>>>
>>>//To instantiate:
>>>SomeClass sc = SomeClass.GetInstance();
>>>// This won't compile:
>>>SomeClass sc2 = new SomeClass();
>>
>>Then this defeats the whole purpose. I don't want the class to be created from outside my factory. I want to be able to set things
>>on the instance before returning it to the caller.
>
>Then I think your options are limited. Either
>(a) Create a CTOR for the class with parameters that accept the values you want to set
>or
>(b) Make the class private and hide it behind an interface.

If you think of something like a DataReader. You cannot directly instantiate this class. You need a Command class to return a data
reader. How is this done? Somehow they're only allowing the class to be created via the Command. There must be a way.

Could you elaborate on what you mean by " hide it behind an interface."?

Thanks
Everything makes sense in someone's mind
public class SystemCrasher :ICrashable
In addition, an integer field is not for irrational people
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform