Reputation: 251
Can i access a method from a instance of the class? Example:
class myClass
{
private static int n = 0;
public static myClass()
{ n = 5;}
public static void Afis()
{
Console.WriteLine(n);
}
}
and in the void Main:
static void Main()
{
myClass m = new myClass();
m.Afis();
}
This gives me: cannon be accessed with an instance referece
. Is it because i declared the function static? If that is so when should I use static and when not because in c++ if i declare something with static it just initialize once. Is that the case with c#?
Upvotes: 2
Views: 152
Reputation: 11760
You marked your method as static. Thus you have to access it via the type.
Static methods are used when the method refers to the type and does not use instance information. String.IsNullOrEmpty(string s)
is such a static method. It belongs to the string class but does not need an instance environment. Whereas the ToString()
method that every object inherits from object needs an instance context to define what to express as a string.
Upvotes: 0
Reputation: 41298
Yes - as you suspect - you cannot access an static method via an instance of the object, only via the type itself.
In other words:
myClass.Afis();
Naturally - the converse is also true; you cannot access an instance method via the type either.
You can find out more about when to, and no to, use static
in other questions such as this one, but I would say that static
limits certain desirable things like testability and polymorphism (to name just a couple), and so shouldn't be your "default position".
Upvotes: 1
Reputation: 48547
You need to use the class name rather than your variable name to access the static
method.
myClass.Afis();
I've attached a link to the Static Classes and Static Class Members programming guide as you've asked when you should use them and not use them.
A little exert from the programming guide:
A static class can be used as a convenient container for sets of methods that just operate on input parameters and do not have to get or set any internal instance fields. For example, in the .NET Framework Class Library, the static System.Math class contains methods that perform mathematical operations, without any requirement to store or retrieve data that is unique to a particular instance of the Math class.
Upvotes: 3
Reputation: 10374
Static method is method who connect to the class ad not to instance. You use this if you want that every instance can use the same method. For example if I want to count the instances of class I use static property. To access static methods and properties you have to use the name of the class and not the name of instance.
Upvotes: 1
Reputation: 62093
Is it because i declared the function static?
Exactly.
If that is so when should I use static
Guess what - from the error: when you do not need to access instance variables.
because in c++ if i declare something with static it just initialize once. Is that the case with c#?
If you mean a static constructor - yes.
Basically what you have there is not a "class", but a static class that can not be instantiated at all.
Upvotes: 1
Reputation: 111
That's right - it's a static function and therefore is called like:
myClass.Afis();
Upvotes: 1
Reputation: 14678
Static methods are called through the class itself, not an instance of the class.
static void Main()
{
myClass m = new myClass();
myClass.Afis();
}
Upvotes: 1