Problem
I’m learning C# and am encountering the following problem:
namespace MyDataLayer
{
namespace Section1
{
public class MyClass
{
public class MyItem
{
public static string Property1{ get; set; }
}
public static MyItem GetItem()
{
MyItem theItem = new MyItem();
theItem.Property1 = "MyValue";
return theItem;
}
}
}
}
This is the code I have on a UserControl:
using MyDataLayer.Section1;
public class MyClass
{
protected void MyMethod
{
MyClass.MyItem oItem = new MyClass.MyItem();
oItem = MyClass.GetItem();
someLiteral.Text = oItem.Property1;
}
}
Except when I try to access Property1, everything works properly. Only “Equals, GetHashCode, GetType, and ToString” are available in the intellisense. When I hover my mouse over oItem.Property1, Visual Studio displays the following message.
I’m not sure what this implies; I tried Google but couldn’t figure it out.
Asked by Anders
Solution #1
In C#, unlike VB.NET and Java, you can’t use instance syntax to access static members. What you should do is:
MyClass.MyItem.Property1
remove the static modifier from Property1 to refer to that property (which is what you probably want to do). See my other answer for a conceptual understanding of what static is.
Answered by mmx
Solution #2
Only the type’s name can be used to access static members.
As a result, you must either write,
MyClass.MyItem.Property1
Alternatively, remove the static keyword from Property1’s definition to make it an instance property (which is presumably what you need to do).
Static properties have only one value because they are shared by all instances of their class. There’s no purpose in creating any instances of your MyItem class the way it’s specified right now.
Answered by SLaks
Solution #3
I experienced the same problem, and while it’s been a few years, others may find the following suggestions useful:
Don’t use the word’static’ casually!
Understand the semantics (behavior) and syntax of the term “static” in terms of both run-time and compile-time semantics (behavior).
In MSDN, there are some details on static:
Answered by CarlH
Solution #4
This results in the following error:
MyClass aCoolObj = new MyClass();
aCoolObj.MyCoolStaticMethod();
Here’s how to solve it:
MyClass.MyCoolStaticMethod();
Explanation:
A static method cannot be called from an object instance. The main objective of static methods is that they are not bound to specific instances of objects, but rather that they persist across all instances of that object and/or that they can be used without any instances of that object.
Answered by Andrew
Solution #5
As previously stated, there is no need to use static in this scenario. You can also initialize your property without using the GetItem() method, as shown below:
namespace MyNamespace
{
using System;
public class MyType
{
public string MyProperty { get; set; } = new string();
public static string MyStatic { get; set; } = "I'm static";
}
}
Consuming:
using MyType;
public class Somewhere
{
public void Consuming(){
// through instance of your type
var myObject = new MyType();
var alpha = myObject.MyProperty;
// through your type
var beta = MyType.MyStatic;
}
}
Answered by Alan
Post is based on https://stackoverflow.com/questions/1100009/member-member-name-cannot-be-accessed-with-an-instance-reference