Page 2 of 3
Interpolated Strings
Posted: Fri Aug 17, 2018 6:37 am
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
Interpolated Strings
Posted: Fri Aug 17, 2018 6:53 am
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
Interpolated Strings
Posted: Fri Aug 17, 2018 7:13 am
by FFF
Don't get me wrong, but what exactly has this to do with interpolated strings?
K.
Interpolated Strings
Posted: Fri Aug 17, 2018 7:17 am
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.
Interpolated Strings
Posted: Fri Aug 17, 2018 10:38 pm
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
Interpolated Strings
Posted: Sun Aug 19, 2018 6:11 pm
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
Interpolated Strings
Posted: Sun Aug 19, 2018 7:48 pm
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
Interpolated Strings
Posted: Mon Aug 20, 2018 4:02 am
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.
Interpolated Strings
Posted: Mon Aug 20, 2018 7:30 am
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
Interpolated Strings
Posted: Thu Aug 23, 2018 6:59 am
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