Interpolated Strings

Public forum to share code snippets, screen shorts, experiences, etc.
User avatar
wriedmann
Posts: 3754
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

Interpolated Strings

Post by wriedmann »

Hi,

this is really great:

Code: Select all

local nValue as decimal
nValue := 123.45m	
Console.WriteLine( i"nValue: {nValue:######.0000}" ) // nValue: 123,4500
Console.WriteLine( i"nValue {nValue:F6}" )  // nValue: 123,450000
even if the .NET formatting strings are less powerful than the pictures we know from VO.

Wolfgang

P.S. I have also adjusted the wiki: https://docs.xsharp.it/doku.php?id=strings
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 »

Yes, Wolfgang, it's really convenient.

I find the .net formatting strings is less useful for numbers than VO's when it comes to common numbers and amounts. The formatting in .net however covers a wider field (like hex-values). I think the date formatting in .net is easier with all the standard formats that are ready to use.

https://docs.microsoft.com/en-us/dotnet ... at-strings

/Mathias
User avatar
wriedmann
Posts: 3754
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

Interpolated Strings

Post by wriedmann »

Hi Mathias,

yes, I find the options for numeric less useful than the picture strings in VO. And after all, input of numerics is very important in any business application.

But I see not why the picture clauses in the X# runtime could not be extended by the development team when we need that.

Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
User avatar
SHirsch
Posts: 286
Joined: Tue Jan 30, 2018 8:23 am
Location: Germany

Interpolated Strings

Post by SHirsch »

Hi,
another question for this topic:
I would like to use an interpolated string on a dictionary.
Short example

Code: Select all

VAR dict := Dictionary<STRING, OBJECT>{}
dict:Add("PL_ID", "0001")
VAR val := ei"pl_id: {dict["pl_id"]}"
This throws three errors:
error XS8076: Missing close delimiter '}' for interpolated expression started with '{'.
error XS1056: Unexpected character ''
error XS1003: Syntax error, ']' expected

Is this a bug or wrong syntax?

Regards
Stefan
User avatar
Chris
Posts: 4898
Joined: Thu Oct 08, 2015 7:48 am
Location: Greece

Interpolated Strings

Post by Chris »

Hi Stefan,

Not sure if it should be considered a bug, but I think this is stretching interpolated strings a it too far! The point of using them is to make the code more readable, and this does not seem really readable to me :)

I would instead use String.Format() for such complex embedding of values in strings:

VAR val := System.String.Format("pl_id: {0}", dict["pl_id"])

I realize this is not answering your question, but I thought it might be helpful to give this different suggestion.
Chris Pyrgas

XSharp Development Team
chris(at)xsharp.eu
User avatar
SHirsch
Posts: 286
Joined: Tue Jan 30, 2018 8:23 am
Location: Germany

Interpolated Strings

Post by SHirsch »

Hi Chris,

in C# it compiles without backslash.

Code: Select all

var dict = new Dictionary<String, Object>();
dict.Add("PL_ID", "0001");
var val = $"pl_id: {dict["PL_ID"]}";
IntelliSense is a bit better than XIDE in this part. I think recognizing the brackets and change the color for the interpolated expression back to normal code color would help readability.
VS_IntelliSense_InterpolatedStrings.jpg
VS_IntelliSense_InterpolatedStrings.jpg (12.41 KiB) Viewed 1856 times
Stefan
Post Reply