Results 1 to 8 of 8

Thread: Function help

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2007
    Posts
    6

    Function help

    Hello i recently started to use functions but do not understand the concept. I have worked on a project out of a book that adds coins(quarters, dimes, nickels, pennies) and adds them all up using a function. This is what i have done:

    Dim sngCalculate As Single
    --------------------------------------------------------------------------
    Private Sub btnAddCoins_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddCoins.Click
    Dim sngQuarters As Single = Val(Me.txtValue1.Text)
    Dim sngDimes As Single = Val(Me.txtValue2.Text)
    Dim sngNickels As Single = Val(Me.txtValue3.Text)
    Dim sngPennies As Single = Val(Me.txtValue4.Text)

    sngQuarters = sngQuarters * 25
    sngDimes = sngDimes * 5
    sngNickels = sngNickels * 10
    sngPennies = sngPennies * 1

    Me.lblAnswer.Text = Calculate(sngCalculate)
    End Sub
    --------------------------------------------------------------------------
    Function Calculate(ByVal sngQuarters As Single, ByVal sngDimes _
    As Single, ByVal sngNickels As Single, ByVal sngPennies As Single) As Single

    sngCalculate = sngQuarters * sngDimes * sngNickels * sngPennies
    Return sngCalculate
    End Function

    End Class
    This does not work and i am uncertain of if i am doing the function correctly. Any help would be greatly appreciated. So far it adds everything to 0.
    Last edited by Inugami; Feb 6th, 2007 at 09:48 PM.

  2. #2
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Function help

    functions return a value. That means using that global variable sngCalculate is not needed at all.

    The other thing is, before you are calling this function, you are already calculating the money value of the change that was entered, so multiplying them in your calculate function will produce wrong results. You would want to add them there right? Add up all the value of the dimes, quarters, etc. together and return the value.

    I would think your calculate function would look something like this.

    Also in your code, you are only trying to pass 1 value to the routine, where in reality, you need to pass the quarters, dimes, nickels and pennies to it.

    VB Code:
    1. Function Calculate(ByVal sngQuarters As Single, ByVal sngDimes _
    2.                      As Single, ByVal sngNickels As Single, ByVal sngPennies As Single) As Single
    3.         Return sngQuarters + sngDimes + sngNickels + sngPennies
    4.     End Function

    another suggestion is to move your money multiplying from the button click sub routine into your calculate routine, as that is a better place for it.

  3. #3

    Thread Starter
    New Member
    Join Date
    Feb 2007
    Posts
    6

    Re: Function help

    oh wow, why did i multiply? I'm so stupid xD

    thanks for the help.

  4. #4
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Function help

    no problem..

    And welcome to the forums

  5. #5

    Thread Starter
    New Member
    Join Date
    Feb 2007
    Posts
    6

    Re: Function help

    Thanks but i have another problem that i dont understand
    Me.lblAnswer.Text = Calculate(sngCalculate)

    Its underlined and says


    whats the problem ><?

  6. #6
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Function help

    the problem is your calculate function is expecting you to pass your quarters, dimes, nickels, and pennies variables (in that order) to it. Not just one sngCalculate variable (which as I said is not needed at all.. you should just delete it from your code)

    VB Code:
    1. Me.lblAnswer.Text = Calculate(sngQuarters, sngDimes, sngNickles, sngPennies).ToString

  7. #7

    Thread Starter
    New Member
    Join Date
    Feb 2007
    Posts
    6

    Re: Function help

    Ah yes thank you, i did the exact same thing but without the .ToString

    What is the need or purpose of adding .ToString at the end?

  8. #8
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Function help

    .ToString converts a numeric data type (single) into a string, because the labels text property is a string.

    Because the number value 123 and the string "123" are not the same thing, but either could be converted to the other form using the various conversion functions found right in the framework.

    Like
    VB Code:
    1. Dim MyString as String = (123).ToString
    2. 'or
    3. Dim MySingle as Single = convert.tosingle("123")

    You probably don't HAVE to do this in your code because you are likely coding with Option Strict Off.

    If you code with Option Strict On, then the IDE would complain about your code and tell you it can't just convert the number value to a string value, which is why you call .ToString.

    I always recommend people code with option strict on, but for whatever reason, MS has it off by default in VB.NET, likely do appease VB6 programmers moving to .NET, where things like option strict did not exist.

    However someone new to programming would likely never know about this feature unless someone told them, or they stumbled across it.

    Here is the documentation for Option Strict.
    http://msdn.microsoft.com/library/de...tionstrict.asp

    I suggest turning it on, because it will lead to better code writing, and less weird errors later...

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