-3

I am getting this error with the following line of code,

int numberStored = 9;
record.VALUE = string.Format("{\"FIELDS\":[{\"ELEMENT_ID\":\"275887826\",\"VALUE\":\"{0}\"}]}", numberStored.ToString(), 0);

This works fine if i substitute out {0} and place the 9 in directly, but obviously I don't want it hard coded like this. The previous answers I can see for this problem do not seem to be helping me.

2
  • As an aside, you don't need numberStored.ToString(), you can use numberStored in String.Format. ToString is called implicitly anyway. Commented Mar 11, 2015 at 15:21
  • Please instead of just typing "The previous answers I can see for this problem do not seem to be helping me." into the question as "demonstration" of research add actual links to questions you've found. I.e. "I've searched for bing.com/search?q=c%23+string+format+curly which gave me stackoverflow.com/questions/3773857/…, but I don't get what 'Use double braces' mean" or something similar. Commented Mar 11, 2015 at 15:28

1 Answer 1

8

You have to escape curly braces by doubling them:

int numberStored = 9;
record.VALUE = string.Format("{{\"FIELDS\":[{{\"ELEMENT_ID\":\"275887826\",\"VALUE\":\"{0}\"}}]}}", numberStored.ToString(), 0);
Sign up to request clarification or add additional context in comments.

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.