473,406 Members | 2,710 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Can't find object reference

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[i].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[i].SelectedItem != null && cbArray[i].SelectedItem.ToString()
!= "")
{
klr=Color.Red;
fklr= Color.White;
}
else
{
klr=Color.LightGreen;
fklr= Color.Black;
}
tbArray[i].BackColor=klr;
tbArray[i].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[i]);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.Fon tStyle.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[i].Top=0;}}
public void expand(int i){this[i].Top=0;this[i].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[i]);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));
}
}
}

Nov 16 '05 #1
3 3146
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(...);

--
Happy coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #2
Poewood <Po*****@discussions.microsoft.com> wrote:

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

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #3
Thanx Morten you were right on. I appreciate the time you took to help me.

Poe

"Morten Wennevik" wrote:
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(...);

--
Happy coding!
Morten Wennevik [C# MVP]

Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: lawrence | last post by:
How dangerous or stupid is it for an object to have a reference to the object which contains it? If I have a class called $controllerForAll which has an arrray of all the objects that exist, what...
2
by: brazilnut52 | last post by:
I am going to outline the steps I go through to produce the problem. Hopefully this will help you understand the problem better I have created a simple COM DLL in .NET by using the COM class...
39
by: Randell D. | last post by:
Folks, I'm sure this can be done legally, and not thru tricks of the trade - I hope someone can help. I'm writing a 'tool' (a function) which can be used generically in any of my projects. ...
3
by: Alex Stevens | last post by:
Hi. I have a class and it exposes a property, which accepts a parameter collection object. I want the class to use the parameter object and update it. However I don't want to use a copy of the...
5
by: Greg Vereschagin | last post by:
I'm trying to figure out a regular expression that will match the innermost tag and the contents in between. Specifically, the string that I am attempting to match looks as follows: ...
14
by: Robin Tucker | last post by:
Although I've been working on this project for 8 months now, I'm still not sure of the difference between ByVal and ByRef. As most objects in VB are reference types, passing ByVal I've discovered...
6
by: scottyman | last post by:
I can't make this script work properly. I've gone as far as I can with it and the rest is out of my ability. I can do some html editing but I'm lost in the Java world. The script at the bottom of...
9
by: OuaisBla | last post by:
Although STL container can't support object by reference as a template argument. To make thing worse, allocator can't support stack based allocation. So: std::deque<std::string const &> is...
11
by: LayneMitch via WebmasterKB.com | last post by:
Hello. This is a reference file from a book I read in which the core subject is the use of 'event listeners'. I'm trying to load the file in Firefox and it's giving me an error message: ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.