locked
Calling methods of a class without creating an instance of it

    Question

  • Hi,

               I'm sure this probably has been answered before, but somehow I couldn't find it, so really sorry about that. But how do you call methods of a class without having to create an instance of it? I mean, in my project I have something like:

           public class FileOperations{
                   
            public void read_config_file(...) {

                (...)
            }

       }

           And I want to call the read_config_file(who doesn't depend on anything but the filename and a property where it'll write the config values) without having to do:

           FileOperations fileOp = new FileOperations();
           fileOp.read_config_file();

           I was a poor (poor like in noob) Java programmer but I used humm I believe it were static methods in abstract classes, something like that, don't really remember anymore. Could someone shed a light please? Sorry again, I know this has to be posted already somewhere I just couldn't find it (poor search skills too? :) ) Cheers,

    Tiago
    Thursday, December 28, 2006 10:07 AM

Answers

  • Hi,

    If you want to call a method without having to create an instance of the method's class, you need to create the method as static. Every field that the method uses, must be static to.

    Example:
    public class
    SimpleStaticClass
    {
       private static int
    _counter = 100;

       public static string
    GetMessage()
       {
          return string.Format("Counter: {0}"
    , _counter);
      
    }
    }

    Calling class:
    private void button1_Click(object sender, EventArgs
    e)
    {
       string x = SimpleStaticClass
    .GetMessage();
    }

    Greetz,

    Geert

     

    Geert Verhoeven
    Consultant @ Ausy Belgium

    My Personal Blog

    Thursday, December 28, 2006 12:59 PM

All replies

  • Hi Tiago,

    You need to make your method static.

    Like this:
    public static void read_config_file(string input)
    {
      
    // Do something
    }

    Greetz,

    Geert

     

    Geert Verhoeven
    Consultant @ Ausy Belgium

    My Personal Blog

    Thursday, December 28, 2006 10:58 AM
  • Thank you for your reply,

                  I've tried it before but couldn't go around some errors that it threw, something like:

                 "An object reference is required for the nonstatic field, method, or property         'FileOperations.FileOp.create_streamreader(string)'"


                 Being that FileOperations is the namespace, FileOp the class name and create_streamreader a nonstatic method I created to create the streamreader and handle it's exceptions, and such. What am I doing wrong?
    Thursday, December 28, 2006 12:42 PM
  • Hi,

    If you want to call a method without having to create an instance of the method's class, you need to create the method as static. Every field that the method uses, must be static to.

    Example:
    public class
    SimpleStaticClass
    {
       private static int
    _counter = 100;

       public static string
    GetMessage()
       {
          return string.Format("Counter: {0}"
    , _counter);
      
    }
    }

    Calling class:
    private void button1_Click(object sender, EventArgs
    e)
    {
       string x = SimpleStaticClass
    .GetMessage();
    }

    Greetz,

    Geert

     

    Geert Verhoeven
    Consultant @ Ausy Belgium

    My Personal Blog

    Thursday, December 28, 2006 12:59 PM
  • Oh I see. Thanks for the input, I'll give it a try and hopefully it will work fine :) Thanks again, I'll get back here if needed.
    Thursday, December 28, 2006 1:21 PM
  • Glad to help. If the solution above works, can you please mark the thread as answered?

    Greetz,

    Geert

    Geert Verhoeven
    Consultant @ Ausy Belgium

    My Personal Blog

    Thursday, December 28, 2006 2:13 PM
  • I always do :) I'll get back to this tomorrow...
    Thursday, December 28, 2006 2:15 PM
  • It did work out. Thanks again,

    Tiago
    Friday, December 29, 2006 9:15 AM