Can't find object reference

G

Guest

Okay here are four classes for a pocket pc program: Input, fpositional,
ComboBoxArray and TextBoxArray. The "input" class is the form. I use the
fpositional class to handle most of the functions for the objects on the
form, in addition the The objects are created in the fpositional class and
affixed to the Input form through the fpositional constructor which takes the
form as an argument. The ComboBox and TextBox Array classes hold the
respective objects. I want the objects to interact with eachother through
their
event handlers. This example tries to manipulate the Back and Fore color of
the two object types. (Note: if you don't have a PocketPC the PPC Emulator
will not change the color of the ComboBox.) You can add a MessageBox in the
updateObjectsColors() function in fpositional to report other property
settings.
You will get errors with the updateObjectsColors() function in the Array
classes. It asks for an object reference but I can't seem to find the
reference. The program will run if you comment out the updateObjectsColors()
used in the event handlers of the Array Classes. I hope someone can figure
this out.

Here are the 4 classes:

****Input Class****
using System;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using System.Data;

namespace PocketFSR
{
public class Input : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.MainMenu InputMenu;
fpositional position;

public Input(){InitializeComponent();}

protected override void Dispose( bool disposing )
{
base.Dispose( disposing );
}
#region Windows Form Designer generated code

private void InitializeComponent()
{
this.InputMenu = new System.Windows.Forms.MainMenu();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
//
// button1
//
this.button1.Location = new System.Drawing.Point(48, 208);
this.button1.Size = new System.Drawing.Size(64, 32);
this.button1.Text = "Add";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(120, 208);
this.button2.Size = new System.Drawing.Size(72, 32);
this.button2.Text = "Remove";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// Input
//
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Menu = this.InputMenu;
this.Text = "PocketFSR";

}
#endregion

static void Main(){Application.Run(new Input());}
private void button1_Click(object sender, System.EventArgs e)
{
position = new fpositional(this);
}
private void button2_Click(object sender, System.EventArgs e)
{
position.clearArrays();

}
}
}

*************fpositional class********

using System;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using System.Data;

namespace PocketFSR
{
public class fpositional
{
ComboBoxArray cbArray; TextBoxArray tbArray;
public fpositional(System.Windows.Forms.Form f)
{

tbArray = new TextBoxArray(f);
cbArray = new ComboBoxArray(f);
for(int x = 0;x<5;x++)
{
cbArray.AddNewComboBox();
tbArray.AddNewTextBox();
}
}

public void updateObjectsColors(int i)
{
Color klr, fklr;
switch (cbArray.SelectedItem.ToString())
{
case "Yes":
klr=Color.LightGreen;
fklr= Color.Black;
break;
case "No":
klr=Color.Red;
fklr= Color.White;
break;
case "NA":
klr=Color.Black;
fklr= Color.White;
break;
}

if (cbArray.SelectedItem != null && cbArray.SelectedItem.ToString()
!= "")
{
klr=Color.Red;
fklr= Color.White;
}
else
{
klr=Color.LightGreen;
fklr= Color.Black;
}
tbArray.BackColor=klr;
tbArray.ForeColor=fklr;
}
public void clearArrays()
{
cbArray.ClearArray();
tbArray.ClearArray();
}
}
}

*********TextBoxArray Class************

using System;

namespace PocketFSR
{
public class TextBoxArray : System.Collections.CollectionBase
{
private readonly System.Windows.Forms.Form HostForm;

public System.Windows.Forms.TextBox AddNewTextBox()
{
// Create a new instance of the TextBox class.
System.Windows.Forms.TextBox aTextBox = new
System.Windows.Forms.TextBox();
this.List.Add(aTextBox);
HostForm.Controls.Add(aTextBox);
// Set intial properties for the TextBox object.
aTextBox.Top = Count * 25;
aTextBox.Left = 0;
aTextBox.Multiline=true;
aTextBox.Text = "TextBox " + this.Count.ToString();
aTextBox.GotFocus += new System.EventHandler(GotFocusHandler);
aTextBox.LostFocus += new System.EventHandler(LostFocusHandler);
aTextBox.TextChanged += new System.EventHandler(TextChangedHandler);
return aTextBox;
}
public TextBoxArray(System.Windows.Forms.Form host) {HostForm = host;}
public System.Windows.Forms.TextBox this [int Index] {get{return
(System.Windows.Forms.TextBox) this.List[Index];}}
public void Remove(){if (this.Count >
0){HostForm.Controls.Remove(this[this.Count-1]); this.List.RemoveAt(this.Count -1);}}
public void ClearArray(){for (int i =
this.List.Count-1;i>-1;i--){HostForm.Controls.Remove(this);this.List.RemoveAt(i);}}
public void GotFocusHandler(Object sender, System.EventArgs e)
{
if (this[this.List.IndexOf(sender)].Text != null &&
this[this.List.IndexOf(sender)].Text != "")
{
this[this.List.IndexOf(sender)].SelectionStart =
this[this.List.IndexOf(sender)].Text.Length;
}
else
{
updateObjectsColors(this.List.IndexOf(sender));
}
}
public void LostFocusHandler(Object sender, System.EventArgs e)
{
updateObjectsColors(this.List.IndexOf(sender));
}
public void TextChangedHandler(Object sender, System.EventArgs e)
{
updateObjectsColors(this.List.IndexOf(sender));
}
}
}

*********ComboBoxArray Class************

using System;

namespace PocketFSR
{
public class ComboBoxArray : System.Collections.CollectionBase
{
private readonly System.Windows.Forms.Form HostForm;
public System.Windows.Forms.ComboBox AddNewComboBox()
{ // Create a new instance of the ComboBox class.
System.Windows.Forms.ComboBox aComboBox = new
System.Windows.Forms.ComboBox();
this.List.Add(aComboBox);
HostForm.Controls.Add(aComboBox);
// Set intial properties for the ComboBox object.
//aComboBox.Size = System.Drawing.Size(46,20);
aComboBox.Top = Count * 25;
aComboBox.Left = 100;
aComboBox.Items.Add("NA");
aComboBox.Items.Add("Yes");
aComboBox.Items.Add("No");
aComboBox.Font=new
System.Drawing.Font("Tahoma",10,System.Drawing.FontStyle.Bold);
aComboBox.SelectedIndexChanged += new
System.EventHandler(SelectedIndexChangedHandler);
aComboBox.GotFocus += new System.EventHandler(GotFocusHandler);
return aComboBox;
}
public ComboBoxArray(System.Windows.Forms.Form host){HostForm = host;}
public System.Windows.Forms.ComboBox this [int Index] { get {return
(System.Windows.Forms.ComboBox) this.List[Index];}}
public void setPosition(){for(int i =
0;i<this.List.Count;i++){this.Top=0;}}
public void expand(int i){this.Top=0;this.BringToFront();}
public void Remove(){if (this.Count >
0){HostForm.Controls.Remove(this[this.Count-1]);this.List.RemoveAt(this.Count
-1);}}
public void ClearArray(){for (int i =
this.List.Count-1;i>-1;i--){HostForm.Controls.Remove(this);this.List.RemoveAt(i);}}
public void GotFocusHandler(Object sender, System.EventArgs
e){this[this.List.IndexOf(sender)].BackColor=System.Drawing.Color.White;this[this.List.IndexOf(sender)].ForeColor=System.Drawing.Color.Black;}
public void SelectedIndexChangedHandler(Object sender, System.EventArgs e)
{ switch(this[this.List.IndexOf(sender)].SelectedItem.ToString())
{
case "NA":
this[this.List.IndexOf(sender)].BackColor=System.Drawing.Color.Black;
this[this.List.IndexOf(sender)].ForeColor=System.Drawing.Color.White;
break;
case "No":
this[this.List.IndexOf(sender)].BackColor=System.Drawing.Color.Red;
this[this.List.IndexOf(sender)].ForeColor=System.Drawing.Color.White;
break;
case "Yes":
this[this.List.IndexOf(sender)].BackColor=System.Drawing.Color.LightGreen;
this[this.List.IndexOf(sender)].ForeColor=System.Drawing.Color.Black;
break;
}
updateObjectsColors(this.List.IndexOf(sender));
}
}
}
 
M

Morten Wennevik

Hi Poewood,

Well, of course you get errors (I assume you mean compiler errors).
You call updateObjectsColors, but you don't say where the method is located, so the compiler will look inside the TextBoxArray/ComboBoxArray classes. And there are no updateObjectsColors method in either one.

You do, however, have the method in the fpositional class, but to use that one you will need to pass it as a reference when creating the arrays.

tbArray = new TextBoxArray(f, fp);

public TextBoxArray(System.Windows.Forms.Form host, fpositional fp)
{
HostForm = host;
parent_fpositional = fp;
}

....

parent_fpositional.updateObjectsColors(...);
 
J

Jon Skeet [C# MVP]

You will get errors with the updateObjectsColors() function in the Array
classes. It asks for an object reference but I can't seem to find the
reference.

What *exactly* does it do? Please post the *exact* error message.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top