CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jan 2015
    Posts
    1

    New to Programming - need help understanding code.

    Hi all, sort of new to the programming scene and I was initially going to go in the deep end and try and learn java, but then I was recommended to start off with an easier language like Visual Basic, so here I am taking that advice.

    I bought a book called Visual Basics in easy steps, the book was initially easy to follow, howeve I have now come across a few things that have not been explained properly in this book.

    Code:
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
            Dim Sales() As Double = {5601, 8502, 6703, 4204, 7065, 8206, 9107, 6508, 7209, 5010, 8011, 7012}
    
            Dim sum As String
    
            Dim counter As Integer 
            Do Until counter = Sales.length
                sum = FormatCurrency(Sales(counter)) //this is what I don't understand, what does this part of the code mean?
                counter = counter + 1
                sum = sum & vbTab & MonthName(counter) // also this one
                ListBox1.Items.Add(sum)
            Loop
        End Sub
    Please look within this code and see where I'm confused.

    Thanks.

  2. #2
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: New to Programming - need help understanding code.

    You are dealing with an array of numbers.

    Sales(0) would be 5601 Sales(1) would be 8502 and so on for each number in the array
    The code is simply looping through the array, formatting the numbers into currency format and adding them to a listbox along with a month name which is also an array but apparently defined somewhere else.
    Always use [code][/code] tags when posting code.

  3. #3
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: New to Programming - need help understanding code.

    OK, let us have a look - just to add onto what DM has said.. :

    Code:
    Do Until counter = Sales.length
                sum = FormatCurrency(Sales(counter)) //this is what I don't understand, what does this part of the code mean?
                counter = counter + 1
                sum = sum & vbTab & MonthName(counter) // also this one
                ListBox1.Items.Add(sum)
            Loop
    The do Until loop will loop until a certain condition is met; in this case when counter becomes the same value as the length of the Sales array.
    For every loop iteration it will add a currency symbol to the current item in the array ( so let's use the first one ) - it will produce this :

    $ 5601

    Then, it will increment the loop counter

    Then it adds a Tab and the Current Monthname to the existing string - this is known as concatenation, read up on it here and read up on the VB MonthName function here

    Lastly, it adds the resulting string to the listbox. The resulting string ( assuming of course we are still dealing with the first item here ) would now look like :

    Code:
    $ 5601      January
    I hope it helps..

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

Featured