0

I have done this before and since forgot, but would like to again in C#. I cannot, however, for the life of me remember what this practice is called so I'm having trouble even Googling it.

I'm thinking of a string that literally just means another piece of text. Like my string is KNOTS_PER_MILLIMETER, I want to be able to say that that is 0.00194384 and then simply type KNOTS_PER_MILLIMETER anywhere in my code instead of 0.00194384. This would also allow me to change that value if I have to.

Thanks!

Edit: Realized I'm essentially looking to implement #define in C#.

ThePartyTurtle
  • 2,276
  • 2
  • 18
  • 32
  • a `const` definition? – ryanyuyu Apr 20 '16 at 22:13
  • 3
    [`const`](https://msdn.microsoft.com/en-us/library/e6w8fe1b.aspx) is the keyword you are looking for. Note that constants belong to a class, so if you are within the same class you can use just the "name", but outside that class you must specify the class name. If you're looking for macro replacement like C++ has via `#DEFINE`, there is no such mechanism in C#. – D Stanley Apr 20 '16 at 22:13
  • Ahhh I just realized what the term I'm remembering is, but still don't know if it's available in C#. I essentially want to do a #define like in C or C++. I edited my question to reflect that. – ThePartyTurtle Apr 20 '16 at 22:18
  • 1
    @ThePartyTurtle C# does not have any mechanism that does that. Constants and Enums _sort of_ replace the name with the actual value, but they are more limited than C++ `#DEFINES` (e.g. you could replace a token with _code_ in C++; you can't in C#) – D Stanley Apr 20 '16 at 22:23
  • @D Stanley, gotcha, yea that clears it up for me and that was exactly what I was thinking of. Thanks! – ThePartyTurtle Apr 20 '16 at 22:28
  • 3
    I am curious to know (1) why you are dividing speed by length; knots are a unit of speed, and (2) where the number 0.00194384 comes from. I don't see any relationship between nautical miles, hours, and meters that has that ratio. Or for that matter, why you are mixing nautical miles with the metric system at all. What's your application? – Eric Lippert Apr 20 '16 at 22:40
  • @Eric, that's a typo. I was looking to convert millimeters/second into knots. Speed to speed, that number is the constant by which you multiply a mm/s value to obtain the speed in knots. – ThePartyTurtle Jun 24 '16 at 18:42
  • 1
    That now makes sense. Consider writing your code as: `const double MetersPerNauticalMile = 1852; const double SecondsPerHour = 3600; const double MMPerSecondToKnots = SecondsPerHour / (1000 * MetersPerNauticalMile);` If you write it that way then (1) it becomes crystal clear what all the "magic" constants mean, and (2) you have more useful constants to work with if you need them. – Eric Lippert Jun 24 '16 at 19:33
  • 1
    I note also that these are excellent examples of a correct use of constants. Constants should be quantities which are *eternally unchanging*. The number of meters per nautical mile is 1852 now and forever, so it should be a constant. Don't use constants for things like version numbers, company names or the price of gold; those things all change. – Eric Lippert Jun 24 '16 at 19:35

1 Answers1

2

One piece of text that maps to another piece of text is called macros. And C# doesn't have them.

But there is a specific tool that might be suitable for your task - constants.

public class Constants
{
     public const double KNOTS_PER_MILLIMETER =  0.00194384;
}


// in code:
var knots = millimeters * Constants.KNOTS_PER_MILLIMETER;
Oleh Nechytailo
  • 2,155
  • 17
  • 26
  • Ahh I hhad just remembered I aws thinking of a #define type situation from C or C++. Macros, you are correct! Thanks for that and for the suggestion. – ThePartyTurtle Apr 20 '16 at 22:20
  • 1
    @ThePartyTurtle: Remember that in C# you use `CamelCase` for constants, not `SHOUTY_SNAKE_CASE`. You want `KnotsPerMillimeter`. – Eric Lippert Apr 20 '16 at 22:37
  • I meant to say `PascalCase` of course. `camelCase` starts with a small letter. – Eric Lippert Apr 20 '16 at 23:07