Anton Kriel
Anton Kriel

Reputation: 33

vb.net error - Value of type 'Char' cannot be converted to 'Date'

Still a noob at this so please be patient. Short version of my problem is I have a monthcalendar that pics a range of dates and adds them to label

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

Now that works perfectly. But this is part of individual records I want to add to my database. So was thinking of doing a For Each loop but getting the above mentioned error - Value of type 'Char' cannot be converted to 'Date' This is the beginning of the code

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

Please help

Upvotes: 0

Views: 1576

Answers (2)

Joel Coehoorn
Joel Coehoorn

Reputation: 415810

You can get this down to one (long) line of code:

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

Break it up just a bit for readability:

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

And then build your label string from the Days() array you already have, rather than building the label first and reconstructing the array later.

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

Upvotes: 1

Mark Hall
Mark Hall

Reputation: 54532

Since you have put them in using a common deliminator "vbNewLine', try using Split to retrieve them, though you would be better off using a List(of T) like Plutonix states in the first place.

This is an example using a console application that I used to test, so the UI elements are not present and were replaced with the equivalent variables.

Using 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

Using List(of DateTime) This is a lot easier with out having to reconvert.

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

Upvotes: 1

Related Questions