Click here to Skip to main content
15,887,214 members
Home / Discussions / C#
   

C#

 
GeneralRe: Track two types of values within one variable Pin
Paul Conrad28-Jul-12 9:31
professionalPaul Conrad28-Jul-12 9:31 
GeneralMessage Removed Pin
27-Jul-12 12:24
professionalN_tro_P27-Jul-12 12:24 
GeneralRe: Track two types of values within one variable Pin
SledgeHammer0127-Jul-12 12:36
SledgeHammer0127-Jul-12 12:36 
GeneralMessage Removed Pin
27-Jul-12 13:52
professionalN_tro_P27-Jul-12 13:52 
GeneralRe: Track two types of values within one variable Pin
jschell28-Jul-12 6:33
jschell28-Jul-12 6:33 
AnswerRe: Track two types of values within one variable Pin
Trak4Net27-Jul-12 18:26
Trak4Net27-Jul-12 18:26 
GeneralRe: Track two types of values within one variable Pin
Paul Conrad28-Jul-12 9:32
professionalPaul Conrad28-Jul-12 9:32 
AnswerRe: Track two types of values within one variable Pin
Northcodedotno29-Jul-12 8:36
Northcodedotno29-Jul-12 8:36 
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:
C#
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:
C#
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...
AnswerRe: Track two types of values within one variable Pin
BobJanova29-Jul-12 23:16
BobJanova29-Jul-12 23:16 
GeneralAdding databound ComboBox into cells of a DataGridView Pin
Dewald27-Jul-12 2:11
Dewald27-Jul-12 2:11 
QuestionHow to make GRIDVIEW as Rounded Corners Pin
mukusingh27-Jul-12 0:33
mukusingh27-Jul-12 0:33 
AnswerRe: How to make GRIDVIEW as Rounded Corners Pin
Richard MacCutchan27-Jul-12 1:57
mveRichard MacCutchan27-Jul-12 1:57 
AnswerRe: How to make GRIDVIEW as Rounded Corners Pin
AmitGajjar27-Jul-12 17:54
professionalAmitGajjar27-Jul-12 17:54 
Questionrefresh result in MDI child Pin
Jassim Rahma27-Jul-12 0:05
Jassim Rahma27-Jul-12 0:05 
AnswerRe: refresh result in MDI child Pin
Eddy Vluggen27-Jul-12 0:09
professionalEddy Vluggen27-Jul-12 0:09 
GeneralRe: refresh result in MDI child Pin
Jassim Rahma27-Jul-12 0:28
Jassim Rahma27-Jul-12 0:28 
GeneralRe: refresh result in MDI child Pin
Eddy Vluggen27-Jul-12 0:47
professionalEddy Vluggen27-Jul-12 0:47 
GeneralRe: refresh result in MDI child Pin
Jassim Rahma27-Jul-12 2:52
Jassim Rahma27-Jul-12 2:52 
GeneralRe: refresh result in MDI child Pin
Eddy Vluggen27-Jul-12 2:59
professionalEddy Vluggen27-Jul-12 2:59 
QuestionHow can I attach wndproc to another window and do something when certain wm_event appears ? Pin
Member 964871826-Jul-12 23:57
Member 964871826-Jul-12 23:57 
AnswerRe: How can I attach wndproc to another window and do something when certain wm_event appears ? Pin
Eddy Vluggen27-Jul-12 0:52
professionalEddy Vluggen27-Jul-12 0:52 
GeneralRe: How can I attach wndproc to another window and do something when certain wm_event appears ? Pin
Member 964871827-Jul-12 1:54
Member 964871827-Jul-12 1:54 
GeneralRe: How can I attach wndproc to another window and do something when certain wm_event appears ? Pin
Eddy Vluggen27-Jul-12 1:59
professionalEddy Vluggen27-Jul-12 1:59 
GeneralRe: How can I attach wndproc to another window and do something when certain wm_event appears ? Pin
Dave Kreskowiak27-Jul-12 2:13
mveDave Kreskowiak27-Jul-12 2:13 
GeneralRe: How can I attach wndproc to another window and do something when certain wm_event appears ? Pin
Member 964871827-Jul-12 3:54
Member 964871827-Jul-12 3:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.