为什么这个 while 循环不会中断?

发布于 2025-01-01 05:21:11 字数 742 浏览 1 评论 0原文

date[inc] = [0] = "01/01/2011"

dateTimePicker1.Value = {01/01/2011 00:00:00}

if 条件为更改辅助日期时间选择器时从未满足。第一个日期时间选择器必须更改一次,此代码才能工作。在这两种情况下,此代码均由 ValueChanged 触发。

有什么想法吗?

编辑:

int start;
int inc = 0;
while (true)
{
   inc++;
   if (Convert.ToString(dateTimePicker1.Value.ToShortDateString()) == date[inc])
   {
      start = inc;
      inc = 0;
      break;
   }  
}

=/=

int start;
int inc = 0;
while (true)
{
   if (Convert.ToString(dateTimePicker1.Value.ToShortDateString()) == date[inc])
   {
      start = inc;
      inc = 0;
      break;
   }  
   inc++;
}

仍然非常有帮助的评论,谢谢大家。显然由于增量而错过了第一个日期,导致无限循环。

date[inc] = [0] = "01/01/2011"

dateTimePicker1.Value = {01/01/2011 00:00:00}

The if condition is never fulfilled when changing a secondary datetimepicker. The first datetimepicker has to be changed once before this code will work. This code is triggered by a ValueChanged in both cases.

Any ideas?

Edit:

int start;
int inc = 0;
while (true)
{
   inc++;
   if (Convert.ToString(dateTimePicker1.Value.ToShortDateString()) == date[inc])
   {
      start = inc;
      inc = 0;
      break;
   }  
}

=/=

int start;
int inc = 0;
while (true)
{
   if (Convert.ToString(dateTimePicker1.Value.ToShortDateString()) == date[inc])
   {
      start = inc;
      inc = 0;
      break;
   }  
   inc++;
}

Still very helpful comments, thanks everyone. Obviously was missing the first date because of an increment, causing an infinite loop.

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

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

发布评论

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

评论(4

夏九 2025-01-08 05:21:11

尝试像这样进行比较

if (dateTimePicker1.Value.ToString("MM/dd/yyyy",
       new System.Globalization.CultureInfo("en-US") == date[inc])

,或者

//Considering your date format is MM/dd/yyyy, coz 01/01/2011 doesn't help in uderstanding
//in date format, if it would have been 01/13/2011, it would have helped
if (dateTimePicker1.Value.Date == 
       DateTime.ParseExact(date[inc], "MM/dd/yyyy", 
           new System.Globalization.CultureInfo("en-US")).Date)

您也可以在 date[inc] 上执行 .Trim() 它可能会对您有所帮助。

编辑

查找

  • 当您比较字符串时,要在开头和结尾 空格。如果需要,请使用 .Trim()
  • 区分大小写删除它们。执行 string.Equals(string1, string2, StringComparison.InvariantCultureIgnoreCase)

希望这对您有用。

Try to compare like this

if (dateTimePicker1.Value.ToString("MM/dd/yyyy",
       new System.Globalization.CultureInfo("en-US") == date[inc])

OR

//Considering your date format is MM/dd/yyyy, coz 01/01/2011 doesn't help in uderstanding
//in date format, if it would have been 01/13/2011, it would have helped
if (dateTimePicker1.Value.Date == 
       DateTime.ParseExact(date[inc], "MM/dd/yyyy", 
           new System.Globalization.CultureInfo("en-US")).Date)

Also you can do .Trim() on date[inc] it might help you.

EDIT

When you compare string things to look for

  • White Spaces at beginning and at end. Remove them if required using .Trim()
  • Case Sensitivity. Do string.Equals(string1, string2, StringComparison.InvariantCultureIgnoreCase)

Hope this works for you.

ぽ尐不点ル 2025-01-08 05:21:11

尝试:

if (dateTimePicker1.Value.ToString("MM/dd/yyyy") == date[inc])
// ...

Try:

if (dateTimePicker1.Value.ToString("MM/dd/yyyy") == date[inc])
// ...
孤独患者 2025-01-08 05:21:11

您正在进行字符串比较:

"01/01/2011" != "1/1/2011"

You're doing a string comparison:

"01/01/2011" != "1/1/2011"
A君 2025-01-08 05:21:11

如果字符串永远不相等,则没有条件突破

if the strings are never equal, theres no condition to break out on ever

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