Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > Other .NET > General .NET
| Search | Today's Posts | Mark Forums Read
General .NET For general discussion of MICROSOFT .NET topics that don't fall within any of the other .NET forum subcategories or .NET language forums.  If your question is specific to a language (C# or Visual Basic) or type of application (Windows Forms or ASP.Net) try an applicable forum category. ** PLEASE BE SPECIFIC WITH YOUR QUESTION ** When posting here, provide details regarding the Microsoft .NET language you are using and/or what type of application (Windows/Web Forms, etc) you are working in, if applicable to the question. This will help others answer the question without having to ask.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the General .NET section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 28th, 2006, 09:47 AM
Registered User
 
Join Date: Aug 2006
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Object reference Syntax help

I am getting an error when I try to call a function from Form1.cs.
The function works properly when called from a button on the form. Instead of posting the code here you can see it in the following MSDN article. http://msdn.microsoft.com/library/de...VisualCNET.asp

I adapted the code to create a control array of text boxes. In the section "creating a common event handler" the article demonstrates firing a message box that displays which control was clicked. What I want to do is call a function on the form which sums up all of the numbers in the text box whenever the number in a textbox is changed. So I am using
aHoursTextBox.TextChanged += new System.EventHandler(TextChangedHandler); in the AddNew method of the array class and I have the event handler setup so that I can trigger the same message box when the text is changed so I know that everything is correct except that I get the following error when I try to compile:
An object reference is required for the nonstatic field, method or property 'BCTimeEntry.TimeEntry.SumUp()'
Here is my SumUp method:
<div align="left">
public void SumUp()
        {
            int iRowCount = MyEarnCodeArray.Count;
            double dReg = 0,dVac = 0,dPers = 0,dSick = 0,dJury =0,dBereav = 0;
            string sEarnCode;
            for (int i = 0; i < iRowCount ; i++)
            {
                //MessageBox.Show(Convert.ToString(iRowCount));
                sEarnCode = MyEarnCodeArray[i].Text;
                //MessageBox.Show(sEarnCode + " " + MyTotalHoursArray[i].Text);
                switch(sEarnCode)
                {
                    case "Reg":
                        dReg += Convert.ToDouble(MyTotalHoursArray[i].Text);
                        lblReg.Text = "Reg " + Convert.ToString(dReg);
                        break;
                    case "Vac":
                        dVac += Convert.ToDouble(MyTotalHoursArray[i].Text);
                        lblVac.Text = "Vac " + Convert.ToString(dVac);
                        break;
                    case "Pers":
                        dPers += Convert.ToDouble(MyTotalHoursArray[i].Text);
                        lblPers.Text = "Pers " + Convert.ToString(dPers);
                        break;
                    case "Sick":
                        dSick += Convert.ToDouble(MyTotalHoursArray[i].Text);
                        lblSick.Text = "Sick " + Convert.ToString(dSick);
                        break;
                    case "Jury":
                        dJury += Convert.ToDouble(MyTotalHoursArray[i].Text);
                        lblJury.Text = "Jury " + Convert.ToString(dJury);
                        break;
                    case "Bereav":
                        dBereav += Convert.ToDouble(MyTotalHoursArray[i].Text);
                        lblBereav.Text = "Bereav " + Convert.ToString(dBereav);
                        break;

                }

            }
        }
</div id="left">

Can someone help me with the correct syntax for calling this method?
Thanks
Dan

 
Old August 29th, 2006, 10:30 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , .
Posts: 453
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via AIM to Ankur_Verma Send a message via MSN to Ankur_Verma
Default

How are you calling this function?

Regards
Ankur
 
Old August 30th, 2006, 10:12 AM
Registered User
 
Join Date: Aug 2006
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

From the textBox array class I have tried the following:
The message box works but I can't get the SumUp routine to fire from the TextChangedHandler.
<div align="left">
public void TextChangedHandler(Object sender, System.EventArgs e)
        {
                //BCTimeEntry.TimeEntry.SumUp();

            //((System.Windows.Forms.Form) sender).
            //System.Windows.Forms.Form.TimeEntry.SumUp();
            //((System.Windows.Forms.Form) sender).SumUp();
            //System.Windows.Forms.MessageBox.Show("You have clicked TextBox " +
            // ((System.Windows.Forms.TextBox) sender).Tag.ToString());
    //TimeEntry.SumUp();
</div id="left">

 
Old August 30th, 2006, 10:41 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , .
Posts: 453
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via AIM to Ankur_Verma Send a message via MSN to Ankur_Verma
Default

What I can guess looking at this piece of code is that you have a Class
called TimeEntry in which you are writing all this code. That is TextChangedHandler
and SumUp are both defined in the class TimeEntry. Plese verify if this is the case.

And if that indeed is the case

then try writing only

SumUp();

after the messagebox and it should work.

Regards
Ankur
 
Old September 1st, 2006, 01:27 PM
Registered User
 
Join Date: Aug 2006
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I want to call it from another class in the same project and I get the error described with all of the syntax variations I have tried. If I try to call to call it from the same class then SumUp() does work. Since I am using the other class to provide the functionality of a text box control array, I can only use the other class to capture the text changed event.

 
Old September 3rd, 2006, 07:17 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , .
Posts: 453
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via AIM to Ankur_Verma Send a message via MSN to Ankur_Verma
Default

It still is quite solvable.
At some point you must have called the 'AddNew' function of control array
class. You can call the SumUp function in the same manner.
Do you know the concept of Classes and Objects?
If you dont then just do this. Lets assume that you called AddNew function
like this

xyz.AddNew();

in this case xyz is the object of your control array class and through xzy
you can call all the public functions of your control array class. AddNew is
one such function and so is SumUp.

To call SumUp you write a similar statment as above, keep the things before the
dot(.) as they were in the statement with which you called AddNew, only in place
of AddNew you write SumUp this time like this

xyz.SumUp().

If that doesnt fix it, post the code of both your classes to get a sure shot
solution.

Regards
Ankur




Similar Threads
Thread Thread Starter Forum Replies Last Post
object reference not set to instant of an object shahidrasul ASP.NET 2.0 Basics 1 September 5th, 2008 02:01 PM
Object Reference not set to an instance of object srinivas_chakka ASP.NET 1.0 and 1.1 Professional 0 February 8th, 2006 11:56 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.