Home > Software engineering >  How do I loop through classes effectively in C#
How do I loop through classes effectively in C#

Time:10-11

hello i have a few classes and i have problem looping through some of the classes to get the values.

eg, i want to know which sensors are having issues. sensors comprises of lidar, imu, camera1234. i want so check their status and if their status is not 'good' then i need to say 'lidar not good'.

ways that i have thought of:

  1. list of classes? but what is the 'Type' for classes?
  2. using Root class, access the other classes from it, similar to how we do for iterating through properties in classes. but how do i do this?

'''

public class Battery
{
    public string status { get; set; }
    public string value { get; set; }
}

public class Camera1
{
    public string status { get; set; }
}

public class Camera2
{
    public string status { get; set; }
}

public class Camera3
{
    public string status { get; set; }
}

public class Camera4
{
    public string status { get; set; }
}

public class Cpu
{
    public string status { get; set; }
    public string value { get; set; }
    public string temperature { get; set; }
    public string ram { get; set; }
    public string utilisation { get; set; }
}

public class CurrentJob
{
    public object job { get; set; }
    public object task { get; set; }
    public object location { get; set; }
}

public class Dex
{
    public CurrentJob current_job { get; set; }
    public Battery battery { get; set; }
    public Gpu gpu { get; set; }
    public Cpu cpu { get; set; }
    public Lidar lidar { get; set; }
    public Camera1 camera1 { get; set; }
    public Camera2 camera2 { get; set; }
    public Camera3 camera3 { get; set; }
    public Camera4 camera4 { get; set; }
    public Imu imu { get; set; }
}

public class Gpu
{
    public string status { get; set; }
    public string value { get; set; }
    public string memory { get; set; }
    public string utilisation { get; set; }
}

public class Imu
{
    public string status { get; set; }
}

public class Lidar
{
    public string status { get; set; }
}

public class Root
{
    public Dex dex { get; set; }
}

CodePudding user response:

I'll throw you a bone. The classes all share a Status Property so create a base class called

class Device{
    public string name { get; set; }
    public string status { get; set; }
}

Now all your classes can derive from it:

public class Camera1 : Device
{ ]

Then you can have a List<Device> devices = new(); and iterate over that

foreach(var device in devices)
{ 
   if (device.status == "good") 
   {
     System.Diagnostics.Debug.WriteLine(device.Name   " all good");
   }
   else 
   {       
     System.Diagnostics.Debug.WriteLine(device.Name   " not good");
   }
}

It can depend in some scenarios it maybe better to use an Interface:

public interface IDevice{
    string GetName();
    string GetStatus();
}

Implement these in your classes:

public class Camera1 : IDevice
{
    public string GetStatus(){
         return "good";
    }
}

Then you could iterate over anything that implements the Interface (not tested):

List<IDevice> idevices = new List<IDevice>();
idevices.Add(new Camera1());
idevices.Add(new Camera2());
idevices.Add(new Camera3());

foreach(var device in idevices)
{
    System.Diagnostics.Debug.WriteLine(device.GetStatus());
}

CodePudding user response:

Saw that JEremey proposed something good while I was writing, but here is "another bone", in a similar fashion.

Note:

  1. I was more minimal in my interface design (I took care only of the Status part, not even the name).

  2. As per the description of your description, I think something much more detailed is needed for status, even a class for itself. Status seems a complex thing where you want to read / store various information (like a boolean value to tell if is it good or not a description or what is the issue if any). But below I kepty status as a simple string for a start.

using System;
using System.Linq;
using System.Collections.Generic;

public interface HasStatus
{
    string Status { get; set; }
}

public class Battery : HasStatus
{
    public string Status { get; set; }
    public string Value { get; set; }
}

public class Camera : HasStatus
{
    public string Status { get; set; }
    public int Id { get; set; }
}

public class Cpu : HasStatus
{
    public string Status { get; set; }
    public string Value { get; set; }
    public string Temperature { get; set; }
    public string Ram { get; set; }
    public string Utilisation { get; set; }
}

class HelloWorld {
  static void Main() {
    Console.WriteLine("Hello World");
    
    var allMyStuff = new List<HasStatus>
    {
        new Camera { Id = 111, Status = "Good" },
        new Camera { Id = 200, Status = "Sensor has a problem" },
        new Camera { Id = 304, Status = "Good" },
        new Camera { Id = 467, Status = "Good" },
        new Cpu { Status = "Doesn't run very fast", Ram = "1MB" },
        new Battery { Value = "a lot", Status = "Good" }
    };
    
    int i = 0;
    foreach (var stuff in allMyStuff)
    {
        Console.WriteLine($"Status of object N° {i} of type {stuff.GetType().Name} is '{stuff.Status}'");
        i  ;
    }
        
  }
}

You can filter the not-good ones, for instance with :

var allNotGoodStuff = allMyStuff.Where(x => x.Status != "Good");

foreach (var badStuff in allNotGoodStuff)
{
    // Do some display or anything else with each bad stuff here...

}
  • Related