BASIC OOP question

cheezy

Newcomer
Joined
Apr 11, 2003
Messages
13
Location
Tulsa, Oklahoma
Hi everyone;

I'm new at this and am pretty frustrated at what I'm missing here; hopefully somebody can enlighten me.

I have a class called DataStore that basically has a 2D Array in it:

C#:
public class DataStore  
{
	public string[,] X;
	public DataStore()
	{
	     InitializeString();
	}
	public void InitializeString()
	{
	 
	}
	public void StringMaker()
	{
	   string[,] X = new string[2,2];
                }
}

And I have a class Form1 (actually a Windows Form) that creates an instance of DataStore (called DS1):

public class Form1 : System.Windows.Forms.Form
{
	private System.Windows.Forms.Label label1;
	public DataStore DS1;
	private System.Windows.Forms.Button button1;
	private System.ComponentModel.Container components = null;

	public Form1()
	{					InitializeComponent();
	}

	protected override void Dispose( bool disposing )
	{
		if( disposing )
		{
			if (components != null) 
			{
			   components.Dispose();
			}
		}
		base.Dispose( disposing );
	}


				
	[STAThread]
	static void Main() 
	{			
                                Form1 main_form = new Form1();
		main_form.Show();
		Application.Run();

	}

	private void Form1_Load(object sender, System.EventArgs e)
	{
		DataStore DS1 = new DataStore();
	 }

	private void button1_Click(object sender, System.EventArgs e)
	{
			
	if (DS1  ==  null)
	{
		MessageBox.Show("X is null already");
                          	}
	else
	{
		if(DS1.X == null)
		{
		               DS1.StringMaker();
			DS1.X[0,0] = "Foo";
			label1.Text = DS1.X[0,0].ToString();
		}
	}

	private void button2_Click(object sender, System.EventArgs e)
	{
		 DataStore DS1 = new DataStore();
	}
  }


Button 1 basically checks the 'health' of the object DS1.

The first time I click button1, DS1 is confirmed to be null, but I instantiated it on Form1 load. Button2 is just being ornery on my part, but it STILL doesn't instantiate. And if I EVER attempt to put something in the DS1.X[] string, I get "Object reference not set to an instance of an object." (of course I do; DS1 doesn't exist!!!)

Can someone enlighten me here; I've been scratching my head on this type problem for a while now and I'm at wits' end.

Thanks!!
[edit]Added the [cs] [/cs] tags - Orb[/edit]
 
Last edited by a moderator:
change your form load to
C#:
   private void Form1_Load(object sender, System.EventArgs e)
    {
        DS1 = new DataStore();
     }
and all should be well.

Your current form load is declaring and instantiating a new variable called DS1 rather than referencing the form level one.

if you want button2 to work the same change will be needed.
 
OK, Thanks; that probably solved (hopefully) half of the problem; now I get an "Object reference not set to an instance of an object." issue on the line below "DS1.StringMaker();" statement in the button1 click event.

Why isn't my DataStore StringMaker method instantiating my string? Should it have to, since I already created an instance of DS1?

Thanks!
 
Back
Top