4

I have a database with an status field that can keep the following values:

  • 0 Registrado
  • 1 Activo
  • 2 Finalizado
  • 3 Anticipado
  • 4 Reestructurado

Of course I just keep the number in a tinyint datatype in my database. Then I need to query that table but the GUI must show string value and not numeric value.

What is the best way to achieve it? Should I use a Enum datatype or dictionary ? What would the advantages of using one over the other?

In addition, results will be shown in a datagrid

Davide Piras
  • 43,984
  • 10
  • 98
  • 147
Jorge Zapata
  • 2,316
  • 1
  • 30
  • 57

2 Answers2

3

The approach I would take is

  • Map the tinyint values into an enum
  • Create a static Dictionary<TheEnumType, string> which maps the values to the user string

For example

public enum Names {
  Registrado = 0,
  Activo = 1,
  Finalizado = 2,
  Anticipado = 3,
  Reestructurado = 4
}

Now you can easily convert between the DB value and the appropriate enum

int theDbValue = ...;
Names name = (Names)theDbValue;

Building up the mapping between the names can now be easily done with a Dictionary<Names, string>.

var map = new Dictionary<Names, string>();
map[Names.Registrado] = "...";
map[Names.Activo] = "...";
// etc ...
JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
2

If we are talking about a limited amount of choices i would suggest enums since performance wise they are a lot better. A couple of similar questions that could help

Community
  • 1
  • 1
Kypros
  • 2,997
  • 5
  • 21
  • 27