开发者

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

    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 parsin开发者_运维问答g 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) {}


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);


Change it to

SetBookmarkPage(int.Parse(Console.ReadLine()));

You were missing () after Console.ReadLine


You need to call Console.ReadLine:

SetBookmarkPage(int.Parse(Console.ReadLine()));    

Note the extra () in the above.

Your current method is passing a delegate built from the Console.ReadLine method, not the result of the method being called.

That being said, if you're reading input from a user, I would strongly recommend using int.TryParse instead of int.Parse. User input frequently has errors, and this would let you handle it gracefully.


You want:

SetBookmarkPage(int.Parse(Console.ReadLine()));

At the moment it's viewing Console.ReadLine as a method group, and trying to apply a method group conversion - which will work if you're then using it as an argument for a method taking a Func<string> or something similar, but not for a method taking just a string.

You want to invoke the method, and then pass the result as an argument. To invoke the method, you need the parentheses.


You probably meant:

SetBookmarkPage(int.Parse(Console.ReadLine()));

Notice the parens after ReadLine. You are trying to pass a delegate for ReadLine instead of the return value.


Console.ReadLine is a method, you must invoke it with parenthesis:

SetBookmarkPage(int.Parse(Console.ReadLine()));

Without the parenthesis, the compiler thinks it is a method group.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜