Reputation: 1
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
Reputation: 165
The 1st form is the best for the following reasons:
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:
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
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
Reputation: 747
static class ColumnNames
{
public const string Names = "Names";
public const string Number = "Numbers";
public const string Alarms = "Alarms";
}
Upvotes: 0
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