简体   繁体   English

如何在C#中访问其他班级成员

[英]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. 我实际上是从第二个线程调用SetFreq()的。 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. 第二个线程通过Lru_Channel_Details类共享主线程的数据。 I'm having difficulty understanding how to access the ChFreq data member from the Lru_SetChanFreq class in the 2nd thread. 我很难理解如何从第二个线程中的Lru_SetChanFreq类访问ChFreq数据成员。 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. 请原谅,我是C#和面向对象编程的新手。 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. 您实际上从未真正调用过SetFreq()方法,因此那里永远不会有任何值。 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. 您永远不会设置ChFreq的值,因此它只是它的默认值,一个空字符串。

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. 您没有调用SetFreq因此永远不会设置该值。 You need to actually call SetFreq prior to your Console.WriteLine statements. 您实际上需要在Console.WriteLine语句之前调用SetFreq

  • 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. 即使您正在调用它, SetFreq创建一个新的Lru_operation实例,该实例在该方法完成运行时会丢失,而不仅仅是将值分配给当前实例。 Remove the three lines of code from SetFreq and replace them with: chFreq = "405.0"; SetFreq删除三行代码,并将其替换为: 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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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