Question

I have a method that takes a string argument (a file path), and i wanna make it acessible to all forms.

I know that when i use

public static methodname(string path) { //code } 

it will work on all forms as long as i call it, and when i dont have static there i have to instantiate it.. the thing is.. why this difference? What does it really means to instantiate? I've read a few topics about it, but i couldn't quite understand how, can you take the time to explain it to a amateur beginner?

Was it helpful?

Solution 2

You use non-static classes or object oriented classes, if you want to maintain a state.

When you use static classes, there isn't any state.

For example:

class HelloWorld
{
    public string Name {get;set;}
    public void SayHello()
    {
       Console.WriteLine(Name + " hello!");
    }
    public void SayHi()
    {
       Console.WriteLine(Name + " hi!");
    }
}

You would use the above like this:

HellowWorld obj = new HelloWorld();
obj.Name = "John";
obj.SayHi(); 
obj.SayHello();

If that was a static class:

static class HelloWorld
{
    public static void SayHello(string Name)
    {
       Console.WriteLine(Name + " hello!");
    }
    public static void SayHi(string Name)
    {
       Console.WriteLine(Name + " hi!");
    }
}

You would call the above like this:

HelloWorld.SayHello("John");
HelloWorld.SayHi("John");

As you can see, you are repeating yourself.

So it all depends on how you want the class to behave. If you want do and forget, then static class is your friend. If you don't want it to forget something, then use Objects

OTHER TIPS

This tells the compiler that he method is entirely self-contained, and does not rely on or utilize any object state, or instance-based properties or fields of the class it is defined in. As a consequence, you do not need to instantiate an instance of the type (class or struct) in order to use this method. It becomes like a global function or subroutine which you can call simply by name, (you must use the class/struct name and method name) without any preparation.

to call non-static method MyMethod() on class foo

var f = new foo();
f.MyMethod();

to call static method MyMethod() on class foo

foo.MyMethod();

You can define static methods in a non-static class, (if the method does not rely on any object state doesn't mean the object does not have any state...). but you cannot put non-static methods in a static class (if the method requires access to object state, then it obviously can't be in a class that has no state).

Now you can write a method that does not require object state, and neglect to declare it as static. Then it would require the same usage syntax like a non-static method even though it is functionally static. That is an error the compiler will accept (although many tools like Resharper will warn you about this).

This is classic programming. Static methods are used when all the data you need is from the parameter. Also static methods are called upon a class. When you make a method static then it uses instance variables to do something. Instantiontain allows a user to initialize all fields in that class then the user can call a method upon that object that behind the scenes uses instance variables to do something.

As you said, a non-static method means that it belows to an object (so you have to instantiate one in order to use the method. On the other hand, a static method below to the class, so when you call it, it has nothing to do with an object.

For example:

class C{
    static int f(int i){return i+1;}
}

C.f(2); // returns 3

You only can do it if the method is static, it is, it belongs to a class, not an object. If f() were not be static, you have to instantiate one object to use it:

class C{
    int f(int i){return i+1;}
}

C.f(2); // invalid!
C c = new c(); c.f(2); // returns 3

On the other hand, this code is invalid:

class C{
    int a;
    static int f(){return a;}
}

C.f(); // Invalid: each instance of C has it own attribute a.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top