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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
codeMetis
  • 420
  • 4
  • 16
  • 3
    It is highly recommended to state the programming language you're using (and tag it too) – Nir Alfasi Apr 23 '13 at 00:00
  • 1
    perhaps you are trying for string.Format(thingyouwanttoformat)? – iGanja Apr 23 '13 at 00:19
  • 2
    Possible Duplicate: [Convert int to string in C#](http://stackoverflow.com/q/3081916/299327) – Ryan Gates Apr 23 '13 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()` – Kate Gregory Apr 23 '13 at 13:27

1 Answers1

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);
}
Rob Fonseca-Ensor
  • 15,510
  • 44
  • 57