-
January 6th, 2014, 08:16 PM
#1
How to access other class members in C#
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've searched here and googled unsuccessfully to find an answer to my question. Can someone show me what I have overlooked? Below is a snippet of the code involved.
Thank you very much, Kent
Code:
// this class I want to use the value of ChFreq from another class
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(); // 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); // also fails to display the value
}
}
// in this class, I have set the values of ChFreq to 405.0
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 class is my main operations class. the below objects are used to access other
// class members from this class
public class Lru_operation
{
public Lru_SetChanFreq SetChanFreq = new Lru_SetChanFreq();
public Lru_Channel_Details ChanDet = new Lru_Channel_Details();
// does stuff with the above class objects' methods
}
-
January 10th, 2014, 12:05 AM
#2
Re: How to access other class members in C#
More info needed.
I'm assuming you are referring to the ChFreq member?
While you have your Set/Get coded, I do not see where you are actually providing a value to ChFreq.
And where are you running this from? I don't see a MAIN here
When you are setting the value of ChFreq from SetFreq(), you are creating an object called LurOp1.
But then, when you are trying to print it, you are instead creating a completely DIFFERENT object called LruOp2.
Also, you are creating a third object called LruSetChFreq1.
Three different objects and not one of them are you actually setting the value of ChFreq.
Either you instantiate an object from class Lru_SetChanFreq and then set the value of ChFreq before you try to print it (from the same object), or you place a constructor within Lru_SetChanFreq so that the value is set when the object is instantiated.
Example:
Code:
static void Main(string[] args)
{
Lru_SetChanFreq SCF = new Lru_SetChanFreq();
SCF.ChFreq = "405.0";
Console.WriteLine("SCF.ChFreq = {0}", SCF.ChFreq);
}
}
or perhaps...
Code:
class Program
{
static void Main(string[] args)
{
//instantiate the object which triggers the constructor to set ChFreq.
Lru_SetChanFreq SetFreq = new Lru_SetChanFreq();
//Call the PrintFreq() method of the object.
SetFreq.PrintFreq();
}
}
// in this class, I have set the values of ChFreq to 405.0
public class Lru_SetChanFreq
{
private string chFreq;
public string ChFreq
{
get { return chFreq; }
set { chFreq = value; }
}
//constructor
public Lru_SetChanFreq()
{
this.ChFreq = "405.0";
}
public void PrintFreq()
{
Console.WriteLine("The ChFreq is {0}", this.ChFreq);
}
}
I'm no expert, but it seems you are trying to get info from several objects that have not had its member value set.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|