![]() |
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; |
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. |
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!
|
Re: Typcasting: TVarData -> TCustomVariantType
Hat denn keiner ne Idee???
:gruebel: :wall: |
Alle Zeitangaben in WEZ +1. Es ist jetzt 07:48 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz