Scopo
Scopo

Reputation: 301

Alternative to operand && (C#)

so I'm trying to make a code with &&. However, when I put that in, it said that I couldn't apply the operand to it.

In specific, it reads: Operator "&&" cannot be applied to operands of type 'Server.Enums.PokemonType' and 'Server.Enums.PokemonType'

However, I need to be able to link these two things so I can make the code be two PokemonTypes. So is there any alternative or work-around for not being able to use &&?

If you need it, this is my code:

case 225: 
{
    //Flying Press -- INCOMPLETE, needs Flying-type
    setup.Move.Element = Enums.PokemonType.Fighting && setup.Move.Element = Enums.PokemonType.Flying;
    if (setup.Defender.VolatileStatus.GetStatus("Minimize") != null) 
    {
        setup.Multiplier *= 2;
        setup.Move.Accuracy = -1;
    }
}
break;

Upvotes: 0

Views: 638

Answers (2)

BrunoLM
BrunoLM

Reputation: 100331

To add multiple values to a enum variable you need to declare the enum with [Flags] attribute.

So your enum would be:

[Flags]
public enum PokemonType
{
    Fighting = 1 << 0,
    Flying = 1 << 1,
    Normal = 1 << 2,
    Dragon = 1 << 3,
}

Define enumeration constants in powers of two, that is, 1, 2, 4, 8, and so on. This means the individual flags in combined enumeration constants do not overlap

Then assign using Enums.PokemonType.Fighting | Enums.PokemonType.Flying so it is possible to track all values assigned to it later.

Upvotes: 3

Servy
Servy

Reputation: 203820

The system defined && operator only supports boolean operands. The & operator will work for enums (because it also works on all integer types, which the enums are based on). Of course, if you want an enum that represents the combination of two flag values then you'll want to OR them together (using |), not AND them together.

Upvotes: 4

Related Questions