How will u make a method not create instance of any class..
see i have two class in the namespace in the second class i want a method which will not allow to create a instance..
you have give some signature to that mehod .. i want that signature.. can any one tell me..
thanks ®ards
udu
A staic method doesn't create an instance of the class it resides within. Is this what you want. For instance:
public static MembershipUser GetUser() { }
class myclass { public static void print() { myclass class1 = new myclass(); Response.WriteLine("Print The MEthod"); } }
.. print method accept the creation of object
class myclass { public static void print() { myclass class1 = new myclass(); Response.WriteLine("Print The MEthod"); } }
.. print method accept the creation of object
it possible make print method to throw an error while creating any instnace like class1.........
The only thing I could think of would be to throw an erro within your default constructor like so:
public Class1(){throw new Exception("Sorry, you can instantiate this class.");}1class Test {2protectedTest() {3 }4 }Pay attention to line 2. Constructor is marked as protected. That means it could be called only by Test class descendants. If you want to deny instatiation of class with any outer code mark the constructor as private. But in this case you have to wrote some another function that creatse instance of the class, e.g:
class Test {private Test() { }public static Test Create(){return new Test(); } }class Program {static void Main(string[] args) { Test test = Test.Create(); } }}if the function is is within test class then that fucntion should not allow to create instance of test class.. how can i make that signature of the function...
ecbruck:
The only thing I could think of would be to throw an erro within your default constructor like so:
public Class1(){throw new Exception("Sorry, you can instantiate this class.");}
i should only change the signature of function which is going to throw an error while instanciating the class
0 comments:
Post a Comment