Doubt when declaring enum with string [duplicate]

2

Declare an enum type string, like this:

public enum SEXO
{
    M = "Masculino",
    F = "Feminino"
}

How do I get an enum similar to the one above. This way give error:

  

Can not implicitly convert type 'string' to 'int'

    
asked by anonymous 12.05.2016 / 14:22

1 answer

3

You can not, at least not directly from the way you're trying to ask the question.

You can achieve a similar effect using extensions , see example

public static string GetStringValue(this Enum value)
{
    var type = value.GetType();

    var fieldInfo = type.GetField(value.ToString());

    var attributes = fieldInfo.GetCustomAttributes(
        typeof(StringValueAttribute), false) as StringValueAttribute[];

    var stringvalue = attributes != null && attributes.Length > 0 ? attributes[0].StringValue : value.ToString();
    return stringvalue;
}

public class StringValueAttribute : Attribute
{
    public string StringValue { get; protected set; }

    public StringValueAttribute(string value)
    {
        StringValue = value;
    }
} 

enum declaration

public enum Sexo
{
    [StringValue("Masculino")]
    M = 1,    
    [StringValue("Feminino")]
    F = 2
}

Use

string descrEnum = Sexo.M.GetStringValue();

I vaguely remembered that this code had come out of here from SOpt, now I hardly found in this answer .

    
12.05.2016 / 14:28
How to put Month and Year only in DataPicker (JavaFX) ___ ___ erkimt Show error message when entering phone ______ qstntxt ___

I have a form and I need this field

%pre%

In case it is the phone, it will display the following error messages: If the user types less than 8 digits, an error message appears, a %code% .

If the user types "-" which is an invalid character, an alert with = %code%

And if the number is valid (8 digits) nothing appears.

My code:

%pre%

It's not working.

The reference in the HTML to the javascript has to be by onKeyup if possible, as the javascript has to check after the user types.

Sincerely,

Gabriel

    
______ azszpr127718 ___

I suggest using the validation supported by the browser itself. This only works if the browser supports HTML5

%pre%

To use something more complete or different you can continue to use the onchange event with this automatic validation:

%pre%
    
______ azszpr127721 ___

You can do this:

%pre% %pre%

Remembering that it is more practical to limit size by html using maxLength, and using onblur prevents it from showing the message at all times when typing.

    
______ azszpr127729 ___

To complement, I'll leave a more complete version of @GabrielRodrigues' answer.

I made using the jQuery Mask Plugin , I believe it increases the usability of the field since it already comes formatted, in addition it it also "prohibits" any letters or any non-digit characters from being typed.

%pre% %pre%
    
______ azszpr127738 ___

I got it! I used the answer from @Bruno Costa and Js from @Gabriel Rodrigues. It just looks like this:

%pre%     
___