PDA

Ver la Versión Completa : Propiedad Capacity


esocrates
05-07-2006, 19:45:07
Encontré en este hilo:
http://www.clubdelphi.com/foros/showthread.php?t=33311
Una línea de código que no comprendo.

procedure TForm1.Button1Click(Sender: TObject);
var
I:Integer;
begin
if ListBox1.Items.Capacity=0 then
begin
for I:=0 to TreeView1.SelectionCount-1 do
ListBox1.Items.Add(TreeView1.Selections[i].Text);
end;
end;

Me refiero a la línea que contiene Capacity (la marqué en azul).
Les agradeceré un comentario aclaratorio.
Un saludo y gracias

marcoszorrilla
05-07-2006, 19:53:31
Pues nos informa de si el ListBox, tiene asignado algún valor o esta vacío.

Un Saludo.

marcoszorrilla
05-07-2006, 19:57:37
procedure TForm1.Button1Click(Sender: TObject);
begin
if ListBox1.Items.Capacity = 0 then
ShowMessage('Vacio')
else
Showmessage('Tiene asignados:'+IntToStr(ListBox1.Items.Capacity));
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
if ListBox1.Items.Count = 0 then
ShowMessage('Vacio')
else
Showmessage('Tiene asignados:'+IntToStr(ListBox1.Items.Count));
end;

En estos dos ejemplos, sería lo mismo utilizar Capacity que Count.

Aunque también conviene este apunte de la ayuda de Delphi, porque en algunos casos debe de utilizarse con otros propósitos:
Indicates the number of strings the string list has allocated memory to hold.

property Capacity: Integer;

Description

Use Capacity to find out how much memory is available for holding strings, or to reallocate the memory to allow more or fewer strings.

Capacity is the number of entries in the string list that have been allocated, as opposed to Count, which is the number of strings in the list. Thus, Capacity is always greater than or equal to Count.

Adding new strings will cause the Capacity property to increase if necessary, but setting the Capacity property will not change the Count property. Do not set Capacity to a value less than Count, or the list will be truncated and Count will indicate that the list contains more strings than is the case. Also, if Capacity is set to a value less than Count, the memory used to store the strings that are dropped from the list (as opposed to the entries in the list that point to those strings) is not freed.
Un Saludo.

esocrates
05-07-2006, 22:28:58
Muchas gracias marcoszorrilla.
Ahora me queda más claro.
Un saludo