简体   繁体   English

“ToString”方法没有重载需要 1 个参数

[英]No overload of the 'ToString' method takes 1 arguments

I have this problem:我有这个问题:

blTimer.Text = (TimeSpan.FromMinutes(60) - (DateTime.Now - startTime))
              .ToString("hh\\:mm\\:ss");

At this point:这一点:

ToString("hh\\:mm\\:ss"); 

Visual Studio shows me the error in the title. Visual Studio 向我显示了标题中的错误。 How I can resolve?我该如何解决?

In the future, you should always copy/paste THE FULL, EXACT ERROR MESSAGE into the body of your question!将来,您应该始终将完整、准确的错误消息复制/粘贴到您的问题正文中!

In this case, please consider changing your format string, eg .ToString("hh:mm:ss") .在这种情况下,请考虑更改您的格式字符串,例如.ToString("hh:mm:ss")

The backslash ("") typically indicates an "escape sequence": https://docs.microsoft.com/en-us/cpp/c-language/escape-sequences?view=msvc-160反斜杠(“”)通常表示“转义序列”: https : //docs.microsoft.com/en-us/cpp/c-language/escape-sequences?view=msvc-160

See also How do I write a backslash () in a string?另请参阅如何在字符串中写入反斜杠 ()?


ADDENDUM:附录:

There are a few things that could be going wrong, including: a) the errant backslash in your format string, b) applying the format string to the wrong data type, c) other?有一些事情可能会出错,包括:a) 格式字符串中的错误反斜杠,b) 将格式字符串应用于错误的数据类型,c) 其他?

Since it sounds like you've fixed the backslash, I'm guessing maybe the current problem is that you're invoking .ToString() on the wrong data type (a type that ISN'T a System.DateTime object).由于听起来您已经修复了反斜杠,因此我猜测当前的问题可能是您在错误的数据类型(不是System.DateTime 对象的类型)上调用 .ToString() 。

SUGGESTIONS:建议:

  • When in doubt, one powerful troubleshooting technique is to split your "complex expression" into "individual statements".如有疑问,一种强大的故障排除技术是将您的“复杂表达式”拆分为“单独的语句”。
  • Another powerful technique is to write a "Short, Self Contained, Correct (Compilable), Example" - an SSCCE另一个强大的技术是编写一个“简短的、自包含的、正确的(可编译的)示例”——一个SSCCE

Here is an example:下面是一个例子:

using System;

/*
 * SAMPLE OUTPUT:
 * endTime: 12:00:00
 */
namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            DateTime startTime = DateTime.Parse("10/18/21 11:00:00");
            DateTime endTime = startTime + TimeSpan.FromMinutes(60);
            string s = endTime.ToString("hh:mm:ss");
            Console.WriteLine("endTime: {0}", s);
        }
    }
}

I hope this helps point you in the right direction...我希望这有助于为您指明正确的方向......

if startTime is of type DateTime there is nothing wrong.如果startTimeDateTime类型,则没有任何问题。 TimeSpan has an overload ToString(string format) and your format works. TimeSpan有一个重载ToString(string format)并且您的格式有效。 Tested in PowerShell.在 PowerShell 中测试。

This is the code:这是代码:

timer.Tick += (obj, args) =>
        lblTimer.Text =
        (TimeSpan.FromMinutes(60) - (DateTime.Now - startTime)).ToString("hh/mm/ss"); //here is the problem on .ToString

        timer.Enabled = true;
        tmr_hide.Start();
        tmr_show.Start();
        tmr_if.Start();
        tmr_himmi.Start();
        tmr_clock.Start();

RESOLVED :D已解决:D

The problem was the framework version when i created the project i didn't saw the version of .net so that was the problem!问题是我创建项目时的框架版本我没有看到 .net 的版本所以这就是问题所在! Thanks everybody for helping me!谢谢大家帮助我!

@paulsm4 - Yep. @paulsm4 - 是的。 Thank you, that code is useful for me on some other projects thanks!谢谢,该代码对我在其他一些项目中很有用,谢谢!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM