Home > Back-end >  How can I make one piece of code for several structs?
How can I make one piece of code for several structs?

Time:08-22

I have 4 structs:

Computer, Home, Car, Ship

I declared:

1. An array that can hold up to 5 types of computers.

2. An array that can hold up to 20 houses, each can have one computer.

3. An array that can hold up to 20 cars, each can have one computer.

4. An array that can hold up to 20 ships, each can have one computer.

This is the code:

struct Computer
{
    public string pcName;

    public string motherBoardName;
    public string graphicCardName;

    public int memorySize;
    public int numberOfDrivers;
}

struct Home
{
    public Computer pc;
    list of parameters for home... (int, byte, string, double)    
}

struct Car
{
    public Computer pc;
    list of parameters for a car... (int, byte, string, double)    
}

struct Ship
{
    public Computer pc;
    list of parameters for a ship... (int, byte, string, double)    
}

Computer[] pc = new Computer[5]; // 5 types of computers

Home[] home = new Home[20]; // Up to 20 houses
Car[] car = new Car[20]; // Up to 20 cars
Ship[] ship = new Ship[20]; // Up to 20 ships

I want to be able to set/get parameters for each one of the 5 computers in the pc array, and also for each one of the computers (pc) at home, car or a ship.

On my panel I have an editBox for each one of the pc parameters:

pcName, motherBoardName, graphicCardName, memorySize, numberOfDrivers

I have a "Set" and a "Get" buttons, and some few more controls (for example radioButtons) that let the user choose if he's setting or getting parameters for one of the 5 computers (in the computers array) or for a PC in one of the homes, one of the cars, or one of the ships.

Now comes my main question:

Can I somehow write a single piece of code that can Set a PC parameters for each one of them? (computer[5], home[20], car[20], ship[20]) and a single piece of code that can Get a PC parameters from each one of these arrays and show them on the panel?

Or must I write one function to set parameters for a PC, one that set parameters for a home pc, one for a car pc... so I will end up with 8 different functions for all the sets and gets?

I hope that my question is clear enough.

Waiting for your advice. Thanks.

CodePudding user response:

As I said in the comments, you should be using classes rather than structures. The behaviour of value types makes working with mutable structures tricky so structures should generally only be small, immutable types. You should be using classes with properties, rather than structures with fields.

We only need two of your types to demonstrate what you could/should do:

public class Computer
{
    public string Name { get; set; }

    // ...
}

public class Home
{
    public string Name { get; set; }
    public Computer Computer { get; set; }

    // ...
}

You can then write a method that takes an IEnumerable<Computer> to modify any list of Computer objects.

private void ModifyComputers(IEnumerable<Computer> computers)
{
    foreach (var computer in computers)
    {
        // Use computer here.
    }
}

If you have a Computer array then you can pass that directly. If you have a Home array then you can get the Computer from each one using LINQ and pass the result.

Computer[] computers;
Home[] homes;

// ...

ModifyComputers(computers);
ModifyComputers(homes.Select(h => h.Computer));

That Select method will take an enumerable list as an input, perform the specified transformation on each item and return an enumerable list containing the results. In that last line of code, the transformation is "get the Computer from the Home".

Actually, one point to note is that, because you're using arrays that may have null elements, the code above may throw a NullReferenceException. You would need to either change the method to account for null:

private void ModifyComputers(IEnumerable<Computer> computers)
{
    foreach (var computer in computers)
    {
        if (computer != null)
        {
            // Use computer here.
        }
    }
}

or make sure to not pass any null references:

Computer[] computers;
Home[] homes;

// ...

ModifyComputers(computers.Where(c => c != null));
ModifyComputers(homes.Where(h => h != null).Select(h => h.Computer));

If you go for the first option, you'd also need to account for null references in the Home array:

ModifyComputers(homes.Select(h => h?.Computer));

The ?. operator will return null if the first operand is null where the . operator would throw an exception.

  • Related