Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Delphi 2010 Check if a PChar is Empty

Posted on 2010-09-02
4
Medium Priority
?
1,352 Views
Last Modified: 2012-05-10
Hello,

In Delphi 2007 i have the following function to check if a PChar is Empty...you should remember that in Delphi 2007 PChar = PAnsiChar and not PWideChar as actually in Delphi 2010 of course hehe

function EmptyPChar(P: PChar): Boolean;
begin
  try
    Result := (P = nil) or (P^ = #0);
  except
    Result := True;
  end;
end;

//Added to support PAnsiChar...
function EmptyPAnsiChar(P: PAnsiChar): Boolean;
begin
  try
    Result := (P = nil) or (P^ = #0);
  except
    Result := True;
  end;
end;

Works fine... but in Delphi 2010 making some tests like

type
  TSomething = record
    bla: PChar;
    ble: PChar;
    bli: PAnsiChar;
  end;

var
  Something: TSomething;
begin
  Something.ble := 'AAA';

  if EmptyPChar(Something.bla) then
  ShowMessage('Empty PChar'); //should appears... but isnt!

  if EmptyPAnsiChar(Something.bli) then
  ShowMessage('Empty PAnsiChar');

Should show both messages, since i just set BLE and not BLA but just show "Empty PAnsiChar"
But Something.bla is empty!
If i remove the bli: PAnsiChar; from the record, works fine!

Soembody has another or the right way to get compatible with Delphi 2010?

Regards,
Carlos
0
Comment
Question by:cebasso
  • 2
4 Comments
 
LVL 4

Expert Comment

by:irishbuddha
ID: 33588059
This actually works as noted in Delphi 2009 with a copy/paste of your functions and test.
I'll boot 2010 after lunch as I'm a bit low on resources at the moment and check it out there, but it's possible there is a configuration setting that may be getting in the way here.
0
 
LVL 26

Accepted Solution

by:
Russell Libby earned 2000 total points
ID: 33588100
Carlos,

Local variables can/will contain random data until a value is assigned to them. You should clear this record before attempting to examine the fields within.

  FillChar(Something, SizeOf(Something), 0);

Regards,
Russell
0
 
LVL 4

Author Closing Comment

by:cebasso
ID: 33588273
@rllibby yeahh really! i forgot about this :D

thank you!
0
 
LVL 4

Author Comment

by:cebasso
ID: 33588288
and sorry for this mistake/stupid question!

i'm very late in my project and now more and more since i need to get it working Delphi 2010
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

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…
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…
Screencast - Getting to Know the Pipeline
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…

782 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