K
K
Kerim Rezai Shirazi2022-04-12 16:03:30
C++ / C#
Kerim Rezai Shirazi, 2022-04-12 16:03:30

Why from try catch, switch does not see the variable?

Why from try catch, switch does not see the variable?
62557891d17d8637390721.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vasily Bannikov, 2022-04-12
@judge971

Because curly braces in C# limit the scope of variables.
If you want the Number1 variable to be visible outside of the try block, you need to take it up a level.
Approximately like this:

double Number1 = 0;
try {
 // Тут пы инициализируем переменную
} catch {
  //Как-то обрабатываем возможную ошибку
}
// Тут мы можем использовать Number1

But since you are trying to handle an error when parsing numbers, then use the double.TryParse function instead of double.Parse - instead of throwing exceptions, it returns a flag whether the number was recognized successfully or not.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question