Is this the correct use of Enums?

Is this a good case to use enthusiasm? Or would it be better to use an array? The values ​​here have not changed, maybe they say once a year.

using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { public enum transmission { Manual, NonSynchronous, Automatic, SemiAutomatic, Continuously, Infinitely, Electric, Hydrostatic, Hydrodynamic, } public enum bodystyle { Convertable, Hatchback, Sports, Sedan } public enum carcolors { Red, Blue, Yellow, White, Black, Green } public enum fueltype { Biofuels, FossilFuels, Nuclear, Fission, Fusion } public class Car { public Car(String cName, double cMaxSpeed, String cTransmission, String cBodystyle, String cColors, String cFueltype) { carname = cName; transmission = cTransmission; bodystyle = cBodystyle; colors = cColors; fueltype = cFueltype; maxspeed = cMaxSpeed; } public string carname { get; set; } public string transmission { get; private set; } public string bodystyle { get; private set; } public string colors { get; private set; } public string fueltype { get; private set; } public void carInfo() { Console.WriteLine("------------------------------------"); Console.WriteLine("Car Name: " + this.carname); Console.WriteLine("Car Transmission: " + this.transmission ); Console.WriteLine("Car Bodystyle: " + this.bodystyle); Console.WriteLine("Car Colors: " + this.colors); Console.WriteLine("Car Fueltype: " + this.fueltype); Console.WriteLine("Car MaxSpeed: " + this.maxspeed); Console.WriteLine("------------------------------------"); } } public class Program { static void Main(string[] args) { Car nissan = new Car("Lamborgini", 255, Convert.ToString(transmission.Automatic), Convert.ToString(bodystyle.Sports), Convert.ToString(carcolors.Red), Convert.ToString(fueltype.Biofuels)); nissan.carInfo(); } } } 
+4
source share
8 answers

You have defined several types of Enum , but you are not actually using them. Therefore, in this regard, this is not the right use.

As for the use of the array - I do not see the value of this for enumerations.

The types you pass are all string , not Enum types, as well as the types of your properties.

Proper use would look like this:

 public enum BodyStyle { Convertable, Hatchback, Sports, Sedan } public class Car { public Car(String cName, BodyStyle cBodyStyle) { carname = cName; this.BodyStyle = cBodyStyle; } public string carname { get; set; } public BodyStyle BodyStyle { get; private set; } } 
+4
source

The problem will be that a recompilation will be required to change them. What else does this app bind? Can these values ​​be stored in a database? This would make it easier to update, and you could do it without recompiling the application.

+2
source

The problem is, when they change, you have to modify, compile, test and release your code. I would suggest if something can change, and then save it somewhere where it can be configured at runtime, for example, in a database or configuration file.

0
source

No, I would declare properties with enumerations, which they should represent as follows:

  public transmission transmission { get; private set; } 
0
source

It doesn't look like you are using enums in your code. Property types must match the enumerations you created:

 public bodystyle BodyStyle { get; set; } 
0
source

The enumerations you listed are really more suitable as data and are loaded as such (into arrays, dictionaries, etc.).

The advantage of enumerations is that they are actually numeric values, so if you need to explicitly test against any of the values ​​that you must use when using the TT template to create enumerations from your data.

Basically, if they are not used (i.e. verified values) programmatically, then they are probably not enumerations :)

0
source

I do not think so.

You work against what Enum is. I think Enum is a family of integer constants; you use it as an object that just happens to convert to a specific string.

0
source

I don’t understand why you are declaring enums while using only the string component of them. In this situation, you can use arrays of strings.

If you want to use enums (and yes, this situation is suitable), you can do:

 public Car(String cName, double cMaxSpeed, transmission cTransmission, bodystyle cBodystyle, carcolors cColors, fueltype cFueltype) { carname = cName; transmission = cTransmission; bodystyle = cBodystyle; colors = cColors; fueltype = cFueltype; maxspeed = cMaxSpeed; } public transmission transmission { get; private set; } public bodystyle bodystyle { get; private set; } public carcolors colors { get; private set; } public fueltype fueltype { get; private set; } 

Not a complete change of code, but I think you get this idea. There is no point in declaring enumerations if you only finish converting them to strings. Keep the listings as they are. Pass them to the constructor and save them as an declared enumeration type.

0
source

Source: https://habr.com/ru/post/1380592/


All Articles