Solved

Problem with variants

Posted on 2006-10-25
2
660 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:
ciuly earned 100 total points
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

772 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

10 Experts available now in Live!

Get 1:1 Help Now