Ian Branch 127 Posted April 25, 2023 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
programmerdelphi2k 237 Posted April 25, 2023 (edited) "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 April 25, 2023 by programmerdelphi2k Share this post Link to post
programmerdelphi2k 237 Posted April 25, 2023 (edited) 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 April 25, 2023 by programmerdelphi2k Share this post Link to post
Ian Branch 127 Posted April 25, 2023 (edited) 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 April 25, 2023 by Ian Branch Share this post Link to post
programmerdelphi2k 237 Posted April 25, 2023 (edited) now, you know "why" use "out Params" - always to receive/to transport some, never like "var Vars" Edited April 25, 2023 by programmerdelphi2k Share this post Link to post
Ian Branch 127 Posted April 25, 2023 How does one make a parameter such as this optional? Share this post Link to post
programmerdelphi2k 237 Posted April 25, 2023 (edited) out needs always a var.... use some like this: xxxx( xxxx; var AOut: string = "") Edited April 25, 2023 by programmerdelphi2k Share this post Link to post
Ian Branch 127 Posted April 25, 2023 But it still requires the parameter in the calling function. Share this post Link to post
programmerdelphi2k 237 Posted April 25, 2023 see if possible ... var AOut = ""); Share this post Link to post
programmerdelphi2k 237 Posted April 25, 2023 if using ... AOut=""); works. then maybe needs change to function xxxx(....): string; Share this post Link to post
programmerdelphi2k 237 Posted April 25, 2023 type TRecResult = record b:boolean; m: string; ... function xxxx(...): TRecResult; Share this post Link to post
Ian Branch 127 Posted April 25, 2023 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
Remy Lebeau 1400 Posted April 25, 2023 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
Ian Branch 127 Posted April 25, 2023 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