I'm learning and there are things I don't understand. I was reading documentation about creating classes and constructors etc. But I can't understand how it works for what I need. I know that it is something basic and that I am not understanding, let me explain:
I need to create the Dog object from the class TDOG and use it independently as a container to make modifications to it, before assigning it back to the array containing a collection of dogs. But when assigning to the variable DOG , the object vector[i]
of the same class, to be able to edit it and later save it in the vector, it is as if any modification in the dog variable, immediately modifies the position of the vector.
type
Tperro = class
nombre : string;
raza : string;
end
var
vector : array [1..10] of TPerro;
implementation
procedure Tform1.cargarperros; //cargo los perros
begin
vector[1]:= TPerro.create
vector[1].nombre := 'Cubo';
vector[1].raza := 'caniche';
vector[2]:= TPerro.create;
vector[2].nombre :='Chopper';
vector[2].raza := 'labrador';
end;
procedure TForm1.edicion; // editar Perro, utilizando el objeto "PERRO" y no directamente desde el vector
var perro : TPerro;
begin
Perro := TPerro.create;
Perro := vector[1]; //asigno los datos de vector[1] a variable PERRO
Perro.name := 'Olmedo';
Showmessage('Nombre anterior ' + vector[1].name); //aca el nombre sale "olmedo" (cuando deberia salir "CUBO")
Showmessage('Nombre Nuevo ' + Perro.name); //aca sale nombre "olmedo"
vector[1] := Perro //aca vuelvo al vector, y le asigno las modificaciones en el (recipiente) PERRO.
end;
The showmessage
( which I use to show you what happens to me ) shows the same thing for both DOG , and for vector[1]
, when in fact I need that the vector that contains the collection of dogs, not be previously modified, without first verifying the DOG object , to which I make the modifications.
I remember that before using the class ( record ) I did not have these problems. But since I need to add methods to the TPerro class , I need to use it as an Object and not as a record. I don't know if you can add methods to the record class though . Still, I'm learning and need to understand how it works.
Any change you make to the DOG object is made immediately to the array position you previously assigned to it. ( PERRO := vector[1]
). Is there something I'm not understanding, or I don't know if I'm missing something, or I should declare the constructor method of the class with some kind of specification.
I hope to explain myself well.