dcsimg
Results 1 to 8 of 8

Thread: [RESOLVED] Menustrip Function

  1. #1

    Thread Starter
    Junior Member VBAppleCoder's Avatar
    Join Date
    Oct 2017
    Posts
    26

    Resolved [RESOLVED] Menustrip Function

    Hello everyone,

    In my project, which is for a final in my class, I am needing some help with a function. This is what my instructor is asking of us.

    " Deposit (menu item) - after the user inputs value in the textbox for the "Enter Amount" and selects this menu item, it will read and convert the text into a
    value and send it to a function, which will calculate the deposit. In addition, this function will keep track of the number of deposits made (counter) and
    running total of deposits (accumulator). The menu item will also display the calculated value in the account balance label."

    I would greatly appreciate any help! I have finished programming a lot of other coding for this program yet this one item is holding me back.

    I do have code that displays the entered amount but when you enter a different amount it does not add it to the previous amount entered.

    Code for that is:

    ' Add the entered amount as deposit
    intAmount = txtAmount.Text
    If txtAmount.Text = True Then
    decAccountBalance += intAmount + decDeposit
    End If

    ' Display amount in the label
    lblBalance.Text = decAccountBalance.ToString("c")
    Last edited by VBAppleCoder; Dec 9th, 2017 at 08:17 PM.

  2. #2
    Hyperactive Member
    Join Date
    Nov 2017
    Posts
    409

    Re: Menustrip Function

    Based solely on the snippet of code you included, my best guess would be that you are running in to an issue with variable scope. Where/how is the decAccountBalance variable declared? If it is declared with "Dim" inside the function that is processing the deposit, change "Dim" to "Static". That will initialize the variable only the first time the function runs. With Dim, it will reset the value to 0 each time the function runs. Your textbook should have some explanation of Dim vs. Static.

    Also the "If txtAmount.Text = True" line is wonky in my opinion. If the purpose of that line is to ensure that the user entered a valid numeric value, there are more proper ways of doing that.

    Edit: Looking at your variables:

    - intAmount is assigned the value entered by the user in the textbox
    - decAccountBalance is your running total balance
    - decDeposit it isn't clear what purpose it has or where it is getting a value assigned.
    Last edited by OptionBase1; Dec 9th, 2017 at 09:14 PM.

  3. #3
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    100,051

    Re: Menustrip Function

    In addition, this function will keep track of the number of deposits made (counter) and running total of deposits (accumulator).
    If you need to store values that persist between method calls then you need variables declared outside of any method.
    it will read and convert the text into a value and send it to a function
    If you intend to call a method and pass it a number then you need to have a method with a numeric parameter. If you're dealing with monetary values then you would generally use Decimal. If there will only be whole values then you can use Integer.

    So, declare appropriate variables and declare an appropriate method. Next, write code to convert the String from the TextBox to an appropriate numerical type and call the method. If you are going to provide proper validation then you should use something like Decimal.TryParse. If you're allowed to assume that input will be valid then you can use Convert.ToDecimal, Decimal.Parse or CDec. Etc.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Junior Member VBAppleCoder's Avatar
    Join Date
    Oct 2017
    Posts
    26

    Re: Menustrip Function

    Ok so how would I fix this issue? I have taken out decDeposit because it does not have any function.

  5. #5
    Hyperactive Member
    Join Date
    Nov 2017
    Posts
    409

    Re: Menustrip Function

    Quote Originally Posted by OptionBase1 View Post
    Where/how is the decAccountBalance variable declared? If it is declared with "Dim" inside the function that is processing the deposit, change "Dim" to "Static". That will initialize the variable only the first time the function runs. With Dim, it will reset the value to 0 each time the function runs. Your textbook should have some explanation of Dim vs. Static.
    See bolded.

  6. #6

    Thread Starter
    Junior Member VBAppleCoder's Avatar
    Join Date
    Oct 2017
    Posts
    26

    Re: Menustrip Function

    I changed it to static like you said and it worked! Thanks!
    Last edited by VBAppleCoder; Dec 10th, 2017 at 05:47 PM.

  7. #7
    New Member
    Join Date
    Dec 2017
    Posts
    1

    Re: [RESOLVED] Menustrip Function

    Any chance you can spare the code you used?

  8. #8

    Thread Starter
    Junior Member VBAppleCoder's Avatar
    Join Date
    Oct 2017
    Posts
    26

    Re: [RESOLVED] Menustrip Function

    Quote Originally Posted by alexE View Post
    Any chance you can spare the code you used?
    Yes but it will have to be tomorrow!

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width