Dear knowledgeable ones.
Some time ago I made a few of my routines kind of universal, and I wonder if there is any way in Delphi (My version is Delphi9) that can be used to reuse this code in example below.
As you see, the two procedures are almost identical, it is just the typedeclaration that is different in the parameters passed to the procedures.

Maybe it is possible to use some kind of typecasting but my experience there is limited.

The code is not bad if only 2 similar blocks, I just see that it would be better with one block of code that handles sorting of maybe 10 different types of data..

Best regards, and thanks in advance for input on this issue.


Example of code:

PROCEDURE BinarySortIndex(VAR ArrParam,ArrParamIndex: DynamicIntegerArray; CONST HighBound:INTEGER); OVERLOAD;
    VAR
      x       : INTEGER;
      TempVar : INTEGER;
    BEGIN
      Lo:=0; Hi:=HighBound; Mid:=FindMid;
      TempVar := ArrParam[HighBound];
      REPEAT
        IF TempVar>ArrParam[ArrParamIndex[Mid]] THEN Lo:=Mid ELSE Hi:=Mid;
        Mid:=FindMid;
      UNTIL (Mid=Lo) OR (Mid=Hi);
      IF TempVar>ArrParam[ArrParamIndex[Mid]] THEN INC(Mid);// We always need a last check just in case.
      FOR x:=HighBound-1 DOWNTO Mid DO ArrParamIndex[x+1] := ArrParamIndex[x];// Shift the index.
      ArrParamIndex[Mid]:=HighBound;// Store the pointer to index at its sorted place
    END;

  PROCEDURE BinarySortIndex(VAR ArrParam:DynamicStringArray; VAR ArrParamIndex: DynamicIntegerArray; CONST HighBound:INTEGER); OVERLOAD;
    VAR
      x       : INTEGER;
      TempVar : STRING;
    BEGIN
      Lo:=0; Hi:=HighBound; Mid:=FindMid;
      TempVar := ArrParam[HighBound];
      REPEAT
        IF TempVar>ArrParam[ArrParamIndex[Mid]] THEN Lo:=Mid ELSE Hi:=Mid;
        Mid:=FindMid;
      UNTIL (Mid=Lo) OR (Mid=Hi);
      IF TempVar>ArrParam[ArrParamIndex[Mid]] THEN INC(Mid);// We always need a last check just in case.
      FOR x:=HighBound-1 DOWNTO Mid DO ArrParamIndex[x+1] := ArrParamIndex[x];// Shift the index.
      ArrParamIndex[Mid]:=HighBound;// Store the pointer to index at its sorted place
    END;

Recommended Answers

All 3 Replies

If both types have a common ancestor (assuming they are objects), then yes, it is possible. Can you post your type definition? (Or perhaps it is some Delphi type I've never used before?)

With Delphi 9, do you mean 2009 ? If so, you can use generics.

Btw, I'm collecting interesting links here, perhaps you find something useful there.

My TypeDefinition is simple enough:

TYPE
  DynamicIntegerArray : ARRAY OF INTEGER;
  DynamicStringArray : ARRAY OF STRING;

When transferring an array as a parameter, I have to make a type first, and then it works.

Those are not compatible. Don't think you can do this an easy way without changing these types. Generics would be your best bet in my opinion.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.