0

I have a DomainUpDown window. I need to read the value from DomainUpDown window and next check this in my if(). For example I give the code: How to do it properly?

string a;
if(DomainUpDown.Text == "Text1")
{
    a="0";
}
else
{
    a="1";
}
Caramiriel
  • 7,029
  • 3
  • 30
  • 50
  • Is this winforms? – FF- May 20 '19 at 17:45
  • Yes....................... – Jan Kowalski May 20 '19 at 17:47
  • What is wring with what you have...other than neither `"0"` nor `"1"` are *numbers*? – Ňɏssa Pøngjǣrdenlarp May 20 '19 at 17:54
  • 1
    https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/types/how-to-convert-a-string-to-a-number – Jim W May 20 '19 at 19:05
  • Without seeing any context we cant tell if there is a better way - but there is nothing wrong with your code if the requirement is `set the variable string a to the value "1" or "0" depending on the text property of the object DomainUpDown` – TomC May 21 '19 at 07:01
  • If you have more cases, you could use a dictionary with keys like DomainUpDown values and you'll read it like `string a = yourDictionary[DomainUpDown.Text]`. [More here](https://stackoverflow.com/questions/12169443/get-dictionary-value-by-key) – barbsan May 21 '19 at 07:10

1 Answers1

0

You can shorten it to this (I suppose your number should be an integer):

int myNumber = DomainUpDown.Text == "Text1" ? 0 : 1;
Rob
  • 11,492
  • 14
  • 59
  • 94