Logic Problem?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Marco

    #1

    Logic Problem?

    Below I've included the code snippet that is giving me problems. In this
    code I am going through the arraylist operatorLineLis t and matching it up
    with records in the arraylist operatorList. My problem is the line "If
    lineDayRec.Day = lineRec.Day Then". Eventough there will be multiple
    records in operatorList the for statement will always exit at the first
    record once it hits that line. I've tried to enclose the entire code in a
    try/catch but it didn't report any errors. This isn't part of a thread so
    there is nothing else changing the data. Does anyone see what I'm doing
    wrong?

    'Add up all of the lines for each day and get the total number of people
    required for that day
    For Each lineDayRec As OperatorLineReq uirements In operatorLineLis t
    Dim foundRec As Boolean = False

    'Try to find the same day in the existing records and add the
    new operator requirements
    For Each lineRec As OperatorRequire ments In operatorList
    If lineDayRec.Day = lineRec.Day Then
    lineRec.People += lineDayRec.Peop le
    foundRec = True
    Exit For
    End If
    Next

    'If the same day was not found then add it in
    If foundRec = False Then
    Dim newRec As New OperatorRequire ments

    newRec.Day = lineDayRec.Day
    newRec.People = lineDayRec.Peop le

    operatorList.Ad d(newRec)
    End If
    Next

    'Holds the operator line requirements for one day
    Private Class OperatorLineReq uirements
    Public Line As String
    Public Day As Date
    Public People As Integer
    End Class

    'Holds the operator requirements for one day
    Private Class OperatorRequire ments
    Public Day As Date
    Public People As Integer
    end Class


  • 

    #2
    Re: Logic Problem?


    "Marco" <nospampleasesy xxpk@hotmail.co m> wrote in message
    news:uDFzy3kqFH A.2540@TK2MSFTN GP09.phx.gbl...
    | Does anyone see what I'm doing
    | wrong?
    |
    | 'Add up all of the lines for each day and get the total number of people
    | required for that day
    | For Each lineDayRec As OperatorLineReq uirements In operatorLineLis t
    | Dim foundRec As Boolean = False
    |
    | 'Try to find the same day in the existing records and add the
    | new operator requirements
    | For Each lineRec As OperatorRequire ments In operatorList
    | If lineDayRec.Day = lineRec.Day Then
    | lineRec.People += lineDayRec.Peop le

    you described what the code is doing but not the specific problem you are
    having. based on the last section of code you posted, i'd say the line above
    would present an interesting delima:

    lineRec.People += lineDayRec.Peop le

    i assume that the problem is that even though foundRec is true, your
    lineDayRec.Peop le count is off...probably always 0 ... am i right?

    try replacing that line with:

    lineDayRec.Peop le += 1

    the cause is that lineRec is a new instance of operatorrequire ments at each
    iteration of the for each loop...meaning that setting the lineRec.People to
    any value is mute outside of the scope of that loop. in the last section of
    your code you are using the lineDayRec.Peop le interface...but you are never
    setting it. try the replacement above and see if that helps...assumin g i've
    guessed the problem based on your code and its description.

    hth,

    me




    Comment

    • 

      #3
      Re: Logic Problem?

      | lineRec.People += lineDayRec.Peop le
      |
      | i assume that the problem is that even though foundRec is true, your
      | lineDayRec.Peop le count is off...probably always 0 ... am i right?
      |
      | try replacing that line with:
      |
      | lineDayRec.Peop le += 1

      actually, that wouldn't be appropriate either since People could be any
      valid number of people. just swap:

      lineRec.People += lineDayRec.Peop le

      with:

      lineDayRec.Peop le += lineRec.People

      see if that works...plus, the following changes may make the code more
      concise:

      For Each lineDayRec As OperatorLineReq uirements In operatorLineLis t
      For Each lineRec As OperatorRequire ments In operatorList
      If lineRec.Day = lineDayRec.Day Then
      operatorList.Ad d(New OperatorRequire ments(lineRec.D ay,
      lineRec.People) )
      Exit For
      End If
      Next
      Next

      Private Class OperatorRequire ments
      Public Day As Date
      Public People As Integer
      Public Sub New(ByVal DateStamp As Date, ByVal Count As Integer)
      Day = DateStamp
      People = Count
      End Sub
      End Class


      Comment

      • Marco

        #4
        Re: Logic Problem?

        No, that's not it. The problem is that the code never actually gets to the
        line "lineRec.Pe ople += lineDayRec.Peop le". It will get to the line before
        that then exit out of the loop eventough it's only at the first record of
        the operatorList arraylist. It's like it thinks that the rest of the
        records in the list are not of OperatorRequire ments type. But if you look
        just a few lines below you can see where I insert new records into the
        arraylist and they are of OperatorRequire ments type. Also when I was
        debugging the code I could see how many records were in the arraylist and it
        was far more then just one.

        I've already gotten around this problem by not specifying the variable type
        in the for statement and instead assigning it to a variable inside the for
        loop. So it looks like this now.

        For lineCounter As Integer = 0 To operatorList.Co unt - 1
        Dim lineRec As OperatorRequire ments = operatorList(li neCounter)

        If lineDayRec.Day = lineRec.Day Then
        lineRec.People += lineDayRec.Peop le
        foundRec = True
        Exit For
        End If
        Next

        The only thing I'm doing different now is retrieving the value inside of the
        arraylist in the second line instead of in the same line that I start the
        for statement. It doesn't make any sense to me as to why the initial code I
        wrote will not work correctly.
        Thanks for trying to help.


        "" <a@b.com> wrote in message news:l_HPe.9288 $WO2.2219@fe06. lga...[color=blue]
        >
        > "Marco" <nospampleasesy xxpk@hotmail.co m> wrote in message
        > news:uDFzy3kqFH A.2540@TK2MSFTN GP09.phx.gbl...
        > | Does anyone see what I'm doing
        > | wrong?
        > |
        > | 'Add up all of the lines for each day and get the total number of people
        > | required for that day
        > | For Each lineDayRec As OperatorLineReq uirements In
        > operatorLineLis t
        > | Dim foundRec As Boolean = False
        > |
        > | 'Try to find the same day in the existing records and add the
        > | new operator requirements
        > | For Each lineRec As OperatorRequire ments In operatorList
        > | If lineDayRec.Day = lineRec.Day Then
        > | lineRec.People += lineDayRec.Peop le
        >
        > you described what the code is doing but not the specific problem you are
        > having. based on the last section of code you posted, i'd say the line
        > above
        > would present an interesting delima:
        >
        > lineRec.People += lineDayRec.Peop le
        >
        > i assume that the problem is that even though foundRec is true, your
        > lineDayRec.Peop le count is off...probably always 0 ... am i right?
        >
        > try replacing that line with:
        >
        > lineDayRec.Peop le += 1
        >
        > the cause is that lineRec is a new instance of operatorrequire ments at
        > each
        > iteration of the for each loop...meaning that setting the lineRec.People
        > to
        > any value is mute outside of the scope of that loop. in the last section
        > of
        > your code you are using the lineDayRec.Peop le interface...but you are
        > never
        > setting it. try the replacement above and see if that helps...assumin g
        > i've
        > guessed the problem based on your code and its description.
        >
        > hth,
        >
        > me
        >
        >
        >
        >[/color]


        Comment

        • 

          #5
          Re: Logic Problem?

          | The only thing I'm doing different now is retrieving the value inside of
          the
          | arraylist in the second line instead of in the same line that I start the
          | for statement. It doesn't make any sense to me as to why the initial code
          I
          | wrote will not work correctly.
          | Thanks for trying to help.

          the problem i believe, is that it sounds like the iteration of the array is
          associated with the in-line declared variable used (first instance) as a
          type of pointer. the subsequent iterations are lost since apparently the for
          each loop still wants to use the first instance of the pointer variable. i
          wouldn't expect this behavior as this is very commonly done in c, c++, c#,
          java, javascript, etc. but then again, this is vb. ;^)

          i'd still use for each...but like this:

          dim lineDayRec as operatorlinereq uirements
          dim lineRec as operatorrequire ments

          For Each lineDayRec In operatorLineLis t
          For Each lineRec In operatorList
          If lineRec.Day = lineDayRec.Day Then
          operatorList.Ad d( _
          New OperatorRequire ments( _
          lineRec.Day, _
          lineRec.People _
          )
          )
          Exit For
          End If
          Next
          Next


          Comment

          Working...