0

In my class, my teacher showed me something similar to this. Visual Studio is saying string doesn't have a definition for parse. I remember in class the teacher said it was something.parse(thingyouwanttoparse). No commas. I've searched online, but all the options are different to the one the teacher showed me. What am I doing wrong?

if (!ValidMenuOption) { string errorMsg = "\n\t Option must be "; int iteration = 1; while (iteration <=numAvailable) { errorMsg = errorMsg + string.parse(iteration) + ", "; iteration += 1 } errorMsg = errorMsg + "or 0"; Console.WriteLine(errorMsg); } //end if 
4
  • 3
    It is highly recommended to state the programming language you're using (and tag it too) Commented Apr 23, 2013 at 0:00
  • 1
    perhaps you are trying for string.Format(thingyouwanttoformat)? Commented Apr 23, 2013 at 0:19
  • 2
    Possible Duplicate: Convert int to string in C# Commented Apr 23, 2013 at 13:05
  • 1
    Parse is generally used for making a string into a number, date, collection of smaller strings etc. Format is generally used for making something into a string. This applies both to words in sentences and to the functions you call. In your case all you seem to need is iteration.ToString() Commented Apr 23, 2013 at 13:27

1 Answer 1

3

Parsing is when you turn a string into a thing. Formatting is the opposite of parsing, and in C# you can format an int by calling .ToString() on it. If you're concatenating strings, then you can even leave this method call off, so your code probably becomes

if (!ValidMenuOption){ string errorMsg = "\n\t Option must be "; int iteration = 1; while (iteration <=numAvailable) { errorMsg = errorMsg + iteration + ", "; iteration+=1; } errorMsg = errorMsg + "or 0"; Console.WriteLine(errorMsg); } 

If you want to get fancy, you could have done it this way too:

if (!ValidMenuOption){ string errorMsg = "\n\t Option must be "+string.Join(", ", Enumerable.Range(1, numAvailable)) + " or 0"; Console.WriteLine(errorMsg); } 
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.