(Real-Life Example: GPS Device Status, Packet Types, Commands)”**
C# में Enum एक ऐसा फीचर है जो आपके code को:
Clean
Readable
Error-proof
Fast
Professional
बनाता है।
Enum उन values के लिए perfect है जो fix, limited और meaningful होती हैं।
GPS server में enums सबसे ज़्यादा use होते हैं:
Device Status
Packet Types
Commands
Network Signal
Alert Types
आज Enum को real-life GPS examples के साथ deep में समझेंगे।
Enum =
Fixed values का strong group
जिन्हें नाम से represent किया जाता है, number से नहीं।
Example:
enum DeviceStatus
{
Online,
Offline,
Sleeping
}
अब DeviceStatus.Online के spelling mistake possible नहीं!
✔ Avoid magic strings
✔ Avoid typing mistakes (e.g., “Onnline”, “ofline”)
✔ Switch statements clean होते हैं
✔ Device status readable
✔ Packet type understandable
✔ Strong typing = less bugs
GPS server में यह बहुत important है।
enum DeviceStatus
{
Online,
Offline,
Idle,
NoGPS,
Charging
}
Device model:
public DeviceStatus Status { get; set; }
Use:
device.Status = DeviceStatus.Online;
हर device packet type अलग होता है:
Location
Heartbeat
Alert
SOS
Login
Status
Create enum:
enum PacketType
{
Login,
Heartbeat,
Location,
Alert,
SOS,
Unknown
}
Use in parser:
PacketType type = PacketType.Location;
Switch:
switch(type)
{
case PacketType.Location: HandleLocation(); break;
case PacketType.SOS: HandleSOS(); break;
}
Readable, clean, safe.
Example commands:
enum GPSCommand
{
Reboot,
CutEngine,
ResumeEngine,
RequestLocation,
SetAPN
}
Use:
SendCommand(device, GPSCommand.Reboot);
In send method:
switch(cmd)
{
case GPSCommand.Reboot:
Send("REBOOT#");
break;
}
Perfect for GPS management.
enum SignalStrength
{
NoSignal,
Weak,
Medium,
Strong,
Excellent
}
Use:
device.Signal = SignalStrength.Medium;
enum AlertType
{
Shock,
LowBattery,
GeoFenceIn,
GeoFenceOut,
OverSpeed,
SOS
}
Use:
if (alert == AlertType.SOS)
AlertPolice(device);
Enums internally int होते हैं:
enum PacketType
{
Login = 1,
Location = 2,
Heartbeat = 3
}
Now:
if ((int)PacketType.Location == 2)
{
// true
}
Serialize:
var json = JsonConvert.SerializeObject(device);
Output:
{
"status": "Online",
"packetType": "Location"
}
Readable JSON.
switch(packetType)
{
case PacketType.Login: LoginParser(packet); break;
case PacketType.Location: LocationParser(packet); break;
case PacketType.Alert: AlertParser(packet); break;
}
Readable
Simple
Zero bugs
C# Enums आपके code को बनाते हैं:
✔ Clean
✔ Error-free
✔ Strong typed
✔ Professional
✔ Easy to maintain
GPS servers, billing systems, ERP, school software —
हर जगह enums का heavy use होता है।
Enums बड़े systems में mandatory होते हैं।
0 Comments
Thanks for Commenting on our blogs, we will revert back with answer of your query.
EmojiThanks & Regards
Sonu Yadav