新问题和评论:
感谢大家的建议。我实际上从第二个线程调用了SetFreq()。我需要两个线程。他们都在运行独立的任务。第二个线程通过Lru_Channel_Details类共享主线程的数据。我很难理解如何从第二个线程中的Lru_SetChanFreq类访问ChFreq数据成员。我更新了以下代码。我希望它能解释我想要做的事情。对不起,如果没有,我很乐意澄清。
再次感谢大家的推荐。
原始问题:
请原谅我,我是C#和面向对象编程的新手。我无法访问其他班级成员。我创建了其他类对象来访问其成员字段。代码编译但我在显示它时没有得到任何结果。 我找不到问题的答案是不成功的。有人可以告诉我我忽略了什么吗?下面是一段代码,说明了我的问题。
再次感谢您提出的任何建议。
// this class I want to use the value of ChFreq from the Lru_SetChanFreq class to do some stuff for the Lru_operation class in the main thread to use
public class Lru_Channel_Details
{
public void actualFreq()
{
Lru_operation LruOp2 = new Lru_operation(); // create main operations class object to access ChFreq
Lru_SetChanFreq LruSetChFreq1 = new Lru_SetChanFreq(); // (optional): create other class object to access ChFreq
Console.WriteLine("LruOp2.SetChanFreq.ChFreq = {0}", LruOp2.SetChanFreq.ChFreq); // fails to display the ChFreq value
Console.WriteLine("LruSetChFreq1.ChFreq = {0}", LruSetChFreq1.ChFreq); // (optional:) also fails to display the value
}
}
// in this class, I have set the values of ChFreq to 405.0. the call to do this came from the Lru_Listen class which runs in a 2ndary thread.
// this class then calls the actualFreq() from Lru_Channel_Details class. The Lru_Channel_Details class is also accessed from the Lru_operation class,
// which is running in the main thread.
public class Lru_SetChanFreq
{
private string chFreq;
public string ChFreq
{
get { return chFreq; }
set { chFreq = value; }
}
public void SetFreq()
{
Lru_operation LruOp1 = new Lru_operation(); // this object accesses multiple other classes
LruOp1.SetChanFreq.ChFreq = "405.0"; // assign this value using main operations class object
LruOp1.ChanDet.actualFreq(); // calls another class method to use ChFreq
// does stuff with LruOp1 to access other class methods (not shown)
}
}
// this is where the program begins. I'm running 2 threads concurrently and I need to share data between them.
[STAThread]
static void Main()
{
// starts a 2ndary thread to do stuff while the main thread is working.
Lru_Listen LruListen1 = new Lru_Listen();
Thread LruListenThread = new Thread(new ThreadStart(LruListen1.ListenForAag));
LruListenThread.Start();
while(!LruListenThread.IsAlive);
Thread.Sleep(1);
Lru_operation LruOpX = new Lru_operation(); // create object to access Lru operation method
LruOpX.LruOperation();
// this class is my main operations class. it is running in the main thread. the below objects are used to access other
// class members. it's main purpose is to take data from the Lru_Channel_Details class do some stuff on it and pass it to
// another class. it must be running in it's own thread.
public class Lru_operation
{
// this object is only used in other classes to access its class members. it's not used in the main operations class
public Lru_SetChanFreq SetChanFreq = new Lru_SetChanFreq();
// this object is used in the main operations class to call methods from its class
public Lru_Channel_Details ChanDet = new Lru_Channel_Details();
// does stuff with the above class objects' methods
}
// this class is running in a 2nd thread concurrently with the main thread. it needs to share other class data with the main thread.
// it's main purpose is to do some stuff to get data then call the SetFreq() from the Lru_SetChanFreq class
public class Lru_Listen
{
public void LruShowRequestData()
{
// do some other stuff
Lru_operation LruOp3 = new Lru_operation(); // create object to access set channel frequency method
LruOp3.SetChanFreq.SetFreq(); // here is where SetFreq() from the Lru_SetChanFreq class is called
}
}
答案 0 :(得分:0)
你实际上从未调用过SetFreq()方法,所以你永远不会有任何值。尝试在构造函数中设置它:
public Lru_SetChanFreq()
{
this.SetFreq();
}
答案 1 :(得分:0)
你永远不会设置ChFreq
的值,所以它只是它的默认值,一个空字符串。
Lru_SetChanFreq LruSetChFreq1 = new Lru_SetChanFreq();
// this
LruSetChFreq1.ChFreq = "new value";
// or this
LruSetChFreq1.SetFreq()
Console.WriteLine("LruSetChFreq1.ChFreq = {0}", LruSetChFreq1.ChFreq);
答案 2 :(得分:0)
我看到两个问题:
您没有调用SetFreq
,因此永远不会设置该值。您需要在SetFreq
声明之前实际致电Console.WriteLine
。
即使您正在调用它,SetFreq
也会创建一个新的Lru_operation
实例,该方法在方法运行时丢失,而不是仅仅将值分配给当前的实例。从SetFreq
中删除三行代码,并将其替换为:chFreq = "405.0";
以下是修改代码以获得预期结果的方法。你刚才犯了一个简单的错误。
public void actualFreq()
{
Lru_operation LruOp2 = new Lru_operation();
Lru_SetChanFreq LruSetChFreq1 = new Lru_SetChanFreq();
LruOp2.SetChanFreq.SetFreq();
LruSetChFreq1.SetFreq();
Console.WriteLine("LruOp2.SetChanFreq.ChFreq = {0}", LruOp2.SetChanFreq.ChFreq);
Console.WriteLine("LruSetChFreq1.ChFreq = {0}", LruSetChFreq1.ChFreq);
}