Solved

Problem with variants

Posted on 2006-10-25
2
673 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

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…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

724 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