Reputation: 1053
I have a class which consists only of string constants. Call it Class A. I declare following variable in here.
public static string SetScore = $"Score[@Set='{currentSet}']";
There is another class, lets call it Class B. I will use my SetScore
variable in class B. now this class B knows what is currentSet
.
Problem is Class A does not know what is 'currentSet'.
Is there any solution to this, other than to declare SetScore
in class B OR using String.Format?
Upvotes: 0
Views: 1021
Reputation: 81493
You can't interpolate like that. The Jiter just wont have any clue of the context or as to when to use that variable.
If you think it through, when should it replace it. On first use? What if you wanted to replace multiple Representations in different contexts, which scope should it consider. Sounds very unpredictable
However, if its an consolation. You could do this
public static string SetScore = "Score[@Set='{0}']";
...
result = string.Format(SetScore,currentSet)
Interpolated Strings (C# Reference)
Used to construct strings. An interpolated string looks like a template string that contains interpolated expressions. An interpolated string returns a string that replaces the interpolated expressions that it contains with their string representations.
Moreso
You can use an interpolated string anywhere you can use a string literal. The interpolated string is evaluated each time the code with the interpolated string executes. This allows you to separate the definition and evaluation of an interpolated string.
Upvotes: 4