Max Yankov
Max Yankov

Reputation: 13327

Passing enum in params

How can I pass an enum as one of the parameters of the method? I only need in some implementations of the method, and not in others. That's how method declaration looks:

public abstract void SomeMethod(params object[] someParams);

And that's how I would like some of the implementations to look:

public override void SomeMethod(params object[] someParams) {

    SomeEnum someEnum = someParams[0];
    if (someEnum == null)
        return;

}

This code, of course, is invalid, but it shows what I intend to do. Other implementations of the method may use other enums, or use no enums at all.

Update: As many have stated, it's a very strange thing to do, throwin type-safety out of the window. So, I decided to explain why I'm doing this.

I have to pass method calls to a lot of various subclasses of a class where I declare this method through a system that sends them across a network (as well as locally) and executes them at specific time in a special order. Is there a better way to do this?

Upvotes: 1

Views: 5392

Answers (4)

nawfal
nawfal

Reputation: 73283

You can pass enum values of a type using the params keyword just as any other object.

public override void SomeMethod(params SomeEnum[] someEnums) 
{
     foreach(SomeEnum e in someEnums)
     {

     }
}

In case you gotta pass other enums as well to this method, you could use System.Enum as parameter since all enums derive from it. Slightly stronger typing.

public override void SomeMethod(params Enum[] enums) 
{
    //something like
    var @enum = enums[0];
    if (!(@enum is SomeEnum))
        return;

    var someEnum = (SomeEnum)@enum;
}

As to acknowledge your edit, the best way is to specify the objects you are going to pass during method call explicitly as the signature of your method. Only if the number of parameters you will pass have to be variable, you should rely on params keyword. Otherwise, you should stick to explicitly declaring the signature. If your enum SomeEnum is optional, you can declare it at last as an optional parameter. Like this:

public override void SomeMethod(int a, string b, SomeEnum? someEnum = null) 
{
    if (someEnum == null)
       //handle the logic here
}

Upvotes: 4

phoog
phoog

Reputation: 43056

I think normally you'd use overloads for different implementations of the method, not overrides, if they require different parameters. These could still have params at the end:

void M(SomeEnum someEnum, params object[] someParams)
{
   //...

Your code could work with this signature, too, however:

public override void SomeMethod(params object[] someParams) {

    if (someParams.Length == 0 or !(someParams[0] is SomeEnum))
        return;

    SomeEnum someEnum = (SomeEnum)someParams[0];
    //...

Upvotes: 1

Paul Ruane
Paul Ruane

Reputation: 38620

public override void SomeMethod(params object[] someParams)
{
    if (!(someParms[0] is SomeEnum))
        return;

    var se = (SomeEnum) someParams[0];
    ...
}

Though quite why you wouldd want to do this over an explicitly typed parameter I cannot imagine.

Upvotes: 4

Jon Skeet
Jon Skeet

Reputation: 1503290

Well it looks like you're really asking for trouble here, basically removing any sort of strong typing from the signature. You'll need to cast.

Secondly, a value of type SomeEnum can never be null, because enums are value types. So you could write:

public override void SomeMethod(params object[] someParams)
{
    if (someParams[0] == null)
    {
        return;
    }
    // This will throw an exception if you've been given the wrong kind
    // of argument. I prefer that to just silently returning, as it would
    // usually indicate a programming error which should be fixed.
    SomeEnum someEnum = (SomeEnum) someParams[0];
    ...    
}

However, you'd be better off working out a way of representing the parameters in a more strongly typed way. Unfortunately, we can't really help you do that without more information.

Upvotes: 2

Related Questions