How to go through an enum?

14

I need to do the following: Pass a string and go through it and take each letter found and add with its corresponding value, type: a = 1, s = 19 and so on.

Well, I made a enum with all values of string , starting with a = 1 to z = 26 (includes K, W, and Y). I'm having trouble getting the letter in for and accumulating its value in relation to enum .

public enum triaguloLetra
{
    a = 1,
    b = 2,
    c = 3,
    d = 4,
    e = 5,
    f = 6,
    g = 7,
    h = 8,
    i = 9,
    j = 10,
    k = 11,
    l = 12,
    m = 13,
    n = 14,
    o = 15,
    p = 16,
    q = 17,
    r = 18,
    s = 19,
    t = 20,
    u = 21,
    v = 22, 
    w = 23,
    x = 24,
    y = 25,
    z = 26
}
string teste = "Stackoverflow";
for (int i = 0; i <= teste.Length - 1; i++)
{
    //Como eu digo que teste[i] = ao enum.s?? e assim por diante
}
    
asked by anonymous 06.07.2015 / 12:34

6 answers

12

Instead of an enum use an Dictionary :

Dictionary<string, int> valorLetra = new Dictionary<string, int>();

valorLetra.Add("a",1);
.....
....
valorLetra.Add("z",26);

string teste = "Stackoverflow";
int soma = 0;
for (int i = 0; i <= teste.Length - 1; i++)
{
    string letra = teste[i].ToString().ToLower();
    soma = soma + valorLetra[letra];
}

However, in this case, you do not need enum or Dictionary :

string teste = "Stackoverflow";
int soma = 0;
for (int i = 0; i <= teste.Length - 1; i++)
{
    soma = soma + Char.ToLower(teste[i]) - 'a' + 1;
}

Using LINQ:

string teste = "Stackoverflow";
int soma = teste.Select(c => Char.ToLower(c) - 'a' + 1).Sum();
    
06.07.2015 / 12:53
6

One more solution:

foreach (var elemento in Enum.GetValues(typeof(TrianguloLetra))) {
    //faz o que você quiser aqui
}

Example:

using System;

public class Program {
    public static void Main() {
        var soma = 0;
        foreach (var elemento in Enum.GetValues(typeof(TrianguloLetra))) {
            soma += (int)elemento;
        }
        Console.WriteLine(soma);
    }
    public enum TrianguloLetra {
            a = 1,
            b = 2,
            c = 3,
            d = 4,
            e = 5,
            f = 6,
            g = 7,
            h = 8,
            i = 9,
            j = 10,
            k = 11,
            l = 12,
            m = 13,
            n = 14,
            o = 15,
            p = 16,
            q = 17,
            r = 18,
            s = 19,
            t = 20,
            u = 21,
            v = 22, 
            w = 23,
            x = 24,
            y = 25,
            z = 26
    }
}

See working on dotNetFiddle .

    
07.07.2015 / 05:28
5

Strictly using the points of your original question ( Lookup in a Enum ), your test should be as follows:

var valorEnum = (triaguloLetra)Enum.Parse(typeof(triaguloLetra), letra);

The Enum.Parse method performs a lookup on the collection of values of Enum , similar to the key lookup of a Dictionary , thus retaining the original structure of your question. p>     

06.07.2015 / 14:41
3

You can do this:

public enum triaguloLetra
{
    a = 'a', b = 'b', c = 'c', d = 'd', e = 'e', f = 'f', g = 'g', h = 'h', i = 'i', j = 'j', l = 'l', m = 'm', n = 'n', o = 'o', p = 'p', q = 'q', r = 'r', s = 's', t = 't', u = 'u', v = 'v', x = 'x', z = 'z'
}

var texto = "Stackoverflow";
foreach (var item in texto.ToLower())
{
    var charEnum = (triaguloLetra)item;
}
    
06.07.2015 / 12:59
3

Taking the advice of @ramaral, a more functional way of expressing code behavior would be:

var valores = new Dictionary<char, int>
{
    {'a', 1},
    {'b', 2}
};


int soma = teste.Select(c => Char.ToLower(c))
                .Select(c => valores[c])
                .Sum();

I've used it to improve the suggestion using a char instead of string dictionary, and using collection initializer .

    
06.07.2015 / 14:22
0

You can do this using LINQ

Func<string, int> Contador = 
    x => 
    x.Select(c => Char.ToLower(c) - 98).Where(c => c > 0 && c < 27).Sum();

Example on DotNetFiddle

    
08.07.2015 / 21:32