Solved

Problem with variants

Posted on 2006-10-25
2
662 Views
Last Modified: 2008-03-17
Hi all,

I have an object ( cut down version)

  TObjField = class( TObject)
    private
      FName: string;
      FValue: Variant;
      FFieldType: TFieldType;

      function GetAsFormattedString: string;
    public
      property Name: string           read FName      write FName;
      property Value: Variant         read FValue     write FValue;
      property FieldType: TFieldType  read FFieldType write FFieldType;
    end;

In my function GetAsFormattedString I am having problems.

function TObjField.GetAsFormattedString: string;
begin
  Result := '';

  if FValue = unassigned then Exit;

    case FFieldType of
      ftString,
      ftMemo      : Result := Trim( FValue);
      ftInteger,
      ftSmallint  : Result := IntToStr( FValue);
      ftFloat,
      ftBCD       : Result := Trim( FloatToStrF( FValue, ffFixed, 18, 2));
      ftCurrency  : Result := Trim( FloatToStrF( FValue, ffCurrency, 18, 2));
      ftDate      : Result := Trim( FormatDateTime( 'dd/mm/yyyy', FValue));
      ftBoolean   : if FValue then
                      Result := 'True'
                    else
                      Result := 'False'
    end;
end;

Now in code I have 2 field objects with the various properties

FFieldType = ftsmallint
with
FValue = 0

and

FFieldType = ftDate
with
FValue = unassigned

When I call the GetAsFormattedString for both of the objects,
the FValue = unassigned for BOTH of them is TRUE.

Why does the integer object also think 0 is unassigned?

0
Comment
Question by:mikelittlewood
2 Comments
 
LVL 28

Accepted Solution

by:
2266180 earned 100 total points
ID: 17801985
because unassigned uses varclear to make the necessary comparisons.
I have had such problems myself yesterday, also with varnull. my workaround is this function:

function isbadolevariant(v:olevariant):boolean;
var strtag:string;
begin
  result:=true;
  try
    if (VarType(v)=Vardispatch) and (integer(IDispatch(v))=0) then
      exit;
    strtag:=vartostr(v);
    if (strtag='0') or (strtag='1') then
      exit;
    result:=false;
  except
    on e:exception do
      showmessage('some error occured: '+e.message);
  end;
end;

of course I am working with interface types only so in my case a value of 0 or 1 (varempty and varnull) are invalid.

in your case you will probably have to work with testing the type as well :
if VarType(unassigned)=varempty then

ex:
var v:variant;
begin
  v:=VarAsType(0,varSmallint);
  if VarType(v) = varEmpty then
    showmessage('empty');
will not show the message.
wheras
  if VarType(unassigned) = varEmpty then
    showmessage('empty');
will show the message.

I don't know if I made a good exmplanation, but feel free to ask :)
0
 
LVL 15

Author Comment

by:mikelittlewood
ID: 17801992
ah ok.

Taking your Empty example I changed it to

if VarIsEmpty( FValue) then exit;

This seems to work fine for both.

Cheers for the pointer
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now