Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Create Instance Of Class Through Method Call Only?
Message
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Divers
Thread ID:
01464302
Message ID:
01464353
Vues:
44
The error I get is Error 1 The type 'System.Data.SqlClient.SqlDataReader' has no constructors defined


>>>>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
If it's not broken, fix it until it is.


My Blog
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform