简体   繁体   中英

Is there a better way to check for valid values?

I have a property in my class that can only be one of several values, what is the best way to limit the input on this property.

Here is what I'm doing now, and I'm sure there must be a better way.

public void SetValue(int value)
{
    if(value != 1 || 
        value != 4 || 
        value != 8 || 
        value != 16 || 
        value != 32 || 
        value != 64, || 
        value != 128)
    {
        property_value = 1;
    }
    else
    {
        property_value = value;
    }
}

Instead of in int , use an Enum with these values.

I am sure each value has a specific meaning - expose these as enum members.

This may not eliminate all issues (since an Enum is simply a wrapper over an integer type and can still get assigned a value that doesn't exist in the enumeration), but should take care of most problems, so long as you are consistent about only passing values from the enumeration itself.

In any rate, you can then simply test the passed in value against the enumeration and throw an exception if it isn't a valid member.

Use enum instead of this numeric values like:

enum Numbers { Name1 = 1, Name2 = 4 ... }

and then you can easilly check if value is one of enum element:

Enum.IsDefined(typeof(Numbers), value );

For your example, you can just do:

property_value = 1;

since your if condition will always be true.

If you want to restrict it to a number of possibilities you could:

Declare an enum:

public enum Value
{
    Default = 1,
    Option1 = 4,
    ...
}

or have a collection of valid values to check:

int[] validValues = new int[] { 1, 4, 8, 16, 32, 64, 128 };
property_value = validValues.Contains(value) ? value : 1;

Although I would prefer to throw an exception on invalid input.

I think you should consider using an enum :

public enum MyEnum
{
    These,
    Are,
    Valid,
    Values
}


public void SetValue(MyEnum _value)
{
   // Only MyEnum values allowed here!
}
if(((value & (value − 1)) == 0) && value != 2 && value <= 128)
  property_value = 1;
else
  property_value = value;

(value & (value − 1)) is a fast way to check if value is a power of two.

As an example: value = 4:

(4(10) & (3(10)) =
100(2) & 011(2) =
000(2) = 0

value = 5

(5(10) & (4(10)) =
101(2) & 100(2) =
100(2) =
4

You could use an enum and check using Enum.IsDefined(value) . But then you'd have to think of a (meaningfull) name for all the possible values.

I think we're missing the INTENT of the function here.

It looks like a bit mask check to me. If that's the case, he's missing 2 from the code sample. Also, note that he's not discarding a value if it isn't one of those specific bits: he preserves it. If it is a value equal to a specific bit (and only that bit) he coerces it to 1.

I think the sample provided by Lee works best in this case; it's simple and to the point. Also, if the check is widened to account for 16 bits (or even 32), it will easily catch them all.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM