Re: Detecting type of variable ?



Normally I don't post my units and test code... but this unit is kinda funny
and cool so here goes:

Not sure if it will work in practice though as noted in the comments...
gonna try it though :D

*** Begin of Debug program ***

program DebugProgramForGenericBooleanOperationsVersion001;

{$APPTYPE CONSOLE}

{

Debug program for Generic Boolean Operations.

version 0.01 created on 25 january 2007 by Skybuck Flying.

proper unit and testing code.

How these will perform for doubles in real world scenerio's remains to be
seen ;)

}

uses
SysUtils,
Unit_GenericBooleanOperations_version_001 in
'..\..\..\Units\Technical\GenericBooleanOperations\Unit_GenericBooleanOperations_version_001.pas';

procedure Test( ParaVariable : variant );
begin
if HasBooleanValue( ParaVariable ) then
begin
if GenericIsTrue(ParaVariable) then
begin
writeln('true');
end else
if GenericIsFalse(ParaVariable) then
begin
writeln('false');
end;
end else
begin
writeln('unknown');
end;
end;

type
TvariableType = boolean;
// TvariableType = double;

var
vVariable : TvariableType;

begin
// double test
vVariable := variant(0.0);
write('0.0: ');
Test( vVariable );

// double test
write('1.0: ');
vVariable := variant(1.0);
Test( vVariable );

// double test
write('-1.0: ');
vVariable := variant(-1.0);
Test( vVariable );

// double test
write('0.5: ');
vVariable := variant(0.5);
Test( vVariable );

// double test
write('-0.5: ');
vVariable := variant(-0.5);
Test( vVariable );

// double test
write('10.5: ');
vVariable := variant(10.5);
Test( vVariable );

// double test
write('-10.5: ');
vVariable := variant(-10.5);
Test( vVariable );

// boolean test
write('false: ');
vVariable := variant(false);
Test( vVariable );

// boolean test
write('true: ');
vVariable := variant(true);
Test( vVariable );

readln;
end.

*** End of Debug program ***

*** Begin of Unit ***

unit Unit_GenericBooleanOperations_version_001;

{

Unit_GenericBooleanOperations

version 0.01 created on 25 january 2007 by Skybuck Flying.

How these will perform for doubles in real world scenerio's remains to be
seen ;)

}

interface

function HasBooleanValue( ParaVariable : variant ) : boolean;
function GenericIsTrue( ParaVariable : variant ) : boolean;
function GenericIsFalse( ParaVariable : variant ) : boolean;

implementation

var
GeneralTrue : variant;
GeneralFalse : variant;

function HasBooleanValue( ParaVariable : variant ) : boolean;
begin
result := false;

if (ParaVariable = GeneralTrue) or (-variant(ParaVariable) = GeneralTrue)
then
begin
result := true;
end else
if (ParaVariable = GeneralFalse) then
begin
result := true;
end;
end;

function GenericIsTrue( ParaVariable : variant ) : boolean;
begin
result := false;

if (ParaVariable = GeneralTrue) or (-Variant(ParaVariable) = GeneralTrue)
then
begin
result := true;
end;
end;

function GenericIsFalse( ParaVariable : variant ) : boolean;
begin
result := false;

if (ParaVariable = GeneralFalse) then
begin
result := true;
end;
end;

initialization
GeneralTrue := variant(true);
GeneralFalse := variant(false);


end.

*** End of Unit ***

Bye,
Skybuck.



.



Relevant Pages

  • Re: Detecting type of variable ?
    ... false in a boolean or in a double no matter which type is used. ... vVariable: TvariableType; ... GeneralFalse: variant; ...
    (alt.comp.lang.borland-delphi)
  • Re: Detecting type of variable ?
    ... false in a boolean or in a double no matter which type is used. ... vVariable: TvariableType; ... GeneralFalse: variant; ...
    (alt.comp.lang.borland-delphi)
  • Re: Detecting type of variable ?
    ... function HasBooleanValue(ParaVariable: variant): boolean; ... function GenericIsTrue(ParaVariable: variant): boolean; ... GenericFalse: variant; ...
    (alt.comp.lang.borland-delphi)
  • Re: Boolean-Variable per default auf FALSE?
    ... kein Boolean, er kann einen beinhalten. ... Ja, genau das hat er auch, nämlich False oder True. ... Ein Variant kann alles mögliche, ... Select Case VarType ...
    (microsoft.public.de.vb)
  • Re: VariantBool, Bool, Int, False, And Not
    ... Function B1As Boolean ... CopyVariant b ... Sub CopyVariant(ByRef v As Variant) ...
    (microsoft.public.de.vb)