5

I have a code in C# (.NET 2.0), where I call a method with an input Enum, but I cannot manage to make it work.

I have a compile error in the method isAnOptionalBooleanValue:

public static bool isAnOptionalBooleanValue(Status status, String parameterName, Object parameter) 
{
    return isAnOptionalValidValue(status, parameterName, parameter, UtilConstants.BooleanValues);
}

public static bool isAnOptionalValidValue(Status status, String parameterName, Object parameter, Enum setOfValues)
{
     ....
}

In other class:

public class UtilConstants
{
    public enum BooleanValues
    {
        TRUE, FALSE
    }
}

This class exists because the boolean value comes as an input String from other system, so I pass it as an Object and translate it to a Boolean from my Enum class.

The error it returns is the following: "UtilConstants.BooleanValues' is a 'type', which is not valid in the given context" with the error in the return isAnOptionalValidValue(...) line.

But I don't see how to fix it. Changing it by:

return isAnOptionalValidValue(status, parameterName, parameter, typeof(UtilConstants.BooleanValues));

does not work either.

Any ideas? Thank you for your help in advance!

3
  • 6
    why are you redefining bool?! Commented Apr 2, 2013 at 13:13
  • I have no idea why you need that enum even if you have tried to explain it. Maybe it would help if you'd show what you will do with it in isAnOptionalValidValue. Commented Apr 2, 2013 at 13:15
  • 2
    @DanielA.White From the question it seems it is so he can create an optional boolean. If only there was framework support for that ;-) Commented Apr 2, 2013 at 13:16

4 Answers 4

7

Instead of UtilConstants.BooleanValues (which indeed is a type and not a value), you need to use actual values. Like this:

UtilConstants.BooleanValues.TRUE | UtilConstants.BooleanValues.FALSE

Or if you actually want to check if an input string matches a constant of an enum type, you should change the signature:

public static bool isAnOptionalValidValue(Status status, String parameterName,
                                          Object parameter, Type enumType)

That way your proposed method call using typeof would work.
You could then get the actual String values of your enum by calling Enum.GetValues(enumType)

Sign up to request clarification or add additional context in comments.

1 Comment

Using as argument (Type enumType) in the declaration and typeof when calling the method, works perfectly. So I can still use my method isAnOptionalValidValue for any kind of Enum, and also booleans. Thank you very much !
6

You may rather use

bool myBool;    
Boolean.TryParse("the string you get from the other system", out myBool);

instead of redefining bool.

You can also do it "optional" as nullable bool (so if no value of true, false it's null):

bool? myBool = null;
bool tmp;
if (Boolean.TryParse("the string you get from the other system", out tmp))
    myBool = tmp;       

1 Comment

Thank you. You are right about redefining Boolean. But in my case, I would like to use the method isAnOptionalValidValue, because its functionality applies to different values grouped in different Enums, and also should work for the FALSE and TRUE values. That is why I created an enum for them. Maybe it would not be necessary or a good idea to redefine Boolean, but I think that makes the method isAnOptionalValidValue valid for booleans also.
2

Enum setOfValues in the parameter list means that you passing specific value like BooleanValues.TRUE - not type itself. To pass type use Type setOfValues.

To parse specific enum value from string use

BooleanValues enumValue = (BooleanValues)Enum.Parse(typeof(UtilConstants.BooleanValues), stringValue);

1 Comment

Thanks for clarifying also. I did not realise that the Enum type meant the enum value itself, and not the type.
0

You mentioned that you get a string from the other system that you have to convert into a Boolean type. So instead of using an enum i would write my own converter for this purpose:

public static class SpecificBooleanConverter
{
    private static Dictionary<string, bool> _AllowedValues;

    static SpecificBooleanConverter()
    {
        _AllowedValues = new Dictionary<string, bool>(StringComparer.CurrentCultureIgnoreCase);
        _AllowedValues.Add("true", true);
        _AllowedValues.Add("false", false);
        _AllowedValues.Add("foo", true);
        _AllowedValues.Add("bar", false);
    }

    public static bool TryParse(string value, out bool result)
    {
        return _AllowedValues.TryGetValue(value, out result);
    }
}

Which could then be called in this way:

bool dotNetBoolean;
var booleansFromOtherSystem = new[] { "hello", "TRUE", "FalSE", "FoO", "bAr" };

foreach (var value in booleansFromOtherSystem)
{
    if (!SpecificBooleanConverter.TryParse(value, out dotNetBoolean))
    {
        Console.WriteLine("Could not parse \"" + booleansFromOtherSystem + "\".");
    }

    if (dotNetBoolean == true)
    {
        Console.WriteLine("Yes, we can.");
    }
    else
    {
        Console.WriteLine("No, you don't.");
    }
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.