If I have an array of objects, how do I check if an object exists at part of the array?
I tried if object=null but it said i need a 'variant'
If I have an array of objects, how do I check if an object exists at part of the array?
I tried if object=null but it said i need a 'variant'
(lol, thumbsdown icon was accidental.. srry. )Code:var Arr: array [0..5] of TObject; begin //Stuff here.... for i:=0 to high(Arr) do if Arr[i] = nil then ShowMessage( Format('Position %d is NIL.', [i]) ); end;
Coders rule nr 1: Face ur bugz.. dont cage them with code, kill'em with ur cursor.
Just little details to add:
If you do this to free item, arr[3] will not be nil
arr[3].Free;
These 2 are the recommended ways:
FreeAndNil(arr[3]);
or
arr[3].Free;
arr[3]:=nil;
----
If you use neither of the 2 methods to nil it, then you might try this check:
If Assigned(arr[3]) then // object exists
// But if it was Freed before without nil i don't really know if there is a chance to AV or misinformation
However Assigned() is slower than if you'd simply check the nil.
thanks both, I think im ready to do memory management now (?)
Bookmarks