List of type struct, modify a field of a struct of a cell in the list leaving the rest equal

3

After searching several days through the network, I have not been able to resolve it:

struct TART
{
    public DateTime time;
    public int color;
    public double altura;
    public string probado;
}

List<TART> LART = new List<TART>();

In the List<TART> you have already entered several elements of type LART , but at a given moment I need to modify one of the fields of LART of a cell of List, the color for example, only that field and the rest ( time , altura and probado ) leave them the same as they were, how would you do it?

What I want is to have an array of tuples, that is, something that can be traversed and that inside have several variables grouped together, and that can erase, modify, add any field. Or if there is a more practical alternative form I accept the recommendation.

    
asked by MiguelRs 15.05.2017 в 19:42
source

4 answers

2

Beware of using struct s. Encourage the use of class unless you have an excellent reason to use struct .

Unless your data does not change, using a struct to save data that can change is almost always a design error and will result in amazing behavior.

For example, let's say you have the following list:

List<TART> LART = new List<TART>();
LART.Add(new TART
{
    time = DateTime.Now,
    color = 10,
    altura = 20.5,
    probado = "abc"
});

LART.Add(new TART
{
    time = DateTime.Now,
    color = 5,
    altura = 40.5,
    probado = "zzz"
});

Now, let's say you want to change the value of color to 100 for the second element. Maybe you can try something like this:

LART[1].color = 100;

... but this results in the following error:

  

Can not modify the return value of 'System.Collections.Generic.List.this [int]' because it is not variable

So, maybe you try the following:

TART t = LART[1];
t.color = 100;

.. that does not result in an error, but it does not work correctly either, because when doing TART t = LART[1]; , t is now only a copy of the struct that is in the list, so that you would only be modifying the copy, and not the item that is on the list.

For it to work, you would be forced to assign the copy back to the list in this way:

TART t = LART[1]; // obtener copia
t.color = 100; // modificar copia
LART[1] = t; // asignar de vuelta a la lista

Again, better define TART as a class instead of struct to avoid this problem and many more. By defining it as a class , all the options mentioned above will work correctly.

    
answered by 15.05.2017 в 20:20
1

A structure is an "almost immutable" type. That means that once you fill in your data, to touch them you will have to build something like an interface to be able to access to modify them.

Each time you make a copy of that structure, you will not copy a pointer to it, but a new copy will be created.

Then your possible solutions are to replace the item in the list with a new item.

or create an interface for your structure that has a set method for the fields you want to modify, and use that field to change the values.

public interface IModificarEstructura
{      String SetColor...       }
public struct TART: IModificarEstructura...

List<TART> LART = new List<TART>();
...
LART [1].SetColor("blanco");
...
    
answered by 15.05.2017 в 20:06
0

Beware that in c # struct must be designed to be immutable. I recommend this reading of why these structs are "evils" Why are mutable structs "evil"?

So, what I see in your code is that you have an unchangeable struct ... then to do what you want you must change your struct to a class and everything would be fine ... or make your struct mutable:

 struct TART
 {
    public DateTime time;
    public int color;
    public double altura;
    public string probado;

    public TART(Datetime time, int color, double altura, string probado): this()
    {
      this.time = time;
      this.color = color;
      this.altura = altura;
      this.probado = probado;
    }
}

List<TART> LART = new List<TART>();      
//instancias un nuevo lart y agregas un nuevo elemento
LART.Add(new LART( tuTiempo, tuColor, tuAltura , probado));
//tomas una copia del primer elemento y cambias el color
LART[0].Color = 11;

Something like that more or less.

    
answered by 15.05.2017 в 20:15
0

Or the simplest thing:

List<TART> LART = new List<TART>();
LART.Add(new TART { time = DateTime.Now, color = 1, altura = 4, probado = "1" });
LART.Add(new TART { time = DateTime.Now, color = 2, altura = 23, probado = "0" });
LART.Add(new TART { time = DateTime.Now, color = 6, altura = 12, probado = "1" });
LART.Add(new TART { time = DateTime.Now, color = 8, altura = 15, probado = "0" });

TART _tart = LART[3];
_tart.color = 333;
LART[3] = _tart;  
    
answered by 15.05.2017 в 20:18