Results 1 to 6 of 6

Thread: [RESOLVED] vb loop issue - for each??

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2013
    Posts
    4

    Resolved [RESOLVED] vb loop issue - for each??

    OK I have a calculator - to work out flooring required.

    This is an assignment so really looking for guidance on what to do rather than an answer.

    I've done 95% of the calc and its functions.

    There is a datagridview on a form for outputting records.

    one of the requirements for 5% of the total mark is to output in the dgv on a separate form the total area of each floor required for each floortype.

    I have this structure in a module:

    Code:
    Module Module1
        Public Structure flooring
            Dim lngth As Integer
            Dim wdth As Integer
            Dim floortype As String
        End Structure
    
        Public myFlooring As flooring
        Public colFloor As New Collection
    
    
    End Module

    The user inputs length - width and flooring type (limited to four types)
    on this particular output i need to find each occurrence of a floortype ie beech add the lengths together for each occurrence of beech. then the same for width.
    Then I need to multiply total length of beech * total width of beech and output this onto a datagridview.

    I have almost no idea what I'm doing.. Think i missed a class or something lol

    this is nowhere near correct I know, I've probably been closer but couldn't see it so am just writing random crap now lol

    Code:
        Private Sub btnTotals_Click(sender As System.Object, e As System.EventArgs) Handles btnTotals.Click
            dgvFlooring.Rows.Clear()
            dgvFlooring.Columns.Clear()
            dgvFlooring.Columns.Add("FloorType", "Floor Type")
            dgvFlooring.Columns.Add("Total", "Total")
    
            Dim beechtotalLength As Integer
            Dim beechtotalWidth As Integer
            Dim beechtotalArea As Integer
    
            Dim Bluetotalwidth As Integer
            Dim bluetotallength As Integer
            Dim bluetotalarea As Integer
    
            Dim floor As flooring
    
    
            For Each quote As Object In colFloor
    
    
                If CStr(floor.floortype) = "Beech" Then
                    beechtotalLength += floor.lngth
                    beechtotalWidth += floor.wdth
                End If
    
            Next
    
            beechtotalArea = beechtotalLength * beechtotalWidth
    
            dgvFlooring.Rows.Add("blue", bluetotalarea.ToString)
            dgvFlooring.Rows.Add("Beech", beechtotalArea.ToString)
    
        End Sub

    so I'm looking for in dgv

    say there are 3 entries of beech floor
    floortype beech length 6 width 6
    floortype beech length 2 width 8
    floortype beech length 6 width 10

    i need to have a total length for beech 6+2+6
    and a total width for beech 6+8+10

    then as output a total area in the dgv (total length * total width)
    ie:

    Beech - 336
    blue - 447
    etc..

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: vb loop issue - for each??

    Wouldn't it be a bit easier to calculate the area for each record rather than calculating total length and total width? Therefore, the first line would give you:

    6x6 = 36

    The second line would give you 2x8=16, and the running total would then be up to 52. The final line would add in a further 60 for a total of 112.

    The advantage of doing it that way is that you only need to keep a running total of the area rather than keeping a running total of both length and width, but it really makes very little difference, as the former would require one variable while the latter would require two. The actual work is pretty much the same either way.

    In any case, it looks like you've pretty much done it, so what are you asking for? You loop through the lines in the DGV looking for lines of a certain type, you then add up the length and the width, and after the loop you multiply the result. That's all what you said you needed to do, so what is left? The structure (which shouldn't be in a module, because a module is really just a class, so you have a module defined inside a class definition, which isn't bad but isn't needed, either) isn't really used for anything, but so what?
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    New Member
    Join Date
    May 2013
    Posts
    4

    Re: vb loop issue - for each??

    thanks for the quick reply.

    I'm just assuming I'm miles off because it does nothing - if it did something I might have a clue.. but its doing absolutely nothing lol

    the beechtotalArea.tostring just comes up as 0

    This bit of the IF statement is underlined in green:

    If CStr(floor.floortype) = "beech" then

    it says: variable "floortype" is used before it has been assigned a value. A NULL reference exception could result at runtime.

  4. #4

    Thread Starter
    New Member
    Join Date
    May 2013
    Posts
    4

    Re: vb loop issue - for each??

    hmm figured out what was up.. i think..

    new code:

    Code:
        Private Sub btnTotals_Click(sender As System.Object, e As System.EventArgs) Handles btnTotals.Click
            dgvFlooring.Rows.Clear()
            dgvFlooring.Columns.Clear()
            dgvFlooring.Columns.Add("FloorType", "Floor Type")
            dgvFlooring.Columns.Add("Total", "Total")
    
            Dim beechtotalLength As Integer
            Dim beechtotalWidth As Integer
            Dim beechtotalArea As Integer
    
            Dim Bluetotalwidth As Integer
            Dim bluetotallength As Integer
            Dim bluetotalarea As Integer
    
            Dim quote As flooring
    
    
            For Each quote In colFloor
    
    
                If CStr(quote.floortype) = "Beech" Then
                    beechtotalLength += quote.lngth
                    beechtotalWidth += quote.wdth
                    beechtotalArea = beechtotalLength * beechtotalWidth
                End If
    
            Next
    
    
    
            dgvFlooring.Rows.Add("blue", bluetotalarea.ToString)
            dgvFlooring.Rows.Add("Beech", beechtotalArea.ToString)
    
        End Sub

  5. #5

    Thread Starter
    New Member
    Join Date
    May 2013
    Posts
    4

    Re: vb loop issue - for each??

    ok so now the issue is with 2 if statements or an else if statement.

    It is just adding the totals together.

    so blue and beech come out with the same total area.

    Code:
        Private Sub btnTotals_Click(sender As System.Object, e As System.EventArgs) Handles btnTotals.Click
            dgvFlooring.Rows.Clear()
            dgvFlooring.Columns.Clear()
            dgvFlooring.Columns.Add("FloorType", "Floor Type")
            dgvFlooring.Columns.Add("Total", "Total")
    
            Dim beechtotalLength As Integer
            Dim beechtotalWidth As Integer
            Dim beechtotalArea As Integer
    
            Dim Bluetotalwidth As Integer
            Dim bluetotallength As Integer
            Dim bluetotalarea As Integer
    
            Dim quote As flooring
    
    
            For Each quote In colFloor
    
    
                If CStr(quote.floortype) = "Beech" Then
                    beechtotalLength += quote.lngth
                    beechtotalWidth += quote.wdth
                    beechtotalArea = beechtotalLength * beechtotalWidth
                ElseIf CStr(quote.floortype) = "Blue Laminate" Then
                    bluetotallength += quote.lngth
                    Bluetotalwidth += quote.wdth
                    bluetotalarea = beechtotalLength * beechtotalWidth
                End If
            Next
    
    
    
            dgvFlooring.Rows.Add("Blue", bluetotalarea.ToString)
            dgvFlooring.Rows.Add("Beech", beechtotalArea.ToString)
    
        End Sub

    lol ok i just read the above again and saw what a prize banana i am.. all fixed..

    wouldnt believe how many hours i was staring at this and just going away and coming back and i can see all my errors lol.

    thanks!!
    Last edited by djrx; May 13th, 2013 at 04:16 PM. Reason: i'm a dumbass

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: [RESOLVED] vb loop issue - for each??

    It should NEVER be able to do nothing. The second thing that should be taught in a programming class is how to use breakpoints. There is no more powerful tool for programming, and most classes seem to leave it out entirely. Once you know breakpoints, no program will EVER do nothing, and if it does, you'll know about it.

    As you might imagine, the most powerful tool has been made super easy to use. Click in the left margin of the code page beside some line of code. There are a few margins, so it can be hard to know which one, but click in the right one and the line will turn maroon, and a maroon ball will appear in the margin. Click on that maroon ball and the breakpoint goes away. You can also use something like F9 while the cursor is in a line of code, and there's a menu on the toolbar, too.

    Once you have a breakpoint set, when execution hits that breakpoint, it will suspend. At that time, you can see what each variable holds, what type it is, and so forth. Generally, you can do this by hovering the mouse over the variable, but sometimes you have to put the cursor in the variable and press Shift+F9. You can highlight entire function calls and use Shift+F9 to see what it will return, too, as well as setting watch variables (so that you can see what a variable holds and watch it change as you step through the code). By doing this, you can see the total state of the system at any time.

    From there, F10 and F11 will step you through the code line by line. There's a difference between the two, but you figure that out. You'll always choose the wrong one, anyways, so why bother mentioning the difference? Therefore, you can see the current state, execute one line, see how things change, execute the next line, and so on. So, you see, no program can ever do nothing, because you can watch everything inside it change line by line. At any time, pressing F5 will return it to normal and it will continue until it hits the next breakpoint, or until it ends, or whatever.
    My usual boring signature: Nothing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width