jesu 0 Posted Monday at 11:04 AM Hello. I'm trying to create a function that can return a float or nil: function myfunc(const pa_value:string): double; begin if pa_value = whatever then result := -1 else result := nil; end; to replace many lines like this (simplified code): if check then parambyname('myparam').asfloat := -1 else parambyname('myparam').clear; What is the right way to do that? Thanks. Share this post Link to post
Virgo 18 Posted Monday at 11:28 AM Using variant? function myfunc(const pa_value:string): variant; begin if pa_value = whatever then result := -1 else result := null; end; and then parambyname('myparam').AsVariant := myfunc(); Share this post Link to post
FPiette 379 Posted Monday at 12:53 PM It depends on what your function does, but just like a function returning an integer returns a special value (often -1 or 0) in case of error, your function could return a special value as a double number. Carefully select the value so that it doesn't collide with expected values in your application. I'm think about a value like 2.12345e-308. Just define a constant to give that value a nice name easy to use and remember. Share this post Link to post
Anders Melander 1756 Posted Monday at 01:25 PM 30 minutes ago, FPiette said: Carefully select the value so that it doesn't collide with expected values in your application. I'm think about a value like 2.12345e-308. Just define a constant to give that value a nice name easy to use and remember. NaN 1 Share this post Link to post
FPiette 379 Posted Monday at 03:06 PM 1 hour ago, Anders Melander said: 2 hours ago, FPiette said: Carefully select the value so that it doesn't collide with expected values in your application. I'm think about a value like 2.12345e-308. Just define a constant to give that value a nice name easy to use and remember. NaN Anders, I'm not sure that NaN is a good choice because this special value is generated in other cases. Something like 2.12345e-308is a better choice and collision risk is much lower. Share this post Link to post
Anders Melander 1756 Posted Monday at 03:14 PM I'm pretty sure that anyone asking for help with something as basic as this isn't handling, or even aware of, NaN. Regardless, I personally wouldn't solve the problem with a magic value. 1 Share this post Link to post
Der schöne Günther 314 Posted Monday at 03:20 PM (edited) Jesu mentioned using Data.DB.TParams.ParamByName which returns a Data.DB.TParam object. TParam has a property Value which is a Variant. As much as I loathe these things, the approach by @Virgo seems to be the most fitting. Edited Monday at 03:21 PM by Der schöne Günther 1 Share this post Link to post
Anders Melander 1756 Posted Monday at 05:26 PM It's impossible to suggest a good solution without knowing more about the context. If it's just a case of either setting or clearing a TParam value then just pass the TParam to the function and let the function operate directly on that. Share this post Link to post
Brandon Staggs 243 Posted Monday at 06:02 PM You're better off doing something like function myfunc(const InVal: String; out OutVal: Double): Boolean; Return True if the value was set and false if not. There are other ways to handle this but the worst way is a magic value in Double. There's no reason to do that when it is so easy to indicate explicitly whether or not the value is valid. 4 Share this post Link to post
dummzeuch 1479 Posted Tuesday at 06:24 AM I usually prefix functions like this with "Try", so instead of function GetTheValue(parameters ...): SomeType; it's function TryGetTheValue(parameters ...; out Value: SomeType): Boolean; 2 Share this post Link to post
Roger Cigol 101 Posted Tuesday at 09:10 AM 2 hours ago, dummzeuch said: TryGetTheValue( Very good suggestion by @dummzeuch Very few people give enough thought to naming of functions and variables. Developing a consistent approach can significantly help with long term program readability / long term support for large projects. Anything that hints at doing that gets my full support ! https://cigolblog.wordpress.com/2023/01/ https://cigolblog.wordpress.com/2019/10/ https://cigolblog.wordpress.com/2017/06/ Share this post Link to post