I think all of us know the how works COALESCE SQL function. True? In a simple version, evaluates two arguments and if the first is NULL, returns the second.

I think all of us know the how works COALESCE SQL function. True? In a simple version, evaluates two arguments and if the first is NULL, returns the second.

I need this behaviour in Delphi.

Anyone know a standard function like this.
--------------------------
The problem becomes because I have a function that receives two parameters of type char and I need to send to it as the argument the first character of a TField and this TField can be null.

ReorganizeCoordinatesControls(
FEditComponent.LAYOUT.AsString[1],
FEditComponent.ORIENTATION.AsString[1]);

when is null, taking the first character causes an AVE. Apart, a variable of type char, can't receive an empty string.

-----------------------------

Of course, I know how to create the function. The curiosity is about if a standard function like this exists in the Delphi libraries.

Of course, I've searched on google, in forums, in stackoverflow and so on.

Anyone knows?
https://docs.microsoft.com/es-es/sql/t-sql/language-elements/coalesce-transact-sql?view=sql-server-2017

Comments

  1. Not quite sure what you are looking for, but a simple
    (FEditComponent.LAYOUT.AsString + #20)[1] should do it. Just change the to whatever default you need.

    ReplyDelete
  2. There is "IfThen" from StrUtils.pas, which comes close to Coalesce:

    function IfThen(
    AValue: Boolean;
    const ATrue: string;
    AFalse: string = ''): string;

    To use it, pass the result of IfThen() to ReorganizeCoordinatesControls():

    IfThen(FEditComponent.LAYOUT.IsNull, #0, FEditComponent.LAYOUT.AsString[1])

    ReplyDelete
  3. Achim Kalwa Unfortunately that won't work. IfThen is a function and all parameters are evaluated for the call, which will give the same AV.

    ReplyDelete
  4. Why not this:

    ReorganizeCoordinatesControls(
    FEditComponent.LAYOUT,
    FEditComponent.ORIENTATION);

    Then have ReorganizeCoordinatesControls determine which TField is assigned and take the first character from the .AsString.

    ReplyDelete
  5. Finally, I did this:

    this is the call
    ReorganizeCoordinatesControls(
    Coalesce(FEditComponent.LAYOUT, '#'),
    Coalesce(FEditComponent.ORIENTATION, '#'));

    This is the method

    function TConfigurationsController.Coalesce(
    AField :TStringField; IfEmpty :char):char;
    begin
    if AField.IsNull then Result := IfEmpty
    else Result := AField.AsString[1];
    end;


    Uwe Raabe
    Your solution is good. But I think is more like a trick, not a technique.

    And more, it assumes that the dataset that owns the TField is in Edit mode, and maybe not.

    I hate the tricks. They complicate the maintenance of the code.

    I know. ...a good comment, near it... but... As Kernighan and Plauger emphasize, "Don't document bad code, rewrite it"

    Anyway, thanks to all.

    ReplyDelete
  6. Attila Kovacs

    You have reason about the bug.

    Here is the fixing:

    function TConfigurationsController.Coalesce(
    AField :TStringField; IfEmpty :char):char;
    begin
    if (AField.IsNull) or (AField.AsString.Trim = '') then Result := IfEmpty
    else Result := AField.AsString[1];
    end;


    And you have reason about the other solution:

    It's a solid trick. At least if the DataSet is in any edit Mode




    Jokes apart. Thanks for pointing me the bug.

    ReplyDelete
  7. Juan C. Cilleruelo What makes you think that the dataset has to be in edit mode?

    ReplyDelete

Post a Comment