public vs public static Methods

c#, static

Solution

For a `public static` method, you don't need a reference to an object. The method is static and can be accessed on `class` level.

If you can't access a public method, then you need a reference to the object, then you can.

public class AClass
{
    public void DoSomething() {}
    public static void DoSomethingElse() {}
}

You can use them as follows:

AClass.DoSomethingElse(); // no object reference required
AClass.DoSomething(); // will give compiler error, since you have no object reference.
var anObject = new AClass();
anObject.DoSomething(); // will work fine.
anObject.DoSomethingElse(); // compile error (thx hvd).

Problem

Having read the access modifiers in C# progamming tutorial, I come to conclusion that defining a method `public` is enough for it to be "seen" from another `Form` of the same namespace. However, in practise whenever I tried to implement this, I also had to define the method as `static` in order for it to be referenced from other `Forms` of the same namespace. Am I loosing something? I am doing somethning wrong?

Original source

Related problems