fbpx
× Share your code snippets, screen shots etc. here

Interpolated Strings

More
5 months 5 days ago #1 by Karl Faller
Interpolated Strings was created by Karl Faller
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

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

More
5 months 5 days ago #2 by Wolfgang Riedmann
Replied by Wolfgang Riedmann on topic Interpolated Strings
Hi Karl,
Currently I am on holiday, will add better samples after my return.
Wolfgang

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

More
5 months 5 days ago #3 by Karl Faller
Replied by Karl Faller on topic Interpolated Strings
Ey, saw your "status" in whatsapp - cool location....
Enjoy your free days, i'm almost at the end of mine <sigh>

Karl

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

More
5 months 5 days ago #4 by Karl-Heinz Rauscher
Replied by Karl-Heinz Rauscher on topic Interpolated Strings
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.

More
5 months 5 days ago #5 by Chris Pyrgas
Replied by Chris Pyrgas on topic Interpolated Strings
I like that a lot, too!

XSharp Development Team
chris(at)xsharp.eu

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

More
5 months 5 days ago #6 by Karl Faller
Replied by Karl Faller on topic Interpolated Strings
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.

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

More
5 months 5 days ago #7 by Karl-Heinz Rauscher
Replied by Karl-Heinz Rauscher on topic Interpolated Strings
? 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.

More
5 months 5 days ago #8 by Karl Faller
Replied by Karl Faller on topic Interpolated Strings
That all works (but only) for "?"...

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

More
5 months 4 days ago - 5 months 4 days ago #9 by Robert van der Hulst
Replied by Robert van der Hulst on topic Interpolated Strings
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
This email address is being protected from spambots. You need JavaScript enabled to view it.
Last edit: 5 months 4 days ago by Robert van der Hulst.

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

More
5 months 4 days ago #10 by Mathias Håkansson
Replied by Mathias Håkansson on topic Interpolated Strings
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.

More
5 months 4 days ago #11 by Mathias Håkansson
Replied by Mathias Håkansson on topic Interpolated Strings
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.

More
5 months 4 days ago #12 by Mathias Håkansson
Replied by Mathias Håkansson on topic Interpolated Strings
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.

More
5 months 4 days ago #13 by Karl Faller
Replied by Karl Faller on topic Interpolated Strings
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.

More
5 months 4 days ago #14 by Mathias Håkansson
Replied by Mathias Håkansson on topic Interpolated Strings
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.

More
5 months 4 days ago #15 by Chris Pyrgas
Replied by Chris Pyrgas on topic Interpolated Strings
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.

More
5 months 2 days ago #16 by Wolfgang Riedmann
Replied by Wolfgang Riedmann on topic Interpolated Strings
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

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

More
5 months 2 days ago #17 by Karl Faller
Replied by Karl Faller on topic Interpolated Strings
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

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

More
5 months 1 day ago #18 by Wolfgang Riedmann
Replied by Wolfgang Riedmann on topic Interpolated Strings
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.

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

More
5 months 1 day ago - 5 months 1 day ago #19 by Mathias Håkansson
Replied by Mathias Håkansson on topic Interpolated Strings
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
Last edit: 5 months 1 day ago by Mathias Håkansson.

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

More
4 months 4 weeks ago #20 by Mathias Håkansson
Replied by Mathias Håkansson on topic Interpolated Strings
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.