Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
N

Northcodedotno

@Northcodedotno
About
Posts
1
Topics
0
Shares
0
Groups
0
Followers
0
Following
0

Posts

Recent Best Controversial

  • Track two types of values within one variable
    N Northcodedotno

    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...

    C# beta-testing question code-review
  • Login

  • Don't have an account? Register

  • Login or register to search.
  • First post
    Last post
0
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups