14

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;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace RestaurantManagementSystem
{
    class unitHandler
    {
        public enum Units
        {
            Grams = 1,
            KiloGrams = 0.001,
            Milligram = 1000,
            Pounds = 0.00220462,
            Ounces = 0.035274,
            Tonnes = 0.000001,

            Litres = 1,
            Millilitres = 1000,
            Cups = 4.22675,
            FluidOunces = 33.814,
            TeaSpoon = 202.884,
            TableSpoon = 67.628,
            CubicFeet = 0.0353147,
            CubicInch = 61.0237,
            CubicCentimetres = 0.0001,
            CubicMetres = 0.001

        }

        public double Convert_Value(Units from, Units to, double quantity)
        {
            double converted_quantity = 0;

            double fromValue = quantity * Convert.ToDouble(from.ToString());

            converted_quantity = fromValue * Convert.ToDouble(to.ToString());

            return converted_quantity;
        }
    }
}

I would somehow want the enum type to contain the double values of conversion factors of each unit then use them for conversion and returning the converted quantity.

John Ernest Guadalupe
  • 6,379
  • 11
  • 38
  • 71

6 Answers6

23

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;
Adil
  • 146,340
  • 25
  • 209
  • 204
5

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

Carson Myers
  • 37,678
  • 39
  • 126
  • 176
3

You can't use float/double with enums. You may have to define constants.

public const double KiloGrams = 0.001;
Habib
  • 219,104
  • 29
  • 407
  • 436
3

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;
}
Zaheer Ahmed
  • 28,160
  • 11
  • 74
  • 110
1

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.

Dmitry S.
  • 8,373
  • 2
  • 39
  • 49
0

You cant use enum with doubles. You can only use it with int and long

John Gathogo
  • 4,495
  • 3
  • 32
  • 48