Interpolated Strings

Public forum to share code snippets, screen shorts, experiences, etc.
MathiasHakansson
Posts: 50
Joined: Fri Feb 16, 2018 7:52 am

Interpolated Strings

Post by MathiasHakansson »

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
MathiasHakansson
Posts: 50
Joined: Fri Feb 16, 2018 7:52 am

Interpolated Strings

Post by MathiasHakansson »

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
FFF
Posts: 1580
Joined: Fri Sep 25, 2015 4:52 pm
Location: Germany

Interpolated Strings

Post by FFF »

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

K.
Regards
Karl
(on Win8.1/64, Xide32 2.20, X#2.20.0.3)
MathiasHakansson
Posts: 50
Joined: Fri Feb 16, 2018 7:52 am

Interpolated Strings

Post by MathiasHakansson »

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.
User avatar
Chris
Posts: 4906
Joined: Thu Oct 08, 2015 7:48 am
Location: Greece

Interpolated Strings

Post by Chris »

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
Chris Pyrgas

XSharp Development Team
chris(at)xsharp.eu
User avatar
wriedmann
Posts: 3755
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

Interpolated Strings

Post by wriedmann »

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:

https://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
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
FFF
Posts: 1580
Joined: Fri Sep 25, 2015 4:52 pm
Location: Germany

Interpolated Strings

Post by FFF »

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
(on Win8.1/64, Xide32 2.20, X#2.20.0.3)
User avatar
wriedmann
Posts: 3755
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

Interpolated Strings

Post by wriedmann »

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
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
MathiasHakansson
Posts: 50
Joined: Fri Feb 16, 2018 7:52 am

Interpolated Strings

Post by MathiasHakansson »

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
MathiasHakansson
Posts: 50
Joined: Fri Feb 16, 2018 7:52 am

Interpolated Strings

Post by MathiasHakansson »

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
Post Reply