Thread: accessing class variables

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    3

    accessing class variables

    hello again,

    sorry about another post but im new to OOP and i am having trouble understanding how i can access a variable from one class from a different class.

    I have a class

    Code:
    public class Main : System.Web.UI.Page
    {
           public string loadFile = " ";
           .    
           .
    public void Page_Load(object sender, System.EventArgs e)
    {
           loadFile = "test";
    }
    then in my messsage class i have

    Code:
    public class Message : System.Web.UI.Page
    {
    Private void Page_Load(object sender, System.EventArgs e)
    {	
    	Main mc = new Main();
           StreamReader sr = new StreamReader(mc.loadFile);	
            .
            .
    }
    i have access to the variable "loadFile" but it doesnt seem to grab the text out of the string. OOP is starting to frustrate me >.<

    Thanks again guys

  2. #2
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986
    You've got a few choices.

    1. Using events to send messages from one class to another (search for 'custom events in C#' on google)
    2. Using events to send messages to an intermediate class, who sends it to the other class (again, custom events)
    3. Use a single class with a static variable called loadFile.
    4. Make loadFile a static propery in the form its already in (in your message class you use Main.loadFile)
    A static variable might be easiest. Just go:
    Code:
    public class Main : System.Web.UI.Page
    {
    	   public static string loadFile = " ";
    	   .	
    	   .
    public void Page_Load(object sender, System.EventArgs e)
    {
    	   loadFile = "test";
    }
    
    --------------
    
    public class Message : System.Web.UI.Page
    {
    Private void Page_Load(object sender, System.EventArgs e)
    {	
    	Main mc = new Main();
    	   StreamReader sr = new StreamReader(Main.loadFile);	
    		.
    		.
    }
    Static veriables like this aren't the best OOP way though. A better way would be to use Main to set the loadFile property on the Message class.

  3. #3
    Registered User
    Join Date
    May 2005
    Posts
    28
    Quote Originally Posted by stovellp
    You've got a few choices.

    1. Using events to send messages from one class to another (search for 'custom events in C#' on google)
    2. Using events to send messages to an intermediate class, who sends it to the other class (again, custom events)
    3. Use a single class with a static variable called loadFile.
    4. Make loadFile a static propery in the form its already in (in your message class you use Main.loadFile)
    You are missing more obvious choices. First off all in OOP you shouldn't have public member fields. Your member fields should be private and if you want to allow direct public access use properties. The reason it isn't "picking" up the string is that you are setting it in the page_load event, where you want to set it, at least for purposes of this test, is in the class constructor.

    Code:
    public class Main : System.Web.UI.Page
    {
    	  private string loadFile;
    
             public Main()
             {
                  loadFile = "test";
             }
    	   
    
             public string LoadFile
             {
                  get
                  {
                      return loadFile;
                  }
              }
    }
    
    --------------
    
    public class Message : System.Web.UI.Page
    {
    Private void Page_Load(object sender, System.EventArgs e)
    {	
    	Main mc = new Main();
    	StreamReader sr = new StreamReader(mc.LoadFile);	
    		.
    		.
    }


    Mezzano

  4. #4
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986
    Quote Originally Posted by me
    Make loadFile a static propery in the form its already in (in your message class you use Main.loadFile)
    I did point out he could use a public property, only I said static. There really is no reason an ASP.NET web page should be creating an instance of another ASP.NET page at runtime. I don't even know if thats legal to do, and if it is, its stupid. That's why I reccomend a public static property - especially since its just for a single string. The only problem is the string will be set once for the appdomain (which could be forever).

    There really are better ways to accomplish what he is trying to accomplish (whatever exactly that is) with better OO than creating another instance of an ASP.NET page in memory.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Accessing a struct element in a different class...
    By Sparrowhawk in forum C++ Programming
    Replies: 0
    Last Post: 03-09-2009, 04:24 PM
  2. Global variables used in a linked class, getting errors.
    By RealityFusion in forum C++ Programming
    Replies: 3
    Last Post: 09-24-2005, 12:25 AM
  3. Static variables and functions problem in a class
    By earth_angel in forum C++ Programming
    Replies: 16
    Last Post: 09-15-2005, 12:08 PM
  4. My Window Class
    By Epo in forum Game Programming
    Replies: 2
    Last Post: 07-10-2005, 02:33 PM
  5. accessing Class members
    By fizisyen in forum C++ Programming
    Replies: 3
    Last Post: 01-07-2004, 06:18 PM