D
D
Danil2021-12-16 12:22:34
C++ / C#
Danil, 2021-12-16 12:22:34

How can you assign an ordinary string to an enum variable?

i need something like this

Programm m_CurentProgramm;//перечисление enum

string set = comboBox1.SelectedText; // например Sity
m_CurentProgramm = Programm.set; // мне нужно собрать значение  Programm.Sity

Is there a way to do this or is it not possible in C#?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vasily Bannikov, 2021-12-16
@Deletron247

enum SomeEnum {
  A,
  B
}
var text = "B";
var enumValue = Enum.Parse<SomeEnum>(text); // SomeEnum.B

PS: there is no such method in .net standard < 2.1 and .net framework, but there is
var enumValue = (SomeEnum) Enum.Parse(typeof(SomeEnum), text);

F
freeExec, 2021-12-16
@freeExec

Enum.Parse(SelectedText)
Or get enum strings - Enum.GetNamesand choose the right one yourself.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question