新問題和意見:如何訪問C#中的其他班級成員
感謝大家的建議。我實際上是從第二個線程調用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
}
}
感謝您的評論!我嘗試了你的建議。它創建了一個stackoverflow異常錯誤。這可能是由於我創建的用於訪問數據成員的Lru_operation類對象。你介意看看我對格蘭特的上述新問題和評論嗎?我以爲我可以通過創建一個對象來訪問其他類成員。非常感謝! – Kent