【问题标题】:vb.net error - Value of type 'Char' cannot be converted to 'Date'vb.net 错误 - 'Char' 类型的值无法转换为 'Date'
【发布时间】:2015-08-17 23:31:47
【问题描述】:

这方面还是个菜鸟,所以请耐心等待。 我的问题的简短版本是我有一个月历,它记录了一系列日期并将它们添加到标签

Dim iStart as DateTime = MonthCalendar1.SelectionRange.Start.ToShortDatestring
Dim iEnd as DateTime = MonthCalendar1.SelectionRange.End.ToShortDatestring
Dim iCount as DateTime = iStart

While iCount <= iEnd
    iCount = iCount.AddDays(1)
    Lable1.Text = Label1.Text & iCount & vbNewLine
End With

现在完美运行。但这是我想添加到我的数据库中的个别记录的一部分。因此正在考虑执行 For Each 循环,但出现上述错误 - 'Char' 类型的值无法转换为 'Date' 这是代码的开头

For Each iCount in label1.text    'This is where the error comes up

请帮忙

【问题讨论】:

  • For Each iCount in label1.text 正在迭代字符串中的 characters。如果您添加了日期范围,则它们不再是日期,而是一串字符。你到底想做什么?
  • 我想将标签中选择并列出的每个日期添加为数据库中的新记录
  • 如果您需要从日历中提取它们,请将它们保存到 List(of Date),以便它们保持日期(希望 db 列也是日期)。如果需要,可以使用标签向用户显示,但 UI 控件会产生可怕的程序变量。
  • 选中时它显示在标签中的唯一原因是让用户仔细检查他们想要使用的日期。你能给我一个将它们保存为 List(of Date) 的示例代码或指向我可以了解更多信息的地方的链接
  • 有没有办法将字符串日期从标签转换回日期格式?喜欢 Cstr 还是 CDate?

标签: vb.net


【解决方案1】:

由于您已使用通用分隔符“vbNewLine”将它们放入其中,因此请尝试使用 Split 检索它们,但首先使用像 Plutonix 状态这样的 List(of T) 会更好。

这是一个使用我曾经测试过的控制台应用程序的示例,因此 UI 元素不存在并被替换为等效变量。

使用Split

Sub Main()
    Dim iStart As DateTime = New DateTime(2014, 1, 1)
    Dim iEnd As DateTime = New DateTime(2014, 1, 15)
    Dim iCount As DateTime = iStart
    Dim LabelText As String
    Dim temp()

    While iCount <= iEnd
        iCount = iCount.AddDays(1)
        LabelText = LabelText & iCount & vbNewLine
    End While
    temp = LabelText.Split(vbNewLine) 'This seperates the single string back to individual entrys
    For Each s As String In temp
        Console.WriteLine(DateTime.Parse(s)) 'Add to DataBase here.
    Next

End Sub

使用List(of DateTime) 这更容易,无需重新转换。

Sub Main()
    Dim iStart As DateTime = New DateTime(2014, 1, 1)
    Dim iEnd As DateTime = New DateTime(2014, 1, 15)
    Dim iCount As DateTime = iStart
    Dim LabelText As String
    Dim tempDate As List(Of DateTime) = New List(Of DateTime)

    While iCount <= iEnd
        iCount = iCount.AddDays(1)
        tempDate.Add(iCount)  'Just add it here no conversion necessary
        LabelText = LabelText & iCount & vbNewLine
    End While

    For Each d As Date In tempDate
        Console.WriteLine(d) 'Add to Database here using Console.WriteLine as example
    Next
End Sub

【讨论】:

  • 谢谢你,但你能解释一下如何通过 For Each 循环将这些单独的日期添加到数据库中吗?除非我需要以其他方式添加它
  • 我假设你的意思是第二个例子,你会像第一个例子中的数组一样做。将编辑
【解决方案2】:

您可以将其简化为一行(长)代码:

Dim Days() As DateTime = Enumerable.Range(0, (MonthCalendar1.SelectionRange.End.Date - MonthCalendar1.SelectionRange.Start.Date).Days).Select(Function(i) MonthCalendar1.SelectionRange.Start.AddDays(i)).ToArray()

为了便于阅读,稍微拆分一下:

Dim start As DateTime = MonthCalendar1.SelectionRange.Start.Date
Dim stop As DateTime = MonthCalendar1.SelectionRange.End.Date
Dim Days() As DateTime = Enumerable.Range(0, (stop - start).Days).
                          Select(Function(i) start.AddDays(i)).ToArray()

然后从您已有的Days() 数组构建您的标签字符串,而不是先构建标签,然后再重新构建数组。

Lable1.Text = String.Join(vbCrLf, Days)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-14
    • 2022-08-18
    • 2015-11-28
    • 1970-01-01
    相关资源
    最近更新 更多