an object Reference is required for the for nonstatic field, metho

J

JB

Hello

Is it possible and if so how, to call a method in a class that
is not called from a control on a form? When you create a button on a form
if you
create a button click event and type someproc(); and there is a method in
the form code module that looks like this:
public void someproc()
{
MessageBox.Show("hello world");
}
the method will execute and show "hello world"

But if a module that does not have a button control calls another module,
that called module will exeute but its method won't call any other methods
that
it contains within itself.

For example: lets call below "mod1" and I marked a ***line 16 in it which
calls
"mod2" in a module below it. When mod2 executes it has a method named
RangeProc.
But when RangProc tries to get executed it says "an object Reference is
required for the nonstatic field, method, or property ...RangeProc()


****** mod1:
using System;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using Microsoft.VisualStudio.Tools.Applications.Runtime;
using Word = Microsoft.Office.Interop.Word;
using Office = Microsoft.Office.Core;
using System.Data.SqlClient;
using System.Collections;
using System.Collections.Generic;
using System.Threading;
namespace mDivisionsProj
{
public partial class ThisDocument
{
private void ThisDocument_Startup(object sender, System.EventArgs e)
{
List<object> DivisionTbl = new List<object>();
***line 16: DivisionTbl = DivData.DivisionList();
}
private void ThisDocument_Shutdown(object sender, System.EventArgs e)
{
}
#region VSTO Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InternalStartup()
{
this.button1.Click += new System.EventHandler(this.button1_Click);
this.Shutdown += new
System.EventHandler(this.ThisDocument_Shutdown);
this.Startup += new
System.EventHandler(this.ThisDocument_Startup);
}
#endregion
}
}


******* mod2
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.Threading;
namespace mDivisionsProj
{
public class DivData
{
private string PDist;
private int ADDist;
private int EDD;
public DivData(string PD, int AD, int ED)
{
PD = PDist;
AD = ADDist;
ED = EDDist;
}

public static List<object> DivisionList()
{
List<object> GetDivList = new List<object>();
GetDivList = GetDivData.rdDivData();
if ((holdDiv == currDiv) & (holdAD < currAD))
{
if (isRange == "Y")
{
nuRange = FromED.ToString() + " To " + ToED;
}
else
{
FromED = holdED;
nuRange = FromED.ToString();
}
RangeProc();
isRange = "N";
}
ToED = " ";
holdDiv = currDiv;
holdAD = currAD;
holdED = currED;
compHold = Convert.ToInt32(holdED);

}
return GetDivList;
}
public void RangeProc()
{
GetDivList.Add(holdAD.ToString());
GetDivList.Add(nuRange);

}
}
}
 
A

Aneesh Pulukkul [http://dotnet-revolutions.blogspo

Hello

    Is it possible and if so how, to call a method in a class that
is not called from a control on a form?  When you create a button on a form
if you
create a button click event and type someproc(); and there is a method in
the form code module that looks like this:
public void someproc()
{
    MessageBox.Show("hello world");}

the method will execute and show "hello world"

But if  a module that does not have a button control  calls another module,
that called module will exeute but its method won't call any other methods
that
it contains within itself.

For example: lets call below "mod1" and I marked a ***line 16  in it which
calls
"mod2"  in a module below it. When mod2 executes it has a method named
RangeProc.
But when RangProc tries to get executed it says "an object Reference is
required for the nonstatic field, method, or property ...RangeProc()

****** mod1:
using System;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using Microsoft.VisualStudio.Tools.Applications.Runtime;
using Word = Microsoft.Office.Interop.Word;
using Office = Microsoft.Office.Core;
using System.Data.SqlClient;
using System.Collections;
using System.Collections.Generic;
using System.Threading;
namespace mDivisionsProj
{
    public partial class ThisDocument
    {
        private void ThisDocument_Startup(object sender, System.EventArgs e)
        {
            List<object> DivisionTbl = new List<object>();
***line 16:            DivisionTbl = DivData.DivisionList();
        }
        private void ThisDocument_Shutdown(object sender, System.EventArgs e)
        {
        }
        #region VSTO Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InternalStartup()
        {
            this.button1.Click += new System.EventHandler(this.button1_Click);
            this.Shutdown += new
System.EventHandler(this.ThisDocument_Shutdown);
            this.Startup += new
System.EventHandler(this.ThisDocument_Startup);
        }
        #endregion
    }

}

******* mod2
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.Threading;
namespace mDivisionsProj
{
    public class DivData
    {
        private string PDist;
        private int ADDist;
        private int EDD;
        public DivData(string PD, int AD, int ED)
        {
            PD = PDist;
            AD = ADDist;
            ED = EDDist;
        }

        public static List<object> DivisionList()
        {
            List<object> GetDivList = new List<object>();
            GetDivList = GetDivData.rdDivData();
                if ((holdDiv == currDiv) & (holdAD < currAD))
                {
                    if (isRange == "Y")
                    {
                        nuRange = FromED.ToString() + " To " + ToED;
                    }
                    else
                    {
                        FromED = holdED;
                        nuRange = FromED.ToString();
                    }
                    RangeProc();
                    isRange = "N";
                }
                ToED = " ";
                holdDiv = currDiv;
                holdAD = currAD;
                holdED = currED;
                compHold = Convert.ToInt32(holdED);

            }
            return GetDivList;
         }
        public void RangeProc()
        {
            GetDivList.Add(holdAD.ToString());
            GetDivList.Add(nuRange);

        }
   }

}

GetDivList is local to the DivisionList method and is not accessible
outside. It should throw an error at compile time itself.

Also RangeProc is an instance method and cannot be called inside a
static method. Make RangeProc static and GetDiveList a static member
of the class.

Regards,

Aneesh
 
G

Göran Andersson

JB said:
Hello

Is it possible and if so how, to call a method in a class that
is not called from a control on a form? When you create a button on a form
if you
create a button click event and type someproc(); and there is a method in
the form code module that looks like this:
public void someproc()
{
MessageBox.Show("hello world");
}
the method will execute and show "hello world"

But if a module that does not have a button control calls another module,
that called module will exeute but its method won't call any other methods
that
it contains within itself.

For example: lets call below "mod1" and I marked a ***line 16 in it which
calls
"mod2" in a module below it. When mod2 executes it has a method named
RangeProc.
But when RangProc tries to get executed it says "an object Reference is
required for the nonstatic field, method, or property ...RangeProc()

That is because DivisionList is a class member (because it's static),
and RangProc is an instance member. You can use class members without
creating an instance of the class, but you can not use instance members
without creating an instance of the class.

Further comments in the code:
****** mod1:
using System;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using Microsoft.VisualStudio.Tools.Applications.Runtime;
using Word = Microsoft.Office.Interop.Word;
using Office = Microsoft.Office.Core;
using System.Data.SqlClient;
using System.Collections;
using System.Collections.Generic;
using System.Threading;
namespace mDivisionsProj
{
public partial class ThisDocument
{
private void ThisDocument_Startup(object sender, System.EventArgs e)
{
List<object> DivisionTbl = new List<object>();
***line 16: DivisionTbl = DivData.DivisionList();
}
private void ThisDocument_Shutdown(object sender, System.EventArgs e)
{
}
#region VSTO Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InternalStartup()
{
this.button1.Click += new System.EventHandler(this.button1_Click);
this.Shutdown += new
System.EventHandler(this.ThisDocument_Shutdown);
this.Startup += new
System.EventHandler(this.ThisDocument_Startup);
}
#endregion
}
}


******* mod2
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.Threading;
namespace mDivisionsProj
{
public class DivData
{
private string PDist;
private int ADDist;
private int EDD;

I suppose that should be named EDDist?

You should try to use more descriptive names. I have no idea what a "P",
an "AD" or an "ED" is supposed to be...
public DivData(string PD, int AD, int ED)
{
PD = PDist;
AD = ADDist;
ED = EDDist;

The destination of the assignment goes on the left side:

PDist = PD;
ADDist = AD;
EDDist = ED;
}

public static List<object> DivisionList()
{
List<object> GetDivList = new List<object>();

There is no reason to create a list if you are going to replace it with
a different list in the next line.
GetDivList = GetDivData.rdDivData();
if ((holdDiv == currDiv) & (holdAD < currAD))

I see a lot of variables here that are not declared in the method...
{
if (isRange == "Y")
{
nuRange = FromED.ToString() + " To " + ToED;
}
else
{
FromED = holdED;
nuRange = FromED.ToString();
}
RangeProc();

As RangeProc is an instance member, you can't call it from a static
method without having a reference to an instance of the class.
isRange = "N";
}
ToED = " ";
holdDiv = currDiv;
holdAD = currAD;
holdED = currED;
compHold = Convert.ToInt32(holdED);

}

Your starting and ending brackets doesn't match. Did you remove something?
return GetDivList;
}
public void RangeProc()
{
GetDivList.Add(holdAD.ToString());

The GenDiList variable is a local variable in another method. You can
not reach that from here. You either have to pass the variable as a
parameter to the method, or make it a member variable. The latter of
course means that you have to create an instance of the class, and
DivisionList has to be an instance member also.
 
J

JB

Hello Goran

Then next time I will try to use create one List<object> and use that
same List<object> throughout solution. I have been calling List<objects>
because I thought each module needed its own just like each module needs its
own variables.

I have been trying to send only the parts of the code that produce
the error because the code I have been writing has been too long. That is
why I am starting to create methods and call them. So I thank you for
explaining what I was doing wrong when I create my methods. Now my methods
work. I followed your instructions and changed the List<object> into a
static List<object>
and changed the members in the List<object> to static members and
the code is working now.
 
J

JB

Hello Aneesh

I have been trying to send only the parts of the code that produce
the error because the code I have been writing has been too long. That is
why I am starting to create methods and call them. So I thank you for
explaining what I was doing wrong when I create my methods. Now my methods
work. I followed your instructions and changed the List<object> into a
static List<object>
and changed the members in the List<object> to static members and
the code is working now.
--
JB


Aneesh Pulukkul [http://dotnet-revolutio said:
Hello

Is it possible and if so how, to call a method in a class that
is not called from a control on a form? When you create a button on a form
if you
create a button click event and type someproc(); and there is a method in
the form code module that looks like this:
public void someproc()
{
MessageBox.Show("hello world");}

the method will execute and show "hello world"

But if a module that does not have a button control calls another module,
that called module will exeute but its method won't call any other methods
that
it contains within itself.

For example: lets call below "mod1" and I marked a ***line 16 in it which
calls
"mod2" in a module below it. When mod2 executes it has a method named
RangeProc.
But when RangProc tries to get executed it says "an object Reference is
required for the nonstatic field, method, or property ...RangeProc()

****** mod1:
using System;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using Microsoft.VisualStudio.Tools.Applications.Runtime;
using Word = Microsoft.Office.Interop.Word;
using Office = Microsoft.Office.Core;
using System.Data.SqlClient;
using System.Collections;
using System.Collections.Generic;
using System.Threading;
namespace mDivisionsProj
{
public partial class ThisDocument
{
private void ThisDocument_Startup(object sender, System.EventArgs e)
{
List<object> DivisionTbl = new List<object>();
***line 16: DivisionTbl = DivData.DivisionList();
}
private void ThisDocument_Shutdown(object sender, System.EventArgs e)
{
}
#region VSTO Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InternalStartup()
{
this.button1.Click += new System.EventHandler(this.button1_Click);
this.Shutdown += new
System.EventHandler(this.ThisDocument_Shutdown);
this.Startup += new
System.EventHandler(this.ThisDocument_Startup);
}
#endregion
}

}

******* mod2
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.Threading;
namespace mDivisionsProj
{
public class DivData
{
private string PDist;
private int ADDist;
private int EDD;
public DivData(string PD, int AD, int ED)
{
PD = PDist;
AD = ADDist;
ED = EDDist;
}

public static List<object> DivisionList()
{
List<object> GetDivList = new List<object>();
GetDivList = GetDivData.rdDivData();
if ((holdDiv == currDiv) & (holdAD < currAD))
{
if (isRange == "Y")
{
nuRange = FromED.ToString() + " To " + ToED;
}
else
{
FromED = holdED;
nuRange = FromED.ToString();
}
RangeProc();
isRange = "N";
}
ToED = " ";
holdDiv = currDiv;
holdAD = currAD;
holdED = currED;
compHold = Convert.ToInt32(holdED);

}
return GetDivList;
}
public void RangeProc()
{
GetDivList.Add(holdAD.ToString());
GetDivList.Add(nuRange);

}
}

}

GetDivList is local to the DivisionList method and is not accessible
outside. It should throw an error at compile time itself.

Also RangeProc is an instance method and cannot be called inside a
static method. Make RangeProc static and GetDiveList a static member
of the class.

Regards,

Aneesh
 

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