2014-01-07 85 views
1

新問題和意見:如何訪問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 
    } 
} 

回答

0

你永遠不會真的調用SetFreq()方法,所以你永遠不會有任何值。嘗試在構造函數中設置它:

public Lru_SetChanFreq() 
{ 
    this.SetFreq(); 
} 
+0

感謝您的評論!我嘗試了你的建議。它創建了一個stackoverflow異常錯誤。這可能是由於我創建的用於訪問數據成員的Lru_operation類對象。你介意看看我對格蘭特的上述新問題和評論嗎?我以爲我可以通過創建一個對象來訪問其他類成員。非常感謝! – Kent

0

你從未設置的ChFreq的值,因此,這只是它的默認值,一個空字符串。

Lru_SetChanFreq LruSetChFreq1 = new Lru_SetChanFreq(); 

// this 
LruSetChFreq1.ChFreq = "new value"; 
// or this 
LruSetChFreq1.SetFreq() 

Console.WriteLine("LruSetChFreq1.ChFreq = {0}", LruSetChFreq1.ChFreq); 
+0

嗨,感謝您的建議!我沒有按照你的建議插入上面的代碼。這是與Lru_Chan_Details類和actualFreq()方法內?你介意看看我的新問題和評論以及對其他兩個人的評論嗎?我可以在不通過方法參數列表傳遞數據的情況下訪問其他類數據成員嗎?謝謝你的耐心。 – Kent

0

有兩個問題,我看到:

  • 你不是叫SetFreq所以值永遠不會設置。您需要在您的Console.WriteLine報表之前實際撥打SetFreq

  • 即使你調用它,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); 
} 
+0

嗨格蘭特,感謝您的幫助!我嘗試了你的建議。我必須保持LruOp1.ChanDet.actualFreq();電話(見我上面的新問題和評論)。這是我的理解,我可以創建一個對象到我想訪問它的方法的類。一旦我進入這個方法,即actualFreq(),我想從我來自的類訪問數據成員(ChFreq)。我想我可以通過創建一個對象LruSetChFreq1並從該對象訪問ChFreq來實現。我基本上試圖繞過使用參數列表來傳遞成員。我可以這樣做嗎?哪個方法結束Lru_operation實例? – Kent

+0

你所做的一切都是_valid_(即不會導致異常),但是'LruOp2.SetChanFreq.ChFreq'將總是返回null(字符串的默認值),因爲你沒有調用SetFreq( )''實際上給'ChFreq'一個值的方法。所以輸出將是...什麼也不是。這就是爲什麼你什麼都看不到。我會通過修復更新我的答案。 –

+0

再次感謝格蘭特。我嘗試了修復。它會創建一個stackoverflow異常錯誤。發生什麼事是LruOp1.ChanDet.actualFreq();從SetFreq()中調用。然後LruSetChFreq1.SetFreq();從actualFreq()再次調用,我進入這個無限循環。看到我上面更新的代碼和問題/評論。 SetFreq()在單獨的線程中運行。它最初是從Lru_Listen課程中調用的。我測試確認ChFreq在SetFreq()中分配了405.0。這發生在Lru_Listen課程的初始調用中。是否有另一種方式訪問​​其他班級成員?再次感謝你。 – Kent