HOW to Change the value of a global variable in local scope without overriding

int val; while (sqlReader.Read()) { if (textBox_reg_login.Text == sqlReader["login"].ToString()) { label_reg_login_message.Text = "Логин с таким значением уже существует"; val = 1; //Глобальная переменная переопределяется break; } } if (val == 1) //И здесь уже ошибка: переменной val не присвоено значение { } 
  • 2
    "And here is a mistake" - I beg! What ?! - Igor
  • one
    @Regent Sure. It was an educational comment, "so as not to alienate a new, inexperienced participant." I hope Nicholas will not see him and will not open a discussion on the inadmissibility of such replicas. Interestingly, he has not yet configured the system to search in the comments the words "Nicholas"? - Igor
  • one
    @Igor, the most interesting thing is, doesn’t he have a Parsing error, that after the condition he writes little meaningful words in Cyrillic ... - Yevgeny Ivanov
  • What mistake? no value is assigned to the val variable - user293746
  • one
    And for Evgeny Ivanov I put on two slashes - user293746

1 answer 1

Where the error comes from is clear: after all, nothing guarantees that within the while value of the variable val will be assigned. And reading an uninitialized variable in the language is not allowed.

Initialize val with some value before the loop.


And by the way, you have a val , apparently, a local variable. Class fields (both simple and static) are initialized by default to the default value of the corresponding type.

  • Thanks to all! Hike today I am stupid! - user293746
  • Such a childish question))) - user293746
  • @ user293746 If the answer suits you, do not forget to mark it as appropriate (checkmark to the left of the answer). - Regent
  • @ user293746: Please! Simple questions are important too. - VladD