2

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#?

Dementor
  • 241
  • 1
  • 6
  • 12
  • 1
    use `myClass.Afis()` or remove the `static` keyword in the method declaration – yas4891 Feb 08 '12 at 09:00
  • 1
    See [this question](http://stackoverflow.com/questions/2308681/what-is-the-difference-between-a-static-variable-in-c-vs-c) for a comparison between the C++ and C# meanings of `static`. – PHeiberg Feb 08 '12 at 09:03

7 Answers7

3

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.

Neil Knight
  • 47,437
  • 25
  • 129
  • 188
1

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".

Community
  • 1
  • 1
Rob Levine
  • 40,328
  • 13
  • 85
  • 111
1

Static methods are called through the class itself, not an instance of the class.

static void Main()
{
    myClass m = new myClass();
    myClass.Afis();
}
Robert Rouhani
  • 14,512
  • 6
  • 44
  • 59
1

That's right - it's a static function and therefore is called like:

myClass.Afis();
Ancallan
  • 111
  • 4
1

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.

Adam Houldsworth
  • 63,413
  • 11
  • 150
  • 187
TomTom
  • 61,059
  • 10
  • 88
  • 148
1

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.

Hadas
  • 10,188
  • 1
  • 22
  • 31
0

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.

PVitt
  • 11,500
  • 5
  • 51
  • 85