I have a large C# class which is full of nothing but public const string
fields. However, in one case I am trying to concatenate a string and a enum value into a const string
field, like this:
public const string GET_VALUES = "SELECT * FROM [tbl] WHERE [id] = " + Enum.Val;
However, I get this compiler error:
'Namespace.SqlStatements.GET_VALUES' must be constant
I know I could drop the const
clause, but I would like to keep all fields within this class consistent. Is it possible to concatenate a constant string and a enum in C#?
It is not necessary to assign sequential values to Enum members. They can have any values. In the above example, we declared an enum PrintMedia .
An ENUM is a string object with a value chosen from a list of permitted values that are enumerated explicitly in the column specification at table creation time.
Enum is a class in python for creating enumerations, which are a set of symbolic names (members) bound to unique, constant values. The members of an enumeration can be compared by these symbolic anmes, and the enumeration itself can be iterated over.
From MSDN:
A constant expression is an expression that can be fully evaluated at compile time. Therefore, the only possible values for constants of reference types are string and a null reference.
In your case, the enum will have to be converted to a string using ToString
, which is not possible at compile time. I'd suggest you change it to readonly
as elgonzo mentioned.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With