CS0120: An object reference is required for the nonstatic field, method, or property 'foo'
It looks like you are calling a non static member (a property or method, specifically setTextboxText
) from a static method (specifically SumData
). You will need to either:
Make the called member static also:
static void setTextboxText(int result)
{
// Write static logic for setTextboxText.
// This may require a static singleton instance of Form1.
}Create an instance of
Form1
within the calling method:private static void SumData(object state)
{
int result = 0;
//int[] icount = (int[])state;
int icount = (int)state;
for (int i = icount; i > 0; i--)
{
result += i;
System.Threading.Thread.Sleep(1000);
}
Form1 frm1 = new Form1();
frm1.setTextboxText(result);
}Passing in an instance of
Form1
would be an option also.Make the calling method a non-static instance method (of
Form1
):private void SumData(object state)
{
int result = 0;
//int[] icount = (int[])state;
int icount = (int)state;
for (int i = icount; i > 0; i--)
{
result += i;
System.Threading.Thread.Sleep(1000);
}
setTextboxText(result);
}
More info about this error can be found on MSDN.
An object reference is required for a non-static field, method, or property
Your Main
method is static and you can access only static members of the same class from a static method. All you need to do to make your EasyExploits.Module
static as well:
private static readonly EasyExploits.Module module = new EasyExploits.Module();
an object reference is required for the non-static field method or property 'page.request'
Your issue here is that you made this method static. Static means that it's not tied to a specific instance. The Request
property that this code is referring to is an instance property. This means that this property (can) have a different value for each instance.
ASP.NET will create a new instance of this class for each request that enters, this will make it easier to share information like the Request
, Response
, Context
, ... for a specific request, so you have to think less about tying this all together.
C# error: An object reference is required for the non-static field, method, or property
The Main
method is Static. You can not invoke a non-static method from a static method.
GetRandomBits()
is not a static method. Either you have to create an instance of Program
Program p = new Program();
p.GetRandomBits();
or make
GetRandomBits()
static.
C# an object reference is required for the non-static field, method, or Property - Speech Synthesis
iSpeech
is a class, but you need an instance of the class in order to use non-static methods.
Think of it like List<string>
. You can't call
List<string>.Add("Hello");
because List<string>
is a class, like the blueprint for creating an object. (You would get the exact same error.) You would need to create an instance of that class to use it:
var myList = new List<string>();
myList.Add("Hello");
So in the case of your class, iSpeech
, if you declared
var mySpeechThing = new iSpeech();
then mySpeechThing
would be a variable representing an instance of iSpeech
, and then you could do
await mySpeechThing.SpeakTextAsync("test speech", this.uiMediaElement);
Sometimes a class has methods that can be called without modifying the state of the object (like calling Add
on a List<string>
changes its state by adding a string to it.) We declare those as static
methods. They belong to the class, not to an instance of the class.
To do that you would put the keyword static
in the method declaration like this:
public static async Task SpeakTextAsync(string text, MediaElement mediaElement)
Then you could use it the way you were trying to.
static
methods cannot access non-static class properties or methods. And although some may disagree, it's generally better practice not to use static
methods. They're not evil, but until you're more familiar I'd lean the other way.
Related Topics
How to Save Picturebox.Image to File
C# Split String and Remove Empty String
Owin Security - How to Implement Oauth2 Refresh Tokens
How to Compare Unicode Characters That "Look Alike"
Are 2 Dimensional Lists Possible in C#
Send Keys Through Sendinput in User32.Dll
Why Is It Impossible to Override a Getter-Only Property and Add a Setter
Keep Window on Top and Steal Focus in Winforms
How to Hide Desktop Icons Programmatically
Dataview.Sort - More Than Just Asc/Desc (Need Custom Sort)
Equivalence of "With...End With" in C#
How to Create a More User-Friendly String.Format Syntax
What's a Good Threadsafe Singleton Generic Template Pattern in C#