0

This is a sense the continue of this question. But for be accurate I explain what I want to do. I've developed a web application that allow the user to manage the working days (I call it working plan), now I'm current developing a client application for synch the local data on the web database. So I must have the full compatibility with all data information from the client to the web. The bottom code simply create a workingPlan for a specific user, this workingPlan is in JsonFormat, the result's that I want achieve is like this:

"monday":{
"start":"09:00",
"end":"18:00",
"breaks":[
{
"start":"11:20",
"end":"11:30"
},
{
"start":"14:30",
"end":"15:00"
}
]
},
"tuesday":{
"start":"09:00",
"end":"18:00",
"breaks":[
{
"start":"11:20",
"end":"11:30"
},
{
"start":"14:30",
"end":"15:00"
}
]
},

How you can see each day have the start and the end working time. Also, in each day, I can have multiple breaks, this is for split the working slot. Now the json encoded on the top is a string generated from PHP, while the json generated in vb.net is the result of DateTimePicker filled. Essentially what I need to do is valorize the breaks, now I add the breaks in a DataGrid, for example:

enter image description here

The final json format will be (for this specific case):

{"monday":{"start":"09:00","end":"18:00","breaks":[{"start":"19:30","end":"19:50"},{"start":"21:40","end":"21:30"}]},"tuesday":{"start":null,"end":null,"breaks":[{"start":null,"end":null},{"start":"14:30","end":"15:00"}]},"wednesday":{"start":"09:00","end":"18:00","breaks":[{"start":"19:37","end":"19:50"}...

The class for manage the Week, Breaks Day are so declared:

 Public Class WorkDay          

    <JsonProperty("start")>
    Public Property starttime As String
    <JsonProperty("end")>
    Public Property endtime As String
    Public Property breaks As Break()

End Class

Public Class Break              

    <JsonProperty("start")>
    Public Property starttime As String
    <JsonProperty("end")>
    Public Property endtime As String

End Class

Public Class WorkWeek          

    Public Property monday As WorkDay
    Public Property tuesday As WorkDay
    Public Property wednesday As WorkDay
    Public Property thursday As WorkDay
    Public Property friday As WorkDay
    Public Property saturday As WorkDay
    Public Property sunday As WorkDay

    Public Sub New()
        monday = New WorkDay
        tuesday = New WorkDay
        wednesday = New WorkDay
        thursday = New WorkDay
        friday = New WorkDay
        saturday = New WorkDay
        sunday = New WorkDay
    End Sub

End Class

Now, for valorize the breaks of each day I iterate on this DataGrid

For Each row As DataGridViewRow In User.break_view.Rows

                If Not row.IsNewRow Then

                    If row.Cells(0).Value.ToString = "Monday" Then

                        workWeek.monday.breaks = row.Cells(1).Value.ToString 'Incriminated line

                    End If

                 ... each day condition

                End If

            Next

on this workWeek.monday.breaks = row.Cells(1).Value.ToString line I get this error:

Can't convert String into Users.Break()

also I want to know if there's a way to optimize it, 'cause actually I must perform 7 condition and check if the current day in iteration is Monday or Tuesday etc... and so add the break to this specific day. If I was unclear on something that I explained, please tell me and I'll try to provider more and specific details. Thanks for the attention.

Community
  • 1
  • 1
Dillinger
  • 1,823
  • 4
  • 33
  • 78
  • This is because the property breaks is defined of type "Break". We cannot assign directly. you can try some thing like workWeek.monday.breaks=new Break() followed by workWeek.monday.breaks.starttime=row.Cells(1).value.ToString – Baskar Rao Dec 13 '15 at 18:58
  • 1
    I get starttime is not a member of Break – Dillinger Dec 13 '15 at 19:15

1 Answers1

1

First, you should change the Breaks declaration to:

Public Property breaks As New List(Of Break)

As shown in the answer. Then, since it is List(of Break) you will need to add New Break objects to it:

For Each row As DataGridViewRow In User.break_view.Rows
    ' ...or Exit For since it is always the last one:
    If row.IsNewRow Then Continue For

    Dim brk As new Break       ' create a New Break object
    Brk.starttime = row.Cells(1).Value.ToString
    Brk.endtime = row.Cells(2).Value.ToString     '??

    ' figure where to add it
    Select Case row.Cells(0).Value.ToString.ToLowerInvariant
        Case "sunday"
            ' WorkWeek is a Type, avoid using type names as variables:
            myWorkWeek.sunday.Breaks.Add(brk)
        Case "monday"
            myWorkWeek.monday.Breaks.Add(brk)
        ...
     End Select
Next

Since the same json has multiple breaks, there would have to be more to the loop somewhere. But those would just be added as well (just not sure from where). If you were using the dictionary form, you could get rid of the case statement using row.Cells(0) as the key.

If a break were to change (how are you going to find these1?), you can do like in your code:

myWrkWeek.friday.Breaks(0).starttime = "08:15"
myWrkWeek.friday.Breaks(0).endtime = "17:55"

You can also expedite by adding starttime and endtime as parameters to the Break constructor. Since there is no reason for one of these to exist without both a start and stop time, that makes sense. But since the source is DGV cells, it wont look any clearer in code.

1 If this is actually something akin to a timecard app, you probably want something like multiple In/Out times for each day, then that class could have a property which returns a TimeSpan giving the total accumulated time.

Ňɏssa Pøngjǣrdenlarp
  • 38,411
  • 12
  • 59
  • 178
  • 1
    Now I'm on mobile so I can't try the code but it seems good as always. Thanks, the case is a good way out, I was just coming to mind. – Dillinger Dec 13 '15 at 20:56