I have created a class for handling Unit Conversion in C#. It is not working as it should only returning strings.
Here is the class:
using System;
us
Nope:
I tried giving this to Visual Studio:
public enum Test : double
{
Hello, World
}
And it said:
Type byte, sbyte, short, ushort, int, uint, long, or ulong expected
You cant use enum with doubles. You can only use it with int
and long
You can convert a double
to a long
using the BitConverter.DoubleToInt64Bits(double)
method before hand and hardcode them into an enum with the backing long
type. Then you would have to convert the enum value back to a double
.
It is probably more trouble than it is worth.
No, The default type for enum is int
or long
and you could not use fractional numbers with it. You can use a struct or class intead of enum for double
public struct Units
{
public const double Grams = 1;
public const double KiloGrams = 0.001;
public const double Milligram = 1000;
public const double Pounds = 0.00220462;
public const double Ounces = 0.035274;
public const double Tonnes = 0.000001;
// Add Remaining units / values
}
And use it like
double d = Units.KiloGrams;
You can't use float/double with enums. You may have to define constants.
public const double KiloGrams = 0.001;
Sorry, can't be done. Enums are strictly integers/bytes. Really, they are supposed to be their own type.
You can try:
enum Units() {
Grams ,
KiloGrams ,
Milligram
}
public function GetUnitsFloatValue(Units unit) : float
{
switch (unit)
{
case Units.Grams :
return 1;
case Units.KiloGrams :
return 0.001;
case Units.MilliGrams:
return 1000;
}
//unhandled unit?
return 0;
}