Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Problem with variants

Posted on 2006-10-25
2
Medium Priority
?
674 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 400 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

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…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

721 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