Ссылку на переменную в другой класс - C#

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

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

Здравствуйте!!! Есть такой код:
Листинг программы
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. namespace ReferenseTestApp
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. int i2 = 0;
  14. var test = new Test(ref i2);
  15. while (i2 < 10000)
  16. {
  17. Console.WriteLine(i2.ToString());
  18. Thread.Sleep(1000);
  19. }
  20. }
  21. }
  22. class Test
  23. {
  24. private int i = 0;
  25. private Thread thr;
  26. public Test(ref int i1)
  27. {
  28. i1 = i;
  29. this.thr = new Thread(SetRef);
  30. thr.Start();
  31. }
  32. private void SetRef()
  33. {
  34. while (this.i < 1000)
  35. {
  36. Console.WriteLine("settet...");
  37. this.i++;
  38. Thread.Sleep(1000);
  39. }
  40. }
  41. }
  42. }
нужно чтобы переменная int i2 изменялась в потоке объекта Test???

Решение задачи: «Ссылку на переменную в другой класс»

textual
Листинг программы
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8.  
  9. namespace ReferenseTestApp
  10. {
  11.     class Program
  12.     {
  13.         static void Main(string[] args)
  14.         {
  15.             var cl = new Cl1();
  16.             cl.write();
  17.             Console.ReadKey();
  18.         }
  19.     }
  20.  
  21.     class Cl1
  22.     {
  23.         public int i = 0;
  24.         public void write()
  25.         {
  26.             var test = new Test(this);
  27.             while (i < 10000)
  28.             {
  29.                 Console.WriteLine(i.ToString());
  30.                 Thread.Sleep(1000);
  31.             }
  32.         }
  33.  
  34.     }
  35.  
  36.     class Test
  37.     {
  38.         public int i = 0;
  39.         private Thread thr;
  40.         private Cl1 c;
  41.  
  42.         public Test(Cl1 cl)
  43.         {
  44.             this.thr = new Thread(SetRef);
  45.             this.c = cl;
  46.             thr.Start();
  47.         }
  48.        
  49.         private void SetRef()
  50.         {
  51.             while (this.i < 1000)
  52.             {
  53.                 c.i++;
  54.                 Thread.Sleep(1000);
  55.             }
  56.         }
  57.     }
  58. }

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


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

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

7   голосов , оценка 3.857 из 5

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

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

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