#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    9
    Rep Power
    0

    How to access other class members in C#


    Forgive me, I'm new to C# and object-oriented programming. I'm having difficulty accessing other class members. I have 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.

    Code:
    // 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
    	}
    }
  2. #2
  3. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,126
    Rep Power
    9398
    Given how little you posted (only part of SetFreq and nothing regarding Lru_operation) I'm going to guess that you're not setting the ChFreq and/or not setting it on the same object that you're using in actualFreq.

    I'm thinking probably the latter. When you do a "new Class" you're creating a brand new class. If code somewhere else also does a "new Class" then it'll get yet another class. The two are separate, and setting the ChFreq on one has absolutely no bearing on the ChFreq of the other.
    Your solution is either (a) make sure you're setting ChFreq on the same object without creating a new instance, and/or (b) pass the instance you made in actualFreq around to the various methods that need to access it.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    9
    Rep Power
    0
    Thank you for your comments! Please see my updated code. I initially assigned the value with: LruOp1.SetChanFreq.ChFreq = "405.0"; in SetFreq(). I then tried to access it with: LruOp2.SetChanFreq.ChFreq in actualFreq(). These methods are in different classes. Are you saying that the best way to access ChFreq is to pass the LruOp1 object thru the argument list as follows? Forgive my object-oriented ignorance (newbie). I was hoping I could access the data members by avoiding passing things in the argument list. Thank you again for your recommendation.

    Code:
    public void SetFreq()
    {
        // do stuff
        LruOp1.ChanDet.actualFreq(LruOp1);
    }  
    
    public void actualFreq(object LruOp1)
    {
        // do stuff
    }

IMN logo majestic logo threadwatch logo seochat tools logo