Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Object-Pascal / Delphi-Language (https://www.delphipraxis.net/32-object-pascal-delphi-language/)
-   -   Delphi Typcasting: TVarData -> TCustomVariantType (https://www.delphipraxis.net/13433-typcasting-tvardata-tcustomvarianttype.html)

scream 18. Dez 2003 13:23


Typcasting: TVarData -> TCustomVariantType
 
Hallo erstmal.
Ich bin relativer Neueinsteiger in Delphi und erstelle mir gerade eine Klasse namens TFrac. Diese Klasse ist einfach ein Bruch (ja, des Teil aus'm Mathematikunterricht, genau ;) ). Jetzt möchte ich "comparison operators", zu Deutsch, Vergleichsoperationen einbinden, werde aus der Dokumentation aber nicht recht schlau. Ich habe nun die Funktion TCustomVariantType.compare eingebunden, aber ich krieg das nicht ganz hin:
Ich habe in meiner Klasse TFrac eine Funktion namens toFloat, die den Bruch in eine Gleitkommazahl umwandelt. Jetzt möchte ich in der Procedure "Compare( const Left, Right: TVarData; var Relationship: TVarCompareResult )" einfach "Left" und "Right" als TFrac behandeln und dann halt Left.toFloat und right.toFloat verlgeichen. Aber wie mache ich das? Wie behandle ich ein TVarData als meine eigene Klasse TFrac?

Hier ist mein Code. Die Funktionen expand, add, subtract, multiply, divide und cancelDown (deutsch: kürzen) habe ich der Übersicht halber ausgelassen ;):

Delphi-Quellcode:
type TVarCompareResult = (crLessThan, crEqual, crGreaterThan);
 
type
  TFrac = class(TCustomVariantType)
    zaehler: Integer;
    nenner: Integer;
    VarType: TVarType;

    //Verschiedene Prozeduren und Funktionen
    constructor create( tempZaehler: Integer; tempNenner: Integer );
    procedure Compare( const Left, Right: TVarData; var Relationship: TVarCompareResult );
    function toFloat: real;
    function expand( a: Integer ): TFrac;
    function add( a: TFrac ): TFrac;
    function subtract( a: TFrac ): TFrac;
    function multiply( a: TFrac ): TFrac;
    function divide( a: TFrac ): TFrac;
    function cancelDown(): TFrac;
end;

implementation

constructor TFrac.create( tempZaehler: integer; tempNenner: integer );
begin
    zaehler := tempZaehler;
    nenner := tempNenner;
    VarType := $0666;
end;

procedure TFrac.Compare( const Left, Right: TVarData; var Relationship: TVarCompareResult );
begin
    if ( Left.VType = VarType ) and ( Right.VType = VarType ) then
    begin
        if TFrac(Left).toFloat = Right.toFloat() then
            Relationship := crEqual
        else if Left.toFloat() < Right.toFloat() then
            Result := crLessThan
        else if Left.toFloat() > Right.toFloat() then
            Result := crGreaterThan;
    end;
end;

function TFrac.toFloat: real;
begin
    toFloat := zaehler / nenner;
end;

Touchdown 18. Dez 2003 15:33

Re: Typcasting: TVarData -> TCustomVariantType
 
Ungleiche Typen werden nicht gleich, nur wenn am wie wild darauf Typcastet.

Ich zumindest sehe keine Typengleichheit, die ein Typcasten ermöglichen würde.

scream 18. Dez 2003 19:30

Re: Typcasting: TVarData -> TCustomVariantType
 
Geh mal davon aus, dass auf beiden Seiten immer ein TFrac steht... also sowohl Left als auch Right ein TFrac ist. Natürlich ist das etwas :gruebel:, aber mehr brauch ich ja garnet, weil mein Prog eh nur TFrac benutzt!

scream 21. Dez 2003 13:26

Re: Typcasting: TVarData -> TCustomVariantType
 
Hat denn keiner ne Idee???
:gruebel: :wall:


Alle Zeitangaben in WEZ +1. Es ist jetzt 15:37 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz