Need explanation

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hd82
    New Member
    • Dec 2009
    • 3

    Need explanation

    First i would like to say hi i am new here and new to programming. im currently takeing an intor to progrmming class useing vb 2008 express edition. so basicly im a newbe. now as for my question. i had to write a program for class as a lab assignment The 12 days of christmas. i was able to write part of it but couldnt quite get it runnin rite. finaly my teacher sat down real fast and fixed my mistakes so it runs fine and i handed it in. the problem im havein is that the professor didnr explain to me wat i did wrong or why wat she did works so i have no idea whats what in the program. it basicly comes down to i dont understand Loops, For Next, or Arrays. if someone could explain to me wats goin on in the program and why it all works i think it would help me understand things finally. heres the code, runs perfectly and does exactly wat it is supposed to.
    Code:
    Public Class frm12DaysOfChristmas
        Dim itemName(12) As String
        Dim itemCost(12) As Double
        
    
        Private Sub btnDisplay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplay.Click
            Dim InputDay As Integer
            Dim CostOfDay As Double = 0
            Dim TotalCost As Double = 0
            InputDay = CInt(mtxtDayNumber.Text)
            lstResults.Items.Clear()
            If (CInt(InputDay) <= 0) Or (CInt(InputDay) >= 13) Then
                MsgBox("Please enter a number from 1 to 12")
                mtxtDayNumber.Clear()
                mtxtDayNumber.Focus()
            Else
                For dayNum As Integer = 1 To InputDay
                    lstResults.Items.Add(itemName(dayNum))
                    CostOfDay = dayNum * itemCost(dayNum) + CostOfDay
                Next
                lstResults.Items.Add(FormatCurrency(CostOfDay))
                CostOfDay = 0
                'Grand Total
                For iCount As Integer = 1 To 12
                    For dayNum As Integer = 1 To iCount
                        CostOfDay = dayNum * itemCost(dayNum) + CostOfDay
                    Next
                Next
                lstResults.Items.Add("Total Cost " & (FormatCurrency(CostOfDay)))
            End If
        End Sub
        Private Sub frm12DaysOfChristmas_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            itemName(1) = "1 Partridge in a pear tree"
            itemName(2) = "2 Turtle doves"
            itemName(3) = "3 French hens"
            itemName(4) = "4 Calling birds"
            itemName(5) = "5 Golden rings"
            itemName(6) = "6 Geese a laying"
            itemName(7) = "7 Swans a swimming"
            itemName(8) = "8 Maids a milking"
            itemName(9) = "9 Ladies dancing"
            itemName(10) = "10 Lords a leaping"
            itemName(11) = "11 Pipers piping"
            itemName(12) = "12 Drummers drumming"
    
            itemCost(1) = 164.99
            itemCost(2) = (20.0)
            itemCost(3) = (15.0)
            itemCost(4) = (149.99)
            itemCost(5) = (79.0)
            itemCost(6) = (60.0)
            itemCost(7) = (600.0)
            itemCost(8) = (5.85)
            itemCost(9) = (528.8)
            itemCost(10) = (428.51)
            itemCost(11) = (201.22)
            itemCost(12) = (199.82)
        End Sub
    End Class
    Last edited by debasisdas; Dec 9 '09, 04:45 PM. Reason: Added code tags.
  • hd82
    New Member
    • Dec 2009
    • 3

    #2
    anyone out their to offer help

    Comment

    • PRR
      Recognized Expert Contributor
      • Dec 2007
      • 750

      #3
      Can you explain what your program does? Then maybe it will be easier to say something. Its difficult to read code and "get" the whole picture.

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        I seriously dislike VB.NET for loops.
        C# loops are so much nicer (the same loop syntax for C++, Java, JavaScript and other languages too)

        Let's ignore your program for the time being and take a look at the following loop:
        Code:
        For counter As Integer = 1 to 10
          'do stuff
        Next
        This loop declares a variable "counter" as an Integer and sets it to 1.
        It's a short hand version of:
        Code:
        Dim counter As Integer = 1
        For counter To 12
          'do stuff
        Next
        The for loop will loop from 1 to 12 in this case and then end.
        You can access what loop iteration you're at using the "counter" variable.
        Say you had the following array of Strings:
        Code:
        Dim myArrayOfTwelveNumbers(12) As String= {"zero","one","two","three","four","five","six","seven","eight","nine","ten","eleven","twelve"}
        'The above array has been declared to hold 13 strings that contain text for numbers 0 - 12
        You can loop through this array and print each the text stored in each element by accessing the element at "counter" for each loop iteration:
        Code:
        Dim myArrayOfTwelveNumbers(12) As String= {"zero","one","two","three","four","five","six","seven","eight","nine","ten","eleven","twelve"}
        For counter As Integer = 1 To 12
          Console.WriteLine(myArrayOfTwelveNumbers(counter))
        Next
        This will print:
        Code:
        one
        two
        three
        four
        five
        six
        seven
        eight
        nine
        ten
        eleven
        twelve
        Note that "zero" is not printed. That is because "zero" exists at the array index 0 and you are looping from 1 to 12.

        If you only wanted to print "four","five"," six" you'd start the loop at element 4 instead:
        Code:
        Dim myArrayOfTwelveNumbers(12) As String= {"zero","one","two","three","four","five","six","seven","eight","nine","ten","eleven","twelve"}
        For counter As Integer = 4 To 6
          Console.WriteLine(myArrayOfTwelveNumbers(counter))
        Next
        It's pretty simple....but I still hate them.

        -Frinny

        Comment

        • hd82
          New Member
          • Dec 2009
          • 3

          #5
          to prr, the prgram is supposed to request an integer from 1 through 12 and then list the gifts for that day along with that days cost. on the nth day, the n gifts are 1 partridge in a pear tree, 2 turtle doves,... n of the nth item. the prgram should also give the total cost of all twelve days.

          Comment

          • PRR
            Recognized Expert Contributor
            • Dec 2007
            • 750

            #6
            I guess Frinavale has answered pretty much most of your queries. Adding to her ans, you can look into:
            Convert to int
            Switch statement

            Comment

            Working...