1

I want to be able to print out three types of messages to the console: warnings, errors and success. For that each time I'd have to change the ForegroundColor of the console to either yellow, red or green, print out the message and change the color back. To make this faster I've decided to create a class (let's say Printer), which has three methods: Warning(message), Error(message), Success(message). Now my question is: should Printer be a struct or a class? I dont't plan to have any more fields/methods in this class.

SalysBruoga
  • 875
  • 1
  • 9
  • 21
  • Sounds like it could even be a static class. – juharr Sep 12 '16 at 14:41
  • Not really, at least I don't think so. I understand structs are most commonly used when you only want to group some simple values together. But I've also heard, that if a class does not modify its members, it should be a struct. I'm not sure how true that is, that's why I'm asking :) – SalysBruoga Sep 12 '16 at 14:41
  • Well if each method just changes the color of the console text then prints the method and then changes it back then all the data is static (the color is based on the method and the message is passed in). Or ask yourself if you created multiple `Printer` instances how would they be different? – juharr Sep 12 '16 at 14:43
  • Yes. So is there no difference whether to use a static class or a struct? – SalysBruoga Sep 12 '16 at 14:45
  • 2
    A static class is just saying that you have a method that is independant of any object. A regular class and a struct are both things that contain data and methods that are based on instatiations. The difference being where in memory the data is stored and how it gets passed. But from what you've stated here I see no reason that this wouldn't be a set of methods in a static class. – juharr Sep 12 '16 at 14:48
  • Okay, thanks a lot for the tips :) – SalysBruoga Sep 12 '16 at 14:56
  • One thing to remember the struct is a value type and it is faster and dynamic than the class which is a reference type, moreover struct like POINT, PADDING, SIZE etc returns a value. But in your case i can consider using a class. – Shift 'n Tab Sep 12 '16 at 15:05
  • Well, you can also use `Singleton` pattern.. But i'd definitely recommend against using `struct` as structs are mostly used for representing light-weight objects with many fields/properties of primitive types. An example of this kind of object is `DateTime`. – Fabjan Sep 12 '16 at 15:05
  • 1
    I find this usually helps me decided if I've met all the crtieria for considering a struct: https://msdn.microsoft.com/en-us/library/ms229017(v=vs.110).aspx – TVOHM Sep 12 '16 at 15:08
  • 1
    Consider locking if multiple threads can use the log printer. Otherwise you might end up with wrong foreground colors. – Good Night Nerd Pride Sep 12 '16 at 15:31

1 Answers1

4

It actually sounds like this should be a static class.

public static class Printer
{
    public static void Warning(string message)
    {
        var currentColor = Console.ForegroundColor;
        Console.ForegroundColor = ConsoleColor.Yellow;
        Console.WriteLine(message);
        Console.ForegroundColor = currentColor;
    }

    //Other similar methods here
}

Then you'd call it like this

Printer.Warning("This is a warning");
juharr
  • 31,741
  • 4
  • 58
  • 93