cheezy Posted July 30, 2003 Posted July 30, 2003 (edited) 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: 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] Edited July 30, 2003 by John Quote
Administrators PlausiblyDamp Posted July 30, 2003 Administrators Posted July 30, 2003 change your form load to 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. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
cheezy Posted July 31, 2003 Author Posted July 31, 2003 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! Quote
Administrators PlausiblyDamp Posted July 31, 2003 Administrators Posted July 31, 2003 same thing really change the StringMaker method to public void StringMaker() { X = new string[2,2]; } Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
cheezy Posted July 31, 2003 Author Posted July 31, 2003 That did it. Thank you very much. Now I can proceed instead of spin wheels! 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.