0

How I can have a string value like "PropertyBag["ABCD"]" ? I want it to assign to a value of a chart in my WPF App. I am using it like as shown below :

 private void Window_Loaded(object sender, RoutedEventArgs e) { ViewModel vm = new ViewModel(); foreach (var str in vm.Data) { string name = "ABCD"; LineSeries lineSeries = new LineSeries(); lineSeries.ItemsSource = new ViewModel().Data; lineSeries.XBindingPath = "Date"; lineSeries.YBindingPath = "PropertyBag[\" + name + \"]"; // here i am getting error saying - Input string was not in a correct format lineSeries.IsSeriesVisible = true; lineSeries.ShowTooltip = true; chart.Series.Add(lineSeries); } this.DataContext = vm; } 

I want it like lineSeries.YBindingPath = "PropertyBag["ABCD"]" // (should include all 4 double quotes). How it is possible ??

I tried this also, but still same error :

 lineSeries.YBindingPath = String.Format(@"PropertBag[""{0}""]", name); 
5
  • 1
    Google is your friend: learn.microsoft.com/en-us/dotnet/desktop/winforms/controls/… Commented Sep 29, 2022 at 12:01
  • You've got escaped quotes inside the square braces, did you also want to put them at the start/end of the string? Those are missing if so. Commented Sep 29, 2022 at 12:02
  • What is LinePath, and what does it expect YBindingPath to look like? This isn't really a C# issue - it's a matter of "whatever the library you're using can accept". Commented Sep 29, 2022 at 12:03
  • @gunr2171 Yes I want all the 4 quotes. Can u pls give me a sample ? Commented Sep 29, 2022 at 12:03
  • lineSeries.YBindingPath = $"PropertyBag[\"{name}\"]";, However, this almost certainly the wrong way to bind the WPF control. Commented Sep 29, 2022 at 12:08

3 Answers 3

2

This must work and it is a more elegant solution:

string name = "ABC"; lineSeries.YBindingPath = string.Format("\"PropertyBag[\"{0}\"]\"", name); 

You escaped the quote before and after name, in doing that you didn't specify where the first part of the string ends and where the second one starts. To avoid errors like that it is better to use the Format method. It makes code easier to read and maintain.

The following solution:

string.Format(@"""PropertyBag[""{0}""]""", name) 

is good too, but I think the first one is more readable. I personally like more the first one. But it is up to you, just avoid concatenation; in modern programming it is deprecated especially when the language has more efficient and powerful tools to do the job.

**** UPDATE ****

If you use the most recent versions of .NET you can also use an even more elegant solution, through string interpolation (like in Kotlin and Swift):

string name = "ABC"; lineSeries.YBindingPath = $"\"PropertyBag[\"{name}\"]\""; 
Sign up to request clarification or add additional context in comments.

2 Comments

@Sergiob. Your answer is correct.
The triple quotes only support by C# 11, namely Raw string literals
1

You're escaping the quotes before the pluses, meaning they get interpreted as characters, not as addition operators. You need to add additional quotation marks, like so:

lineSeries.YBindingPath = "\"PropertyBag[\"" + name + "\"]\""; 

This way, you define two new strings; a prefix and a suffix to the name variable.

You can also use string interpolation, which results in cleaner code:

lineSeries.YBindingPath = $"\"PropertyBag[\"{name}\"]\""; 

3 Comments

System.FormatException: 'Input string was not in a correct format.' It gives PropertyBag["ABCD"]. But I want it like "PropertyBag["PRSPR"]" - should include all the 4 quotes.
user interpolation, not concatenation
@JassimS oops, sorry, didn't notice that you wanted to surround the string with quotes as well! I've updated the answer to include them.
0

Quoted string literals

("PropertyBag[\"" + name + "\"]"); 

Verbatim string literals

(@"PropertyBag[""{0}""]", name); 

Try Raw string literals if you're on C# 11

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.