From what I see there are several ways to do this: 1. Use class/interface inheritance 2. Use an enum 3. Use a boolean It all depends on what you want, If you want good control over the different types of rates you should use class/interface inheritance, if the dollar rate should have different functionality than say a euro rate (convert between rates etc...) Example:
interface Rate
{
double getValue();
}
class DollarRate : Rate
{
double rawval; //the actual value
internal double getValue()
{
return rawval \* 6; //format in the way you want
}
}
class PercetageRate : Rate
{
double rawval; //again, the value
internal double getValue()
{
return rawval; //I just use the raw value for percentage
}
}
class EuroRate : Rate
{
double rawval;
internal double getValue()
{
return rawval \* 9; //some other rate
}
}
void Main()
{
if(myTransactionLine.Amount is DollarRate)
{
//do dollar
}
else if(myTransactionLine.Amount is PercetageRate)
{
//do percentage
}
//continue...
}
If you don't need this then just use the enum approach to save time and space. I also came up with my own approach, I think it's the fastest one that allows for more than 2 rates (IE the bool method) It works like the enum approach but instead of enums you use constants. (It's faster because constants are converted to a raw value at compile time) Example:
static class Rates
{
//you can use whatever datatype you want, I choose byte since its 4 times smaller than int and can hold 256 cominations (including 0). You could also use chars: DOLLAR = '$', PERCENTAGE = '%', EURO = '€' etc...
public const byte DOLLAR = 0;
public const byte PERCENTAGE = 1;
public const byte EURO = 2;
//add more constants for more rates
}
class Rate
{
double value;
byte type; //the type of the rate
internal double Value
{
get { return value; }
}
internal byte Type
{
get { return type; }
}
}
void Main()
{
if(myTransactionLine.Amount.Type == Rates.DOLLAR)
{
//do dollar
}
else if(myTransactionLine.Amount is Rates.PERCENTAGE)
{
//do percentage
}
//continue...
}
Hope this helps! Sorry for long post...