0

Let's say I wanted to create a constant that would return exchange fees for me as double in C#. Here is what I would do in JavaScript:

var exchangeFees = {
  fidelity: .25,
  etrade: .2,
  scottTrade: .15
}

This way, I can import this and have it work as a sort of enum: exchangeFees.fidelity would get me the fee I need, and is clear. In Typescript, I would do something a bit less rational:

//I just realized this could just be a static class after I typed this
export class ExchangeFees {
  FIDELITY: number;
  ETRADE: number;
  SCOTTTRADE: number; 

  constructor() {
    this.FIDELITY= .25;
    this.ETRADE= .2;
    this.SCOTTTRADE = .15;

  }
}

export const exchangeFees : ExchangeFees = new ExchangeFees ();

This seems hacky-as-hell, but Javascript, whatchu gonna do, etc. This just seems hacky in C# though, do I just create a static class and put it in a Constants.cs file like so? Is there a better way?

public static class ExchangeFess
{
    public static double Fidelity = .25;
    public static double Etrade = .2;
    public static double ScottTrade = .15;
}

I read the answer here, should I put it in AppSettings somehow or is there no point?

I realize this is 101 question, but I am not sure I am approaching this cleanly.

VSO
  • 11,546
  • 25
  • 99
  • 187
  • 1
    Have you considered using an enum? – Sweeper Jul 26 '17 at 00:33
  • Yes, iirc there is an extra step to get the actual value, checking examples now. – VSO Jul 26 '17 at 00:35
  • @Sweeper If I recall correctly, you cannot have enums of type double –  Jul 26 '17 at 00:37
  • @YagnaPatel Enums are just `in`t as far as I can tell, and then there is the fact that getting the value requires an extra step: `(int)Exchange.Fidelity;` – VSO Jul 26 '17 at 00:42

2 Answers2

1

What you have is fine. An alternative would be to write:

public sealed class ExchangeFess
{
    public const double Fidelity = .25;
    public const double Etrade = .2;
    public const double ScottTrade = .15;
}

In order to have the field constant. However, that comes with drawbacks of its own, see here.

However, your values don't really seem to be constant, and almost definitely shouldn't be baked into your code. You should use some type of configuration system. AppSettings does provide this functionality, but to me doesn't seem like the correct place to put it. I'd have a configuration file or database which the application reads from at start up.

Then, use your code as is, but replace the hard coded values with a (optionally cached) lookup to the configuration.

Rob
  • 26,989
  • 16
  • 82
  • 98
0

There are a few ways to do this:
1: You could do as you already said, and create a static class for containing constants.
2: Create the constants in your class
3: An enum wouldn't work, it only uses ints, and in your example, you use doubles
4: You could figure out some way to use an Anonymous class.
5: This is a not-so-beautiful kind of solution: use an enum and a function that does some math and returns the proper double result.

Optimistic Peach
  • 3,862
  • 2
  • 18
  • 29