Results 1 to 5 of 5

Thread: New to Programming need help uinder

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2015
    Posts
    1

    New to Programming need help uinder

    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, however 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
    New Member
    Join Date
    Jan 2015
    Posts
    2

    Re: New to Programming need help uinder

    I'm fairly new to this myself, but hope this helps

    This line will convert the double variable ( counter being the position in the array) to a formatted string.

    So if counter is 5, the number 8206 will be formatted to a string such as $8206.00. The actual currency symbol comes from your windows system.

    Monthname(counter) will return the name of the month. If the counter is 5 then it will return May

    The listbox entry will then read

    $8206.00 May

    Hope this helps

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    23,393

    Re: New to Programming need help uinder

    here's an explanation:

    Code:
    Public Class Form1
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
            'this holds 12 values
            Dim Sales() As Double = {5601, 8502, 6703, 4204, 7065, 8206, 9107, 6508, 7209, 5010, 8011, 7012}
    
            Dim sum As String
    
            'counter initial value = 0
            Dim counter As Integer
            Do Until counter = Sales.length
                'array indices are zero based.
                sum = FormatCurrency(Sales(counter)) 'this takes the value from the Sales array, and converts it to a local currency string, storing it in the sum string variable
                counter = counter + 1
                'MonthName indices are one based.
                sum = sum & vbTab & MonthName(counter) 'this adds the month names to the sum string variable
                ListBox1.Items.Add(sum)
            Loop
        End Sub
    
    End Class

  4. #4
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,110

    Re: New to Programming need help uinder

    <edit>Just my ironic luck that this question has sat here for hours, and then as soon as I go to answer it .paul. gets in the first shot. Win some, loose some</edit>

    You should probably learn about using the debugger.
    For instance, since this code is in the Form's Load event, instead of hitting the "play button", which under the Debug menu would be the same as "Start Debugging F5", which some people call the Run button, you could select the "Step Into" option and you should see the program "step" though the code execution line by line. There will be yellow arrow (vb2010 and higher might have a different indication) on the left pointing to the line about to be executed. If you select the step button again, it will execute that line and stop on the next.
    You can move your mouse around to examine some of the values. You could also use a Watch window, or QuickWatch to look at values. There is also an Immediate window where you can execute a line of code, or print a value by using the question mark like:

    ? sum

    to see the value of sum.
    There are many, many options and ways to look at things in the code, so learning the debugging capabilities should be just as importing as learning to program.
    The line you indicated has several parts to it, and I don't know which part you don't understand, so I guess I'll have to assume I need to explain all parts.
    sum = FormatCurrency(Sales(counter))
    1. You have an array Sales, which was preset with a bunch of values {5601, 8502, ... etc.

    2. counter is variable that will be incremented in the loop and used to index into the array to access each value in the array.

    3. sum is a string, so seems to be badly named since it is not a sum of anything.

    4. FormatCurrency is a function, which will take a number parameter and return a string, formatted to indicate a monetary value.
    For instance with Windows set to U.S. locale, if you stepped through the code and after executing that line you hovered you mouse over the "sum" variable, it shows a tooltip 'sum "$5,601.00".
    So, the funtion took the number 5601 (which was put in an array of Double, so as a string would normal print like "5601.0") and instead return a string formatted as what it is programmed to display for Currency.

    As for the second line, try stepping through the code and see what that is doing.
    sum = sum & vbTab & MonthName(counter)

    What is counter at this point the first time through the loop? Answer should be: 1
    If you are stopped in the code, and you got to the immediate window and type in:
    ? monthname(1)
    What is printed?
    What if you type in
    ? monthname(3)

    The "&" symbol in this context is concatenating strings, i.e. joining them together.
    vbTab is a tab character, so the line is setting sum equal to what it was "$5,601.00" and a Tab, and a month name.
    The tab character indicates that the point the next character in the string should start is at the next Tab position in the output.
    What the next tab position is can depend on what the tab setting is on the device you're printing to. It may commonly be every 2 spaces, or 4 spaces, or 8 spaces.
    This will tend to line the Month names into a column, since the first character of the month should align with the next tab position. Assuming all the currency value strings are short enough to fit before the first tab position, all the months will line up, otherwise, if a currency value exceeds the available area, the month will align with the next tab position.

  5. #5
    Addicted Member
    Join Date
    Mar 2010
    Posts
    226

    Re: New to Programming need help uinder

    Try to help other you don't need to know Java just rock with Visual Basics of .net

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