cis
cis

Reputation: 1

Not sure which way to declare my constants in C#

I would like to know how to declare a "global" variable for a class. That is I want that this variable is available everywhere just in this class and not in the other classes.

What is the best form to declare them? or when do I use each of these forms? I would like to have it OO.

1st Form:

enter code here
private const string _Column1= "Names";
private const string _Column2= "Numbers";
private const string _Column3= "Alarms";

2nd Form:

private enum enumColumnNames 
{
    Names,         // integer value = 0
    Numbers,        // 1
    Alarms,         // 2
};

3rd Form:

internal sealed class clsColumnNames
{
    public static readonly clsColumnNames Column1 = new clsColumnNames("Names");
    public static readonly clsColumnNames Column2 = new clsColumnNames("Numbers");
    public static readonly clsColumnNames Column3 = new clsColumnNames("Alarms");

    private clsColumnNames(string value)
    {   Value = value;
    }
    public string Value { get; private set; }
}

4th Form:

internal sealed class clsColumnNames
{
    public static readonly string Column1 = "Names";
    public static readonly string Column2= "Numbers";
    public static readonly string Column3= "Alarms";
}

Thanks!

Cis

Upvotes: 0

Views: 138

Answers (4)

jdnew18
jdnew18

Reputation: 165

The 1st form is the best for the following reasons:

  • Simplest
  • Less typing

The 2nd form will just result in lots of needless extra code/typing (i.e., enumColumnNames.Names.ToString(), just to get the value of your variable. I've already worn my fingers out just typing that once).

For the 3rd form, let's break it down:

  • Making the class internal isn't really changing anything since your variables are private
  • Making the class sealed just means the class cannot be inherited from; again, this is not changing anything since your variables are private
  • Making your variables static just means that they won't show up in an object of the class; again, not really doing anything
  • Making your variables readonly just means that they can only be set in a constructor
  • There is no point in having a property for a variable that you intend to be constant; Properties are generally used to make sure that a variable is being set/gotten properly

And for the 4th form, same gripes as with the 3rd, just minus the last gripe.

EDIT: Since these particular variables you are asking about are supposed to be column names, I would actually recommend Amorphis' answer (sorry Amorphis, I don't have enough rep to upvote your answer just yet). If your global variables are unrelated to each other (i.e., a variable for the name of a webpage vs. a variable for the number of characters allowed in a text field are nice and dissimilar), then you ought to use the first form.

Upvotes: 0

David
David

Reputation: 10708

Since these are constants, I would recommend the enum approach. As mentioned by Amorphis, this will aloow you to get the literal value viw .ToString(). This also allows you to pass around your strings as a parameter boxed to the given values by using the enum type as a parameter, rather than a string which may or may not be one of the given values. There are some ways around this (such as converting an out-of-range int) but it's still less error-prone than passing raw strings everywhere.

Not also, however, that whenever you have front-facing text, you should use a Resource (.resx), because this allows you to adjust how the strings show up depending on the language settings of the machine running your code. If any of these strings are ever to be seen by a user, use resources!

Upvotes: 0

IL_Agent
IL_Agent

Reputation: 747

static class ColumnNames
{
    public const string Names  = "Names";
    public const string Number = "Numbers";
    public const string Alarms = "Alarms";
}

Upvotes: 0

Amorphis
Amorphis

Reputation: 398

private enum enumColumnNames 
{
    Names,         // integer value = 0
    Numbers,        // 1
    Alarms,         // 2
};

And as long as the string value is equal to the enum name you can use enumColumnNames.Names.ToString() will produce "Names"

Upvotes: 2

Related Questions