0

I have the following string expression which am trying to run ,But am getting error saying string format is not correct

for (i = diffvol; i < result.count; i++)
{
    B1Volume = Convert.ToDecimal(String.Format("result.Data[0].B{0}Volume", i));
    B2Volume = Convert.ToDecimal(String.Format("result.Data[0].B{0}Volume", i));
}
5
  • result.Data[0].B{0}Volume => even this is not a valid decimal format... you need to convert expression directly into decimal. Commented Oct 30, 2017 at 9:11
  • 1
    Supposing i==10 what should be the conversion to decimal of this string result.Data[0].B10Volume ? Commented Oct 30, 2017 at 9:12
  • What you are getting in result.Data[0]? Commented Oct 30, 2017 at 9:13
  • @Steve yes it will be Data[0].B10Volume Commented Oct 30, 2017 at 9:15
  • Convert.ToDecimal called with a string expects to have a string that is the text rapresentation of a number, e.g. 10.25. The string you are passing is code that you expect will be evaluated: it will not. Commented Oct 30, 2017 at 11:47

1 Answer 1

1

The error you are getting is related to the Convert.ToDecimal method, NOT the String.Format. The reason should be obvious: a string such as "result.Data[0].B0" is not the string representation of a number. A string such as "1407" would be one.

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.