Raymond Osterbrink
Raymond Osterbrink

Reputation: 540

How to create an Object with multiple conditions

I'd like to create a class-object which has several states, from which only one can be active at each time. So for example:

Class Plane has three conditions: flying, driving, standing

I like to adress the conditions like this:

Plane boeing747 = new Plane;

boeing747.State = flying

or

boeing747.State.flying = true;

if(boeing747.State.flying == true); 
    console.writeline("it flyes");
else if(boeing747.State.driving == true)
    console.writeline("it drives");
else
    console.writeline("nothing goes");
// console reads: "it flyes"

boeing747.State.driving = true;
if(boeing747.State.flying == true); 
    console.writeline("it flyes");
else if(boeing747.State.driving == true)
    console.writeline("it drives");
else
    console.writeline("nothing goes");
// console reads: "it drives"

Upvotes: 0

Views: 119

Answers (1)

C.Evenhuis
C.Evenhuis

Reputation: 26446

You could use an enumeration for this:

public enum PlaneState
{
    Flying,
    Driving,
    Standing,
}

and in your class:

public PlaneState State
{
    get; 
    set; 
}

Upvotes: 6

Related Questions