Niiw Niiw - 10 days ago 6
C# Question

Add a type to a class

I have a simple classe called Device

public class Device
{

public string ID { get; set;}
public string Name {get; set;}
public string Serial {get; set;}

public Device()
{
}

}


With what variable could I define this Device type ?

This device can be or printer, or phone, or computer.. I have tried with a string :

public string DeviceType {get; set;} //e.g : this.DeviceType = "Phone";


But I find it not elegant. How else could I add a Type for this device ? With enum?

enum DeviceType
{
PHONE,
PRINTER,
COMPUTER,
TABLET
}


Any ideas of a nice way of doing this ?

Answer

You had it right... But depending on what you're doing, consider other devices as well.

enum DeviceType
{
   PHONE,
   PRINTER,
   COMPUTER,
   TABLET,
   APPLE_TV,
   AMAZON_FIRESTICK,
   ROKU, 
   XBOX, 
   PLAYSTATION, 
   MISC_DEVICE    /*Just in case the device being used isn't on the list*/
}

If you explain a little more of what you're trying to do, we could help you a little more.