. Advertisement .
..3..
. Advertisement .
..4..
In the below instruction, we shall guide you thoroughly how to deal with the error “an object reference is required for the non-static field, method, or property.”
Sounds like a load of work to get done. Let’s check out whether or not it is a dead end to walk through!
An Object Reference Is Required For Non-Static Field, Method, Or Property – Example
As the name refers, a non-static method, field, or property can only be used once an object instance has been created. Have a look at the situation that triggers such a problem!
First of all, my non-static field is:
private IDictionary<string, string> _mappings =
new Dictionary<string, string>(StringComparer.InvariantCultureIgnoreCase)
Let’s say I wish to employ the following interior of an action:
public static string GetMimeType(string extension)
{
//...
return _mappings.TryGetValue(extension, out mime) ?
mime : "application/octet-stream";
}
This leads to the compiler being disgruntled and ending up with the warning as below:
An object reference is required for non-static field, method, or
property in the return statement.
How To Fix The Error An Object Reference Is Required For Non-Static Field, Method, Or Property
We are aware that this glitch is making you bored, thus we are here to assist. Take a moment to breathe deeply before looking at the solution to your issue.
We have numerous solutions for this dilemma, but the second one is more advisable to use because it has been tried and tested and will always work for you.
Method #1: Make The _mappings Variable Static
The compiler seems to be quite clear in this case: The _mappings variable is not declared static (a non-static or instance field/variable), despite the fact that your GetMimeType method is a static method.
Here is neatly what you need to do if you still wish to use the mappings variable as it is shown above:
private static IDictionary<string, string> _mappings = new Dictionary<string, string>(StringComparer.InvariantCultureIgnoreCase)
A little reminder on the side: Be sure that this is actually the behavior you desire.
A static member can overwrite the existing data and indicates that all instances will share the same mappings variable. Hence, you should convert your method to an instance method (by eliminating the static term) if you only want one mapping variable per class.
Method #2: Make The _mappings Variable Static
There are two options when static members cannot access instance members.
- The field should be static (add the static keyword).
For example:
// CS0120_3.cs
using System;
public class MyClass
{
public static void Main()
{
DoIt("Hello There"); // CS0120
}
private static void DoIt(string sText)
{
Console.WriteLine(sText);
}
}
- Create an instance method for your current method (remove the static keyword).
For example:
// CS0120_2.cs
// CS0120 expected
using System;
public class MyClass
{
public static void Main()
{
var anInstanceofMyClass = new MyClass();
anInstanceofMyClass.TestCall();
}
public void TestCall()
{
}
}
Whether or not the field should be shared across all instances will determine which option you should select.
The Bottom Line
The instruction of how to solve the error “an object reference is required for the non-static field, method, or property is outlined above”. Hopefully, this article can be of great help to you. See then!
Leave a comment