Reputation: 11972
I'm trying to create a class with a constructor:
class MyClass
{
public int VAR1;
public int VAR2;
public MyClass(int var1, int var2)
{
this.VAR1=var1;
this.VAR2=var2;
}
public int DoMath()
{
return this.VAR1+this.VAR2;
}
}
Ok, that example will probably work and once constructed the DoMath method will become available in the class instance.
What I would like to do is run some equations in the constructor, and depending on the outcome the DoMath method may or may not become available. So something like this:
class MyClass
{
public int VAR1;
public int VAR2;
public MyClass(int var1, int var2)
{
if(var1==var2) /*HERE IT CHECKS IF THE VARS ARE THE SAME*/
{ /*IF THEY'RE NOT THE SAME THEN THE DoMath METHOD IS UNAVAILABLE*/
this.VAR1=var1;
this.VAR2=var2;
}
}
public int DoMath()
{
return this.VAR1+this.VAR2;
}
}
Obviously this is only an example, but another way to explain would be this:
I need a class called "Process" where the constructor takes a process id as an argument, the constructor will need to check that this process actually exists before giving access to all the methods in the class.
Any one know how this is possible?
Upvotes: 0
Views: 535
Reputation: 19897
Without knowing the rest of your situation I can't say if this is a good idea or not, but what you could do is use inheritance:
class MyClass
{
public int VAR1;
public int VAR2;
private MyClass()
{
}
public static MyClass MakeInstance(int var1, int var2)
{
if(var1==var2)
{
return new MySubClass1(var1, var2);
}
else
{
return new MySubClass2();
}
}
}
class MySubClass1 : MyClass
{
public MySubClass1(int var1, int var2)
{
this.VAR1=var1;
this.VAR2=var2;
}
public int DoMath()
{
return this.VAR1+this.VAR2;
}
}
class MySubClass2 : MyClass
{
public MySubClass2()
{
}
//Same as MySubClass1 except no DoMath()
}
Then to check if DoMath()
is available you just check the class type.
Upvotes: 0
Reputation: 144106
I would create a static factory method which returned null if the process with the given id did not exist:
public class Process
{
private Process(...)
{
}
public static Process GetById(int processId)
{
if(ProcessExists(processId))
{
//get process details
}
else return null;
}
}
Depending on the methods on this class, you may be able to return a null object instead of null but this sounds unlikely from your description.
Upvotes: 3
Reputation: 1356
You're best bet is to set a flag based on the outcome of your constructor checks on the input. Then throw an exception in the DoMath method if it shouldn't be available. You can't really take away the method at runtime since it is already compiled in.
Upvotes: 0
Reputation: 3951
Since the equality of the 2 parameters will only be known at run-time, you cannot do compile-time checks to hide methods in portions of your code. You could, however, throw an exception of DoMath()
is called at a bad time. This is the perfect usage of exceptions, since presumably the caller of DoMath()
should have known better.
Upvotes: 0
Reputation: 241585
This is not possible as you want it. What you can do is provide a property
private readonly bool canDoMath;
public bool CanDoMath { get { return this.canDoMath; } }
and set this.canDoMath
in the constructor as to whether or not you can do math. Then you should have DoMath
throw an InvalidOperationException
if this.canDoMath
is false but the method is invoked.
Upvotes: 3