The best overloaded method match for 'int.Parse(string)' has some invalid arguments

Petrus K. picture Petrus K. · Sep 23, 2011 · Viewed 7.9k times · Source
    Console.WriteLine("Enter the page that you would like to set the bookmark on: ");
    SetBookmarkPage(int.Parse(Console.ReadLine));

It's the int.Parse(string) part that gives me the error message of the topic of this thread. Don't really understand what I should do, I'm parsing a string into an int and sending it with the SetBookmarkPage method, what am I missing? SetBookmarkPage looks like this and is contained in the same class:

private void SetBookmarkPage(int newBookmarkPage) {}

Answer

Tejs picture Tejs · Sep 23, 2011

There is no overload of int.Parse that takes a delegate. It sounds like you wanted to do

 int.Parse(Console.ReadLine())

However, even then you're exposing your program to a potential exception. You should do something like this:

 int bookmarkId = 0;
 string info = Console.ReadLine();

 if(!int.TryParse(info, out bookmarkId))
    Console.WriteLine("hey buddy, enter a number next time!");

 SetBookmarkPage(bookmarkId);