与“int.Parse(string)”匹配的最佳重载方法有一些无效的参数

发布于 2024-12-06 17:51:40 字数 378 浏览 5 评论 0原文

    Console.WriteLine("Enter the page that you would like to set the bookmark on: ");
    SetBookmarkPage(int.Parse(Console.ReadLine));

这是 int.Parse(string) 部分,为我提供了该线程主题的错误消息。不太明白我应该做什么,我正在将字符串解析为 int 并使用 SetBookmarkPage 方法发送它,我错过了什么? SetBookmarkPage 看起来像这样,并且包含在同一个类中:

private void SetBookmarkPage(int newBookmarkPage) {}

    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) {}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

相思故 2024-12-13 17:51:40

不存在需要委托的 int.Parse 重载。听起来您想要这样做

 int.Parse(Console.ReadLine())

,但是,即使如此,您也会使您的程序面临潜在的异常。你应该做这样的事情:

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

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

 SetBookmarkPage(bookmarkId);

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);
來不及說愛妳 2024-12-13 17:51:40

将其更改为

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

Console.ReadLine 之后您缺少 ()

Change it to

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

You were missing () after Console.ReadLine

只怪假的太真实 2024-12-13 17:51:40

您需要调用 Console.ReadLine:

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

注意上面额外的 ()

您当前的方法正在传递从 Console.ReadLine 方法构建的委托,而不是正在调用的方法的结果。

话虽这么说,如果您正在读取用户的输入,我强烈建议使用 int.TryParse 而不是 int.Parse。用户输入经常有错误,这可以让您优雅地处理它。

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.

谜泪 2024-12-13 17:51:40

您想要:

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

目前它正在将 Console.ReadLine 视为方法组,并尝试应用方法组转换 - 如果您随后将其用作用于采用 Func 或类似内容的方法的参数,但不适用于仅采用字符串的方法。

您想要调用该方法,然后将结果作为参数传递。要调用该方法,您需要括号。

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.

要走就滚别墨迹 2024-12-13 17:51:40

您可能的意思是:

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

注意 ReadLine 之后的括号。您正在尝试传递 ReadLine 委托而不是返回值。

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.

你与清晨阳光 2024-12-13 17:51:40

Console.ReadLine 是一个方法,必须用括号调用它:

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

没有括号,编译器认为它是一个方法组。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文