Как использовать переменную из другого метода? - C#

Узнай цену своей работы

Формулировка задачи:

Есть метод
Листинг программы
  1. public static void Put()
  2. {
  3. string put = Console.ReadLine();
  4. if (put == "default") put = @"Dictionary.txt";
  5. }
Как переменную "put" использоват в этом конструкторе?
Листинг программы
  1. public Translator(int direction)
  2. {
  3. string[] dictionary = File.ReadAllLines(@"Dictionary.txt", Encoding.Default);
  4. foreach (string line in dictionary)
  5. {
  6. string[] words = line.Split(' ');
  7. if (direction == 1)
  8. dict.Add(words[0], words[1]);
  9. else
  10. dict.Add(words[1], words[0]);
  11. }
  12. }

Решение задачи: «Как использовать переменную из другого метода?»

textual
Листинг программы
  1. private static string put = string.Empty;
  2.  
  3. public static void Put()
  4.         {
  5.             put = Console.ReadLine();
  6.             if (put == "default") put = @"Dictionary.txt";
  7.         }
  8.  
  9. public Translator(int direction)
  10.         {
  11.             string[] dictionary = File.ReadAllLines( put, Encoding.Default);
  12.             foreach (string line in dictionary)
  13.             {
  14.                 string[] words = line.Split(' ');
  15.                 if (direction == 1)
  16.                 dict.Add(words[0], words[1]);
  17.                 else
  18.                 dict.Add(words[1], words[0]);
  19.             }
  20.         }
  21.  
  22. // строка может быть пустой? кто за кем вызывается? Оба метода точно должны быть public?
  23.  
  24. //1 вариант. код выше можно оставить.
  25. public Translator(int direction)
  26.         {
  27.             Put();
  28.             string[] dictionary = File.ReadAllLines( put, Encoding.Default);
  29.             foreach (string line in dictionary)
  30.             {
  31.                 string[] words = line.Split(' ');
  32.                 if (direction == 1)
  33.                 dict.Add(words[0], words[1]);
  34.                 else
  35.                 dict.Add(words[1], words[0]);
  36.             }
  37.         }
  38.  
  39. //2 вариант. код выше не нужен.
  40. public static string Put()
  41.         {
  42.             string put = Console.ReadLine();
  43.             if (put == "default") put = @"Dictionary.txt";
  44.             return put;
  45.         }
  46. public Translator(int direction)
  47.         {
  48.             string[] dictionary = File.ReadAllLines( Put(), Encoding.Default);
  49.             foreach (string line in dictionary)
  50.             {
  51.                 string[] words = line.Split(' ');
  52.                 if (direction == 1)
  53.                 dict.Add(words[0], words[1]);
  54.                 else
  55.                 dict.Add(words[1], words[0]);
  56.             }
  57.         }

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

12   голосов , оценка 3.833 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут
Похожие ответы