azrael_11 0 Posted June 30, 2019 Hello I try to add a var array in procedure parametre. i try this but not working so procedure delete_an_element_from_array(var vArray: TArray; const vIndex: Cardinal); begin ...... end; How can i do this vArray get many array types like records, objects, integers, strings etc. Thank you. Share this post Link to post
David Heffernan 2345 Posted June 30, 2019 More details please. What is the error? What is TArray? I expect that it is your own type because the type defined in the RTL of that name is not an array, it's just a collection of static methods. Share this post Link to post
azrael_11 0 Posted June 30, 2019 31 minutes ago, David Heffernan said: More details please. What is the error? What is TArray? I expect that it is your own type because the type defined in the RTL of that name is not an array, it's just a collection of static methods. Error is that cant compile TArray is a try of mine to add a generics view of point like TArray<> but i cant do that either. The main goall here is the procedure to take any array that maybe is a records or objects or any else and delete the desire element of that array. Share this post Link to post
Uwe Raabe 2057 Posted June 30, 2019 You can use the approach shown in TArray from System.Generics.Collection: type TMyExtArray = class(TArray) public class procedure DeleteElement<T>(var Values: TArray<T>; const Index: Cardinal); end; class procedure TMyExtArray.DeleteElement<T>(var Values: TArray<T>; const Index: Cardinal); begin if (Index < Low(Values)) or (Index > High(Values)) then begin raise EArgumentOutOfRangeException.Create('argument out of range'); end; System.Delete(Values, Index, 1); end; Note that handling different types (intrinsic, pointer, reference counted) in the implementation part can be a bit tricky. Share this post Link to post
David Heffernan 2345 Posted June 30, 2019 Without knowing what types you are using we can only guess what you are doing. Share this post Link to post
azrael_11 0 Posted July 2, 2019 On 6/30/2019 at 3:02 PM, David Heffernan said: Without knowing what types you are using we can only guess what you are doing. On 6/30/2019 at 11:29 AM, Uwe Raabe said: You can use the approach shown in TArray from System.Generics.Collection: type TMyExtArray = class(TArray) public class procedure DeleteElement<T>(var Values: TArray<T>; const Index: Cardinal); end; class procedure TMyExtArray.DeleteElement<T>(var Values: TArray<T>; const Index: Cardinal); begin if (Index < Low(Values)) or (Index > High(Values)) then begin raise EArgumentOutOfRangeException.Create('argument out of range'); end; System.Delete(Values, Index, 1); end; Note that handling different types (intrinsic, pointer, reference counted) in the implementation part can be a bit tricky. Thank you very much. Sorry for the late response. David the arrays is set of record having more records and object types. The main goal here is. example type TmyRecord_2= Class(TObject) procedure : mine(); function : mine_2():boolean; ...... .... end; type TmyRecord = record one: array [0..10] TmyRecord_2 two: string; three ........ ..... end; var myHole_Array: array of TmyRecord; Create a dynamic set of myHole_Array about 100 entries Now i want to Delete the 7 entrie Now i want to Insert a new entrie in 16 position So far is good I Try to make a global procedure to delete or insert entries in different arrays or records. How can i add this Procedure Delete_Entrie_In_Array(myArray : here is the difficult part for me ; vDelete_Num: Cardinal); Thank you again. Share this post Link to post
azrael_11 0 Posted July 2, 2019 On 6/30/2019 at 11:29 AM, Uwe Raabe said: You can use the approach shown in TArray from System.Generics.Collection: type TMyExtArray = class(TArray) public class procedure DeleteElement<T>(var Values: TArray<T>; const Index: Cardinal); end; class procedure TMyExtArray.DeleteElement<T>(var Values: TArray<T>; const Index: Cardinal); begin if (Index < Low(Values)) or (Index > High(Values)) then begin raise EArgumentOutOfRangeException.Create('argument out of range'); end; System.Delete(Values, Index, 1); end; Note that handling different types (intrinsic, pointer, reference counted) in the implementation part can be a bit tricky. I'' try this when i found time Thank you Share this post Link to post
David Heffernan 2345 Posted July 2, 2019 (edited) You can use the Delete function directly if you have a dynamic array. It may be easier to use TList<T> though. However you will need to handle the nested class instances. Probably the design of your record type is a bad choice because it makes the lifetime very hard to manage. Edited July 2, 2019 by David Heffernan Share this post Link to post