Exit out of Class

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

What is the code to exit (break) out of a class?

I have a class, with an if statement, if is true then exit class, if false
continue with the code in the class.
 
Mike L said:
What is the code to exit (break) out of a class?

I have a class, with an if statement, if is true then exit class, if false
continue with the code in the class.

A bit of terminology first - a class doesn't have an "if" statement, a
method does.

However, it sounds like you're basically after a return statement:

if (whatever)
{
return;
}

Note that if the method has a non-void return type, you'll have to
specify which value you want to return.

Alternatively (depending on the situation) you may want to throw an
exception instead. Is your condition an error condition of some kind,
or is it normal behaviour?
 
I do not quite follow what you mean by exit the class... perhaps exit the
method or function? Something like:

void MyFunc()
{
if( something )
{
// Do something
}
else
{
//Do something else and exit function
}

//Continue doing stuff
}

If so, you can simply use the return keyword to return from the function
(and pass back a reference if the function type requires a return value)), so
the above would look like:

void MyFunc()
{
if( something )
{
// Do something
}
else
{
//Do something else
return;
}

//Continue doing stuff
}

Brendan
 
Hi Cadel,

Does your statement of "Exit out of Class" mean exit the calling function?
Does the community's replies make sense to you? Please feel free to tell
us. Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Back
Top