C Board  

Go Back   C Board > General Programming Boards > C# Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 05-14-2005, 09:30 PM   #1
pug
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
pug is offline   Reply With Quote
Old 05-18-2005, 08:30 PM   #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.
nickname_changed is offline   Reply With Quote
Old 05-20-2005, 03:51 AM   #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
Mezzano is offline   Reply With Quote
Old 05-20-2005, 08:46 AM   #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.
nickname_changed is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 12:26 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22