Jump to content
Sign in to follow this  
azrael_11

Using a var array in procedure paremetre

Recommended Posts

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

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
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

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
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
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

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 by David Heffernan

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×