cheezy Posted April 21, 2003 Posted April 21, 2003 (edited) First App; basic question. I'm developing a test app to see how things work. I have two forms, A and B, and I want a string Astring to be available to both forms, even if the first is closed. I have the following (i'm leaving stuff out; if you need more info, i'm here ;) ): namespace Doug { public class Globals { public string Astring = "Joe"; } public class A : System.Windows.Forms.Form { private System.Windows.Forms.Button Next_button; private System.Windows.Forms.Label label_Regno; private System.Windows.Forms.TextBox text_Regno; private System.Windows.Forms.Label label1; public A() { InitializeComponent(); } static void Main() { Globals Joe = new Globals(); A Form_A = new A(); Form_A.label1.Text = Joe.Astring; Form_A.Show(); Application.Run(); } private void Next_Button_Click(object sender, System.EventArgs e) { B Form_B = new B(); Form_B.Show(); this.Dispose(); } ***** End of stuff in the first .cs file. *****Begin .cs file that has Form B stuff public class B: System.Windows.Forms.Form { private System.Windows.Forms.Label label5; private void B_Load(object sender, System.EventArgs e) { label5.Text = Joe.Astring; } **** End of Form B .cs stuff Problem is when I go to Debug this stuff I get "namespace Joe could not be found. " Obviously the instance of Globals called Joe I instantiated in the first part isn't available. Does anybody know the error of my ways here? Thanks. Edited February 19, 2007 by PlausiblyDamp Quote
Leaders Iceplug Posted April 21, 2003 Leaders Posted April 21, 2003 label5.Text = Joe.Astring; Shouldn't that be: label5.Text = Globals.Astring; ? :) Quote Iceplug, USN One of my coworkers thinks that I believe that drawing bullets is the most efficient way of drawing bullets. Whatever!!! :-(
cheezy Posted April 22, 2003 Author Posted April 22, 2003 I tried label5.Text = Globals.Astring; got the error: (420): An object reference is required for the nonstatic field, method, or property 'Doug.Globals.Astring' This would have worked if the original definition for Astring in the Globals class were 'static'. If this was the case (and I've tried it) I don't even have to create an instance of the Globals class (called Joe; an unfortunate and confusing naming convention for this example; sorry). Guess the problem is that I've created an instance of an object called 'Joe' that has an Astring string 'field' which I can't access. Is it a scope or permissions issue?? Thanks. Quote
*Gurus* Derek Stone Posted April 22, 2003 *Gurus* Posted April 22, 2003 Change the Astring declaration to make it static: public class Globals { public static string Astring = "Joe"; } Then access it from your forms like so: label5.Text = Globals.Astring; Quote Posting Guidelines
cheezy Posted April 23, 2003 Author Posted April 23, 2003 OK, that fixed the current problem; thanks. But there's a related problem with accessing objects created in one class from another class; it should work but doesn't. I'll open a new thread to keep things simpler. Thanks again!. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.