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

TOPIC: Interpolated Strings

Interpolated Strings 10 months 4 days ago #5822

  FFF's Avatar Topic Author FFF Away Posts: 574
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.

Interpolated Strings 10 months 4 days ago #5825

  wriedmann's Avatar wriedmann Away Posts: 1567
Hi Karl,
Currently I am on holiday, will add better samples after my return.
Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
This email address is being protected from spambots. You need JavaScript enabled to view it.
www.riedmann.it - docs.xsharp.it

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

Interpolated Strings 10 months 4 days ago #5826

  FFF's Avatar Topic Author FFF Away Posts: 574
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.

Interpolated Strings 10 months 3 days ago #5827

  Karl-Heinz's Avatar Karl-Heinz Online Posts: 264
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 10 months 3 days ago #5828

  Chris's Avatar Chris Offline Posts: 1164
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 10 months 3 days ago #5830

  FFF's Avatar Topic Author FFF Away Posts: 574
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.

Interpolated Strings 10 months 3 days ago #5831

  Karl-Heinz's Avatar Karl-Heinz Online Posts: 264
? 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 10 months 3 days ago #5834

  FFF's Avatar Topic Author FFF Away Posts: 574
That all works (but only) for "?"...

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

Interpolated Strings 10 months 3 days ago #5835

  robert's Avatar robert Offline Posts: 994
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.

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

Last edit: by robert.

Interpolated Strings 10 months 3 days ago #5836

  MathiasHakansson's Avatar MathiasHakansson Offline Posts: 49
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 10 months 3 days ago #5837

  MathiasHakansson's Avatar MathiasHakansson Offline Posts: 49
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 10 months 3 days ago #5838

  MathiasHakansson's Avatar MathiasHakansson Offline Posts: 49
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 10 months 3 days ago #5839

  FFF's Avatar Topic Author FFF Away Posts: 574
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 10 months 3 days ago #5840

  MathiasHakansson's Avatar MathiasHakansson Offline Posts: 49
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 10 months 2 days ago #5852

  Chris's Avatar Chris Offline Posts: 1164
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 10 months 21 hours ago #5866

  wriedmann's Avatar wriedmann Away Posts: 1567
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
This email address is being protected from spambots. You need JavaScript enabled to view it.
www.riedmann.it - docs.xsharp.it

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

Interpolated Strings 10 months 20 hours ago #5868

  FFF's Avatar Topic Author FFF Away Posts: 574
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.

Interpolated Strings 10 months 11 hours ago #5869

  wriedmann's Avatar wriedmann Away Posts: 1567
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
This email address is being protected from spambots. You need JavaScript enabled to view it.
www.riedmann.it - docs.xsharp.it

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

Interpolated Strings 10 months 8 hours ago #5879

  MathiasHakansson's Avatar MathiasHakansson Offline Posts: 49
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 9 months 4 weeks ago #5906

  MathiasHakansson's Avatar MathiasHakansson Offline Posts: 49
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