PDA

Ver la Versión Completa : Error con TStringList


Jan_polero
04-12-2004, 14:23:35
Hola a todos¿Sabe alguien por qué me da un error al intentar hacer un Add con un StringList?:
El código es:

//Código Delphi

public
ObjInv: TStringList; //declaro el StringList en La seccion Public del form
.
.
.
procedure TFInventario.FormCreate(Sender: TObject);
begin
//la creo
ObjInv := TStringList.Create;
end;

//fin

y en un procedimiento que he creado tengo la línea:

ObjInv.Add(Item.NombreItem); //Añando esta propiedad,que es string
esto me produce una excepción:'Acces violation at adress...'.Pero el problema debe ser antes pq al comentar la línea me aparece el mismo error al hacer un Count.Debo de asignar mal la StringList.¿Alguna solución?
Gracias por adelantado.

Sinaloense
04-12-2004, 15:05:37
Debería ser :

ObjInv.Create;

en vez de:

ObjInv := TStringList.Create;

Gydba
04-12-2004, 15:51:24
Hola,

Primero que nada los invito a utilzar las etiquetas DELPHI del foro que permiten una mejor visualización del código. Segundo, podrías probar:

varStrList := TStringList.Create;
...
varStrList.Append('Mi cadena');
...
FreeAndNil(varStrList);

Jan_polero
04-12-2004, 16:03:46
Gracias por responder.
Lo que ocurre es que después de cambiarlo me sigue saltando la misma excepción...:confused:

Gydba
04-12-2004, 16:09:37
¿Probaste con un ejemplo simple como el que te mandé?

Tal vez deberías exponer el código que usás para ver donde falla.

Saludos!

roman
04-12-2004, 20:26:57
Debería ser :

ObjInv.Create;

en vez de:

ObjInv := TStringList.Create;

Desde luego que no. La segunda forma es la correcta.

// Saludos

Sinaloense
05-12-2004, 04:01:50
Nop, la forma que puse es la correcta

La probe antes y funciona muy bien.

roman
05-12-2004, 06:36:56
Parece que hace falta aquí repasar los conceptos básicos de las clases en Delphi.

La ayuda de Delphi, al la letra, dice:



A constructor is a special method that creates and initializes instance objects. The declaration of a constructor looks like a procedure declaration, but it begins with the word constructor. Examples:

constructor Create;
constructor Create(AOwner: TComponent);

Constructors must use the default register calling convention. Although the declaration specifies no return value, a constructor returns a reference to the object it creates or is called in.

A class can have more than one constructor, but most have only one. It is conventional to call the constructor Create.

To create an object, call the constructor method on a class type. For example,

MyObject := TMyClass.Create;


Más adelante dice:



When a constructor is called using an object reference (rather than a class reference), it does not create an object. Instead, the constructor operates on the specified object, executing only the statements in the constructor's implementation, and then returns a reference to the object. A constructor is typically invoked on an object reference in conjunction with the reserved word inherited to execute an inherited constructor.


// Saludos

eduarcol
06-12-2004, 02:15:33
Pero el problema debe ser antes pq al comentar la línea me aparece el mismo error al hacer un Count

Pero un count a que?? a item o a objinv, por cierto ya item esta creado cuando lo llamas