0

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?

César Amorim
  • 367
  • 3
  • 16
  • 3
    This question appears to be off-topic because it is about reading a good book or tutorial. – nvoigt May 10 '14 at 14:39
  • Please reefer the link http://msdn.microsoft.com/en-us/library/79b3xss3.aspx – Gun May 10 '14 at 14:40
  • Read [this SO question](http://stackoverflow.com/questions/2671496/java-when-to-use-static-methods). It is same in Java. – Farhad Jabiyev May 10 '14 at 14:44
  • Going strictly by the title: here is [C# for Dummies](http://www.amazon.co.uk/For-Dummies-Stephen-R-Davis/dp/0764508148) – Abhitalks May 10 '14 at 14:44
  • 1
    The problem is that all of the sources i've found show a meaningful language, so for a beginner it's not easily understood. Thanks anyway – César Amorim May 10 '14 at 17:50
  • 1
    It's also strange that a few users flag my question, and tell me to go read some books, while others answer in a simple and clear language accessible to professionals of other areas, which i believe is the constructive thing to do. It's not the first time i find some keyboard warriors when i try to clarify some basic elements. Please forgive me, for I might have sinned. – César Amorim May 10 '14 at 18:58

4 Answers4

3

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

Charles Bretana
  • 143,358
  • 22
  • 150
  • 216
1

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.

rush2sk8
  • 362
  • 6
  • 16
1

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

Amit Joki
  • 58,320
  • 7
  • 77
  • 95
  • It's a very neat answer, there's only a thing i don't understand, what is the advantage in using a non-static class? What kind of behaviour could i change using it? It seems more complex so i'm guessing it has more potential.Because in those examples there's no limitation on using static or non-static @Amit Joki – César Amorim May 10 '14 at 17:58
  • Also, why can't i use a static method to make a combobox or a textbox public? what do these items have, that make them non-static? – César Amorim May 10 '14 at 19:53
0

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.

eugenioperez
  • 627
  • 7
  • 15