Ссылку на переменную в другой класс - C#
Формулировка задачи:
Здравствуйте!!!
Есть такой код:
нужно чтобы переменная int i2 изменялась в потоке объекта Test???
Листинг программы
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- namespace ReferenseTestApp
- {
- class Program
- {
- static void Main(string[] args)
- {
- int i2 = 0;
- var test = new Test(ref i2);
- while (i2 < 10000)
- {
- Console.WriteLine(i2.ToString());
- Thread.Sleep(1000);
- }
- }
- }
- class Test
- {
- private int i = 0;
- private Thread thr;
- public Test(ref int i1)
- {
- i1 = i;
- this.thr = new Thread(SetRef);
- thr.Start();
- }
- private void SetRef()
- {
- while (this.i < 1000)
- {
- Console.WriteLine("settet...");
- this.i++;
- Thread.Sleep(1000);
- }
- }
- }
- }
Решение задачи: «Ссылку на переменную в другой класс»
textual
Листинг программы
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- namespace ReferenseTestApp
- {
- class Program
- {
- static void Main(string[] args)
- {
- var cl = new Cl1();
- cl.write();
- Console.ReadKey();
- }
- }
- class Cl1
- {
- public int i = 0;
- public void write()
- {
- var test = new Test(this);
- while (i < 10000)
- {
- Console.WriteLine(i.ToString());
- Thread.Sleep(1000);
- }
- }
- }
- class Test
- {
- public int i = 0;
- private Thread thr;
- private Cl1 c;
- public Test(Cl1 cl)
- {
- this.thr = new Thread(SetRef);
- this.c = cl;
- thr.Start();
- }
- private void SetRef()
- {
- while (this.i < 1000)
- {
- c.i++;
- Thread.Sleep(1000);
- }
- }
- }
- }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д