Jump to content
Xtreme .Net Talk

Recommended Posts

Posted (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 by PlausiblyDamp
  • Leaders
Posted

label5.Text = Joe.Astring;

Shouldn't that be:

label5.Text = Globals.Astring;

? :)

Iceplug, USN

One of my coworkers thinks that I believe that drawing bullets is the most efficient way of drawing bullets. Whatever!!! :-(

Posted

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.

Posted

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!.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...