简体   繁体   中英

How to access other class members in C#

New question and comments:

Thanks everyone for your suggestions. I actually called SetFreq() from a 2nd thread. I need two threads. They both are running independent tasks. The 2nd thread shares data for the main thread thru the Lru_Channel_Details class. I'm having difficulty understanding how to access the ChFreq data member from the Lru_SetChanFreq class in the 2nd thread. I updated the code below. I hope it explains what I'm trying to do. Sorry if not, I am happy to clarify.

Thank you all again for recommendations.

Original question:

Forgive me, I'm new to C# and object-oriented programming. I'm having difficulty accessing other class members. I've created other class objects to access their member fields. The code compiles but I don't get any results back when I display them. I'm unsuccessful to find an answer to my question. Can someone please show me what I have overlooked? Below is a snippet of code illustrating my problem.

Thank you again for any recommendations you can give.

    // 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
    }
}

You're never actually calling the SetFreq() method, so you'll never have any values in there. Try setting it in the constructor:

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

You never set the value of ChFreq so it is just its default value, an empty string.

Lru_SetChanFreq LruSetChFreq1 = new Lru_SetChanFreq();

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

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

There are two issues I see:

  • You're not calling SetFreq so the value is never set. You need to actually call SetFreq prior to your Console.WriteLine statements.

  • And even if you were calling it, SetFreq creates a new instance of Lru_operation that is lost when the method is done running, instead of just assigning the value to the current one. Remove the three lines of code from SetFreq and replace them with: chFreq = "405.0";

Here's how you could modify your code to get the expected result. You just made a simple mistake.

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM