To avoid the use of magic numbers, I created a static class to store some integer values that are represented inside a database. I avoided using enums because I need to cast them every time I use them. To benefit from the clean approach of enums while also enjoying the type definition offered by classes.
The Class Approach
public static class TransactionType
{
public static readonly int
Deposit = 1,
Withdrawal = 2,
Transfer = 3,
Payment = 4,
Refund = 5;
}
// Example Usage
public void ProcessTransactionClass(int transactionTypeId)
{
if (transactionTypeId == TransactionType.Deposit)
{
// Handle deposit
}
else if (transactionTypeId == TransactionType.Withdrawal)
{
// Handle withdrawal
}
// ... and so on for other types
}
Enum Approach
public enum TransactionType
{
Unassigned = 0, // Default value
Deposit = 1,
Withdrawal = 2,
Transfer = 3,
Payment = 4,
Refund = 5
}
// Example Usage
public void ProcessTransactionEnum(int transactionType)
{
if (transactionType == (int)TransactionType.Deposit)
{
// Handle deposit
}
else if (transactionType == (int)TransactionType.Withdrawal)
{
// Handle withdrawal
}
// ... and so on for other types
}
I haven't seen this kind of usage anywhere, is this style any good and acceptable?
const? I wonder if your resistance on casting to anenumto anintis more a lack of understanding on your part. Example usage code will be a big help in getting quality answers. \$\endgroup\$constthe value will be stored inside the assembly which I don't want for various reasons. Like a change in a shared library would result in different values because it is determined at compile time \$\endgroup\$