Welcome, Guest
Username: Password: Remember me
Share your code snippets, screen shots etc. here
  • Page:
  • 1
  • 2

TOPIC:

Interpolated Strings 16 Aug 2018 12:07 #5822

  • FFF
  • FFF's Avatar
  • Topic Author


  • Posts: 1377
  • Guys, Wolfgang,
    just found "the" thing about interpolated strings, and thought maybe i'm not the only one a bit slow ;)

    VAR i:= 4 AS INT
    VAR c AS STRING
    c:= i"Bla {i} blabla"

    i.e.: the syntax allows to insert ANY type into a string, handling silently the conversions! That's really cool.

    Even something like
    c:= i"Bla {oMainWindow} blabla"
    works, inserting the classname.

    So you might consider adding/adapting the docs, as up2now the samples insert only strings, which is relatively pointless ;)
    Karl
    Regards
    Karl (X# 2.13.0.6; Xide 1.32; W8.1/64 German)

    Please Log in or Create an account to join the conversation.

    Interpolated Strings 16 Aug 2018 14:11 #5825

    • wriedmann
    • wriedmann's Avatar


  • Posts: 3185
  • Hi Karl,
    Currently I am on holiday, will add better samples after my return.
    Wolfgang
    Wolfgang Riedmann
    Meran, South Tyrol, Italy

    www.riedmann.it - docs.xsharp.it

    Please Log in or Create an account to join the conversation.

    Interpolated Strings 16 Aug 2018 14:43 #5826

    • FFF
    • FFF's Avatar
    • Topic Author


  • Posts: 1377
  • Ey, saw your "status" in whatsapp - cool location....
    Enjoy your free days, i'm almost at the end of mine <sigh>

    Karl
    Regards
    Karl (X# 2.13.0.6; Xide 1.32; W8.1/64 German)

    Please Log in or Create an account to join the conversation.

    Interpolated Strings 16 Aug 2018 18:09 #5827

    • Karl-Heinz
    • Karl-Heinz's Avatar


  • Posts: 773
  • Hi Karl,

    I'm eagerly waiting for the next C# feature to become true.

    local dw as dword

    dw := 12_123_789

    instead of the boring

    dw := 12123789

    ;-)

    regards
    Karl-Heinz

    Please Log in or Create an account to join the conversation.

    Interpolated Strings 16 Aug 2018 18:27 #5828

    • Chris
    • Chris's Avatar


  • Posts: 3650
  • I like that a lot, too!
    XSharp Development Team
    chris(at)xsharp.eu

    Please Log in or Create an account to join the conversation.

    Interpolated Strings 16 Aug 2018 20:49 #5830

    • FFF
    • FFF's Avatar
    • Topic Author


  • Posts: 1377
  • Could it be that you didn't get the drift of my post?
    All samples i saw for i"..." used strings as insertion, and that is really old, as i can do the same with simple +...+, so i never understood the usecase :)
    But for any other type it comes handy...

    K.
    Regards
    Karl (X# 2.13.0.6; Xide 1.32; W8.1/64 German)

    Please Log in or Create an account to join the conversation.

    Interpolated Strings 16 Aug 2018 22:19 #5831

    • Karl-Heinz
    • Karl-Heinz's Avatar


  • Posts: 773
  • ? 12.22 + " " + today() + " " + setcentury() + " " + 12123789

    Maybe even that will work someday ;-)

    ? 12.22 + " " + today() + " " + setcentury() + " " + 12_123_789

    Please Log in or Create an account to join the conversation.

    Interpolated Strings 16 Aug 2018 22:27 #5834

    • FFF
    • FFF's Avatar
    • Topic Author


  • Posts: 1377
  • That all works (but only) for "?"...
    Regards
    Karl (X# 2.13.0.6; Xide 1.32; W8.1/64 German)

    Please Log in or Create an account to join the conversation.

    Interpolated Strings 17 Aug 2018 06:48 #5835

    • robert
    • robert's Avatar


  • Posts: 3176
  • Mmm, that (underscore delimiters inside numeric literals) should not be too difficult to implement I think.
    I'll see what I can do.
    Robert
    XSharp Development Team
    The Netherlands

    Please Log in or Create an account to join the conversation.

    Last edit: by robert.

    Interpolated Strings 17 Aug 2018 08:29 #5836

    • MathiasHakansson
    • MathiasHakansson's Avatar


  • Posts: 50
  • I use this a lot. I have an old C# application built around datasets and i use this when building sql strings.

    plInfo.SqlUpdate =
    $@"UPDATE {Const.TBL_PRICE_LIST_STRUCT}
    SET
    {Const.COL_COLUMN_NUM} = {dataReader.SqlValueString(Const.COL_COLUMN_NUM)}
    ,{Const.COL_START_POSITION} = {dataReader.SqlValueString(Const.COL_START_POSITION)}
    ,{Const.COL_END_POSITION} = {dataReader.SqlValueString(Const.COL_END_POSITION)}
    ,{Const.COL_IS_ACTIVE} = {dataReader.SqlValueString(Const.COL_IS_ACTIVE)}
    ,{Const.COL_IMPORT_FORMAT} = {dataReader.SqlValueString(Const.COL_IMPORT_FORMAT)}
    ,{Const.COL_EDIT_UID} = {dataReader.SqlValueString(Const.COL_EDIT_UID)}
    ,{Const.COL_EDIT_DATE} = '{YYYYMMDDHHMMSS}'
    ,{Const.COL_CREATE_UID} = {dataReader.SqlValueString(Const.COL_CREATE_UID)}
    ,{Const.COL_CREATE_DATE} = {dataReader.SqlValueString(Const.COL_CREATE_DATE)}
    ,{Const.COL_PRICE_FORMAT_NUM} = {dataReader.SqlValueString(Const.COL_PRICE_FORMAT_NUM)}
    WHERE {Const.COL_PLS_NUM} = {dataReader.SqlValueString(Const.COL_PLS_NUM)}";

    Please Log in or Create an account to join the conversation.

    Interpolated Strings 17 Aug 2018 08:37 #5837

    • MathiasHakansson
    • MathiasHakansson's Avatar


  • Posts: 50
  • Now, it didn't look that good in my message, but in the souce code it's formatted so that all equals are on the same tab level. This makes it really readable, and a lot better than adding strings together.

    /Mathias

    Please Log in or Create an account to join the conversation.

    Interpolated Strings 17 Aug 2018 08:53 #5838

    • MathiasHakansson
    • MathiasHakansson's Avatar


  • Posts: 50
  • and one more thing...

    This is the method for formatting values for sql expressions. When I format the date you can see that it's also possible to select which date format to use. I suppose this is possible in XSharp as well.

    public static class DataReaderExtensions
    {
    public static string SqlValueString(this SqlDataReader reader, string colName)
    {
    object value = reader[colName];
    Type type = value.GetType();

    if (type == typeof(DBNull))
    return "NULL";

    if (ArrayHelper.InList(type,typeof(string),typeof(bool)))
    return $"'{value}'";

    if (type == typeof(DateTime))
    return $"'{value:s}'";

    return $"{value}";
    }
    }

    /Mathias

    Please Log in or Create an account to join the conversation.

    Interpolated Strings 17 Aug 2018 09:13 #5839

    • FFF
    • FFF's Avatar
    • Topic Author


  • Posts: 1377
  • Don't get me wrong, but what exactly has this to do with interpolated strings?

    K.
    Regards
    Karl (X# 2.13.0.6; Xide 1.32; W8.1/64 German)

    Please Log in or Create an account to join the conversation.

    Interpolated Strings 17 Aug 2018 09:17 #5840

    • MathiasHakansson
    • MathiasHakansson's Avatar


  • Posts: 50
  • It's examples of interpolated strings.
    /Mathias

    FFF wrote: Don't get me wrong, but what exactly has this to do with interpolated strings?

    K.

    Please Log in or Create an account to join the conversation.

    Interpolated Strings 18 Aug 2018 00:38 #5852

    • Chris
    • Chris's Avatar


  • Posts: 3650
  • Hi Karl,

    I was also confused at first, but $ is the symbol c# uses to specify interpolated strings, Matthias's sample code is full of them :)

    Chris
    XSharp Development Team
    chris(at)xsharp.eu

    Please Log in or Create an account to join the conversation.

    Interpolated Strings 19 Aug 2018 20:11 #5866

    • wriedmann
    • wriedmann's Avatar


  • Posts: 3185
  • Hi Karl,

    since I have been returned from my trip to the US (South Carolina and Georgia), I have changed the topic in the wiki:

    docs.xsharp.it/doku.php?id=strings

    Please let me know if I should add or change something else.

    Wolfgang
    Wolfgang Riedmann
    Meran, South Tyrol, Italy

    www.riedmann.it - docs.xsharp.it

    Please Log in or Create an account to join the conversation.

    Interpolated Strings 19 Aug 2018 21:48 #5868

    • FFF
    • FFF's Avatar
    • Topic Author


  • Posts: 1377
  • Hi Wolfgang,
    hope you had a good trip.
    Had a look, seems fine. Maybe in:
    cString := ei"this is a \"string\". that references {cLocalVar}"
    i'd use:
    cString := ei"this is a \"string\", which resolves {xWhateverExpression}"

    Lazy folks (like me ;)) might skip the new line you added behind. Also the "LocalVar" term implies, that there's a harder restriction of what may be used.

    Karl
    Regards
    Karl (X# 2.13.0.6; Xide 1.32; W8.1/64 German)

    Please Log in or Create an account to join the conversation.

    Interpolated Strings 20 Aug 2018 06:02 #5869

    • wriedmann
    • wriedmann's Avatar


  • Posts: 3185
  • Hi Karl,

    I have changed the article again, hopefully it is easier to understand now. Interpolated and extended strings are a really useful thing.

    Wolfgang

    P.S. yes, my holiday was great (and too short as all of them). We have hiked a lot and seen many animals (including alligators) in their habitats.
    Wolfgang Riedmann
    Meran, South Tyrol, Italy

    www.riedmann.it - docs.xsharp.it

    Please Log in or Create an account to join the conversation.

    Interpolated Strings 20 Aug 2018 09:30 #5879

    • MathiasHakansson
    • MathiasHakansson's Avatar


  • Posts: 50
  • Hi,

    sorry for not explaining more...

    Yes, In C# you use $ for interpolated strings and @ for verbatim strings. The two things I wanted to show were;

    1. SQL-statement (or text file row creation) strings can get a lot more readable with interpolated strings and verbatim strings in combination. If you compare with using StringBuilder or string concatination with +, there is a really big difference. As you can see I have all field names as constants which is really usefull if you want to find all occurrences of a field.
    2. You can also specify the format. In my case I specified the date format in the SqlValueString method. You can also format numbers in a similar way.

    /Mathias

    Please Log in or Create an account to join the conversation.

    Last edit: by MathiasHakansson.

    Interpolated Strings 23 Aug 2018 08:59 #5906

    • MathiasHakansson
    • MathiasHakansson's Avatar


  • Posts: 50
  • Found one more example of date formating that I have used. This one may be a little more useful....

    In this case a filename is constructed by combining a language resource string with a formatted date.
    string fileName = $"{AutoUpdatePricelistsFormLabels.LostPriceLinks} {DateTime.Now:yyyyMMdd HHmmss}.xlsx";

    /Mathias

    Please Log in or Create an account to join the conversation.

    • Page:
    • 1
    • 2