Jump to content
Ian Branch

Assigned(ErrorMessage) problem.

Recommended Posts

Hi Team,

I have a function definition of ..

function ValidEmail(const EmailAddress: string; out ErrorMessage: string): Boolean;
const
  EMAIL_REGEX = '^((?>[a-zA-Z\d!#$%&''*+\-/=?^_`{|}~]+\x20*|"((?=[\x01-\x7f])' + '[^"\\]|\\[\x01-\x7f])*"\x20*)*(?<angle><))?((?!\.)' +
    '(?>\.?[a-zA-Z\d!#$%&''*+\-/=?^_`{|}~]+)+|"((?=[\x01-\x7f])' + '[^"\\]|\\[\x01-\x7f])*")@(((?!-)[a-zA-Z\d\-]+(?<!-)\.)+[a-zA-Z]' +
    '{2,}|\[(((?(?<!\[)\.)(25[0-5]|2[0-4]\d|[01]?\d?\d))' + '{4}|[a-zA-Z\d\-]*[a-zA-Z\d]:((?=[\x01-\x7f])[^\\\[\]]|\\' + '[\x01-\x7f])+)\])(?(angle)>)$';
var
    ....
    ....

The requirement is two fold.

1.  ErrorMessage is optional in the calling program.  i.e. it may be just - if validemail('myemail@somewhere.com') then ....

2.  Assign an error message to ErrorMessage if the construction of the email address is incorrect.

    if (not Result) and Assigned(ErrorMessage) then
      ErrorMessage := 'The following email addresses are invalid: ' + String.Join('; ', InvalidEmails.ToArray);

My problem is that I get an 'Incompatible types' error message for the 'then' in the above.

I don't understand why.  If I have read the help correctly, Assigned(Errormessage) should return a true or false, depending if Errormessage has been 'called'.??

Can somebody enlighten me please?

What do I need to do to meet my needs?

 

Regards & TIA,

Ian

Share this post


Link to post

"ErrorMessage" is a "string", then is not same than an object/property-event for example, to Assigned()  usage. you should use:

if ErrorMessage = ''  or ErrorMessage.IsEmpty... then...

but in your case above, it start "empty"

 

but you cannot known if "it" was defined or not, normally, it should be referenced in the source, like:

 

var

   LText:string;

begin

  ValidEmail( xxxxx, LText);  

 

because "out ErrorMessage" that needs a "var" to received value returned when out-scope.

 

"out" is almost the same than "var"

out  = out

var = in/out

Edited by programmerdelphi2k

Share this post


Link to post
21 minutes ago, Ian Branch said:

if (not Result)  then

ErrorMessage := 'The following email addresses are invalid: ' + String.Join('; ', InvalidEmails.ToArray);

else, LText will be "empty"!

Edited by programmerdelphi2k

Share this post


Link to post

Hi p2k,

OK on the errormessage/object aspect.

 

16 minutes ago, programmerdelphi2k said:

but you cannot known if "it" was defined or not, normally, it should be referenced in the source

Quite so.  I use the basic routine in multiple locations/projects and it doesn't have the error message functionality.

I was looking to do a Global replace of old for new.

Then update the many calling instances at my leisure.

Edited by Ian Branch

Share this post


Link to post

now, you know "why" use "out Params" - always to receive/to transport some, never like "var Vars"

Edited by programmerdelphi2k

Share this post


Link to post

Mmmm.    Makes    "If not ValidEmail('khggh.jghfjgh.vbn') then ..." more complicated and I would still have to revist every call.

Thanks for your thoughts.

I guess I will have to vistit every call.

 

Regards & Tks again,

Ian

Share this post


Link to post
15 hours ago, Ian Branch said:

How does one make a parameter such as this optional?

Out parameters can't be optional.  You will have to overload the function instead, eg:

function DoValidateEmail(const EmailAddress: string; ErrorMessage: PString): Boolean;
begin
  ...
  if (not Result) and Assigned(ErrorMessage) then
    ErrorMessage^ := ...;
  ...
end;

function ValidEmail(const EmailAddress: string; out ErrorMessage: string): Boolean; overload;
begin
  Result := DoValidateEmail(EmailAddress, @ErrorMessage);
end;

function ValidEmail(const EmailAddress: string): Boolean; overload;
begin
  Result := DoValidateEmail(EmailAddress, nil);
end;

Alternatively:

function ValidEmail(const EmailAddress: string; out ErrorMessage: string): Boolean; overload;
begin
  ...
  if not Result then
    ErrorMessage := ...;
  ...
end;

function ValidEmail(const EmailAddress: string): Boolean; overload;
var
  LDiscarded: string;
begin
  Result := ValidEmail(EmailAddress, LDiscarded);
end;

 

Share this post


Link to post

Hi Remy,

Great minds think alike.  There is my mind....

I was laying in bed at 2am mulling the issue, as you do, when it occurred to me that that was what I needed to do.

 

Regards & Tks,

Ian

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×