how to re initiate a object when condition comes true in a member method?
for example
Class AClass
{
public AClass (int a)
{...}
public void DoIt()
{
...
c = PreDo();
...
}
private int PreDo()
{ ...
if (b==1)
xxxxx
...
}
}
int Main开发者_如何学运维 ()
{
AClass myClass = new AClass(2);
myClass.DoIt(); //*
}
* Here I wanna once xxxx is called then myClass is no long useful.
Hence it would try out myClass = new AClass(3), myClass.DoIt()
in the main
instead, and so on.
accross all instances or only in one instance? ... just use a (static) variable notRunAllready
to remember the called (so if (b==1)
goes to if (!notRunAllready && b==1)
) and you set notRunAllready
to true
in xxxx
May be I understood what you are talking about. You probably want to make conditional execution based on some class internal variable wich initialized in class's constructor.
Declare them like a
static
like @CKoening says. But it's wired by we, cause it's not absolutely clear what will gonna happen during the call.The best solution to make conditional execution is add parameters to your
DoIt(..)
method, by making esplicit "what is gonna be happen if I pass this parameter to the function".
If this is not what you're asking about, please explain better.
精彩评论