views:

226

answers:

6

Hello SO,

I'm very new to OOP and am trying my hardest to keep things strictly class based, while using good coding priciples.

I'm a fair ways into my project now and I have a lot of general use methods I want to put into an utilities class. Is there a best way to create an utilities class?

public class Utilities
{
    int test;

    public Utilites()
    {
    }

    public int sum(int number1, int number2)
    {
        test = number1+number2;
    }
    return test;
}

After creating this Utilities class, do I just create an Utilities object, and run the methods of my choosing? Do I have this Utilities class idea correct?

+14  A: 

You should make it a static class, like this:

public static class Utilities {
    public static int Sum(int number1, int number2) {
        return number1 + number2;
    }
}

int three = Utilities.Sum(1, 2);

The class should (usually) not have any fields or properties. (Unless you want to share a single instance of some object across your code, in which case you can make a static read-only property.

SLaks
Sigh gotta read the tags. His example code would have compiled in Java ;)
SB
Wherever it is meaningful, you can write extension methods. For extension methods, you can refer http://msdn.microsoft.com/en-us/library/bb383977.aspx
mohang
@SB I think your previous example was correct. you'd just need to exchange 'final' for 'sealed' :)
Rory Becker
+1  A: 

Best is to make the functions not reliable to members of a class. Therefore you can make the functions static.

Better is to make the functions an extension method of a type. see here for example

PoweRoy
+2  A: 

You will be better off using a static class with static methods. Then you won't need to instantiate your utilities class to use it. It will look something like this:

public static Utilites
{
  public static int sum(int number1, int number2)
  {
     test = number1+number2;
     return test;
  }
}

Then you can use it like this:

int result = Utilites.sum(1, 3);
Jason Webb
This is correct and the correct definition of a utilities class.The other method is creating a utilities instance/object, which for your information (all of you) is not "wrong/bad" at all.When a class needs to be instantiated to be useful, you might want a global utilities object.An example of this is the Application object in Delphi and Lazarus.
Christian Sciberras
True there are times it would be useful. However in the case he showed us, adding two numbers, it made more sense to me to use `static` since there is no start-up cost every time you use the function. If you were doing a lot of work, for example opening a database connection each time, then yeah instantiating a utilities class does make sense.
Jason Webb
A: 

do this.

public static class Utilities
{
    int test;

    public static int sum(int number1, int number2)
    {
        test = number1+number2;
        return test;
    }
}

That way you use it like

int result = Utilities.sum(1, 2);
jamone
+2  A: 

If you are working with .NET 3.0 or above, you should look into extension methods. They allow you to write a static function that will act against a particular type, like Int32, while seeming to be a menthod on that object. So then you could have: int result = 1.Add(2);.

Try this tutorial out. It might just show you another way. ;)

http://www.switchonthecode.com/tutorials/csharp-tutorial-extension-methods

Lance May
A: 

While new to OOP and trying to get a handle on best practices, it may be a good idea to try to avoid utility classes. You could redesign your class like

public class Sum
{
    private int _result;

    public int Result {
       get {
           return _result;
       }
    }

    public Sum(int startNum) {
        _results = startNum;
    }

    public void Add(int num) {
        _result += num;
    }
}

And is called like:

Sum sum = new Sum(1);
sum.add(2);
int result = sum.Result;

It'll be good practice until further experience with OOP can help you examine the trade-offs of using an utility class vs pure OOP principles.

tylermac