How to use counters and accumulators to add a running total and display on a new form-VBForums
Results 1 to 16 of 16

Thread: How to use counters and accumulators to add a running total and display on a new form

  1. #1

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

    How to use counters and accumulators to add a running total and display on a new form

    Hello Everyone,

    I am wanting to know how to use counters and accumulators to add a running total of integers typed into a textbox and later display those added integers to a separate form.

    The description I was given was this:
    "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 have been able to configure the first part. I am just not sure how to make it keep track of the numbers of deposits and running total using counters and accumulators.

    Thanks for the help!

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    97,359

    Re: How to use counters and accumulators to add a running total and display on a new

    You seem to be looking for some magic solution. I've already explained how to do this step by step in an earlier thread. How does counting usually work? You start with a number and you add 1 to it, repeatedly. That's exactly how it works in programming. You declare an Integer variable and you add 1 to it each time the thing you're counting occurs. If you want to display the count then you convert the variable to a String and assign it to the Text of a Label. It's basically the exact same process as you would use if you were doing it manually.
    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

  3. #3

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

    Re: How to use counters and accumulators to add a running total and display on a new

    Well I'm sorry that I have only been programming for about a month and don't understand all there is to coding. This is my first experience with programming. Hopefully I can comprehend what you said in the last post but if not I will continue the search to figure it out.

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    97,359

    Re: How to use counters and accumulators to add a running total and display on a new

    You don't need to understand all there is and nowhere did I imply that you do or should. What you should understand is that programming doesn't exist in a vacuum. It's simply a representation of the real world. If you know how to count in the real world then you know how to count in a computer program. Don't try to complicate things by assuming that there is some mystery to simple things.

    Break down your problem into parts and address each part individually. Many beginners take their specific problem as a whole and start looking for a turnkey solution. Such things rarely exist. All programs are made up of smaller parts, just like machines in the real word. Just as you can build a custom machine to perform a specific job out of off-the-shelf parts, so you can build a custom application to perform a specific task out of smaller parts that are basically turnkey solutions. For instance, counting is going to be done EXACTLY the same way regardless of what the application does that it's being used in. If you have learned about variables, expressions and methods, which you should have done by now, then you already know all you need to know to implement counting in your application. Just as it is in the real world, keeping a sum is pretty much exactly the same as counting except that you will add a newly-entered value to your running total instead of 1 to a count. It's still just adding one number to another and you already know how to do that.

    So, you don't know everything but you don't need to. You do know some things and you should be putting them to use. Pick up a pen and paper and write down the steps involved in creating your application. Research each step in isolation. Learn what's required to implement that step and do it. Once you've done that for all steps, you can look at integrating the partial solutions into a whole.
    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

  5. #5

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

    Re: How to use counters and accumulators to add a running total and display on a new

    Ok so far I have this:
    Const intMAX_SUBSCRIPT As Integer = 999999999
    Dim intSeries(intMAX_SUBSCRIPT) As Integer
    Dim intCount As Integer

    For intCount = 0 To intMAX_SUBSCRIPT
    intSeries(intCount) = 100
    Next

    Now I'm having trouble applying it to a label. This label is on another form in the program and not on the main form.

  6. #6
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    97,359

    Re: How to use counters and accumulators to add a running total and display on a new

    Presumably you know how to display something in a Label. If you don't, that's very easy to find out. As I said earlier, break the problem down. Getting data into another form and displaying data in a Label are two different things. You are (I hope) only having trouble with one of them. To learn the principles involved in moving data between forms, I suggest that you follow the Blog link in my signature below and read my three-part post on the subject. Once you've learned the principles, you can apply them to this and any other scenario in which you need to move data between forms. Once the data is in the other form, displaying the data in a Label is child's play.

    By the way, the code you just posted does not do what the assignment specifies or what I described here or in your previous thread with regards to counting. If this is about isolating one part and addressing that specifically then fair enough but if you think that you've already implemented the counting part there then you need to rethink that.
    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

  7. #7

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

    Re: How to use counters and accumulators to add a running total and display on a new

    Well I'll just have to turn in what I have and fail because its due in a few hours. I do know how to display something in a label however I have never displayed something in a label when the data it needs is on another form. So I don't know how to get the data to the other form. The book I'm using only have one example of arrays and its completely different from what my project is.

  8. #8
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,278

    Re: How to use counters and accumulators to add a running total and display on a new

    I'm not going to do your homework for you, but I'm going to show you something close. Please understand you have to understand WHY this works, because programming is an accumulative process. It's like baking: you can follow a recipe but if you don't understand the physics behind steps like "creaming" the butter, you won't understand what to do with the next batch if the cookies don't turn out just right.

    So if you copy/paste what I do to get a grade, you're going to find your next task miserably difficult, because it'll assume you can do this assignment by yourself. Ten years ago, "my textbook sucks" was a decent excuse. Today there are so many online tutorials for the basic concepts in every language, you'd do better to leave that out of your homework help questions. If I search right now, I can find at least a dozen highly-regarded college/university course notes online. Not many of them are VB, there's a good reason: it's a poor choice for a learner language at this juncture in time. So if you have a bad teacher in a poor language, perhaps reconsider this choice of school. I'm dead serious. I see too many students who post slides that make me weep for their wallets.

    Also worth keeping in mind: JMC is a very well-respected VB expert, so how he's treating you is how the majority of VB developers act towards newbies. If you aren't very comfortable with this, it's easier to change your career path now than later. I'm a C#/Swift developer, and I'm treating you how people in those communities like to treat newbies. To be fair, there are lots of JMCs in the other communities too. The difference is VBForums has long upheld "JMC is an exemplary expert", and in other communities we don't tend to tolerate it so much.

    If I were wrong, it wouldn't have happened so quickly, and the forums wouldn't tolerate it, right?

    Let's talk shop and ignore that he exists. You don't want his advice, and heck, since he hasn't really posted anything maybe he doesn't know how to solve the problem, right? Code is proof. So far all he has is words.

    You start any project with an empty form Class:
    Code:
    Public Class Form1
    
    End Class
    A Class can contain many things, but only two things matter for our purposes:
    • Fields are variables that "belong" to an instance of the class.
    • Methods are Subs/Functions that "do" things with an instance of the class.

    If you drag a button on your form, then double-click the button, Visual Studio will automatically generate a Method that looks like this:
    Code:
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
    End Sub
    Let's make the Method do something. When you click the button, this will change the Text to a number:
    Code:
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim value As Integer = 10
        value = value + 10
        Button1.Text = value.ToString()
    End Sub
    If you click the button, its Text will change to 20. If you click it again, it's still 20. Why? Well, "value" belongs to the Sub. Every time you click the button, the variable is created again. It gets the value 10, then 10 is added. So every time, you get 20.

    What you want if you want to accumulate values is a field. Since fields belong to the class, they'll have the same value no matter how many times a method is called. They get created when the class is created, which for simple, 1-form applications is "when the application starts. Make your form look like this:
    Code:
    Public Class Form1
    
        Private _buttonValue As Integer = 0
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            _buttonValue += 10
            Button1.Text = _buttonValue.ToString()
        End Sub
    
    End Class
    Now, the first time you click the button, the Text changes to 10. Then, the next changes to 20. Then 30. And so on. Because a field belongs to the class, it doesn't get "reset" every time a method is called. So we use fields for things that should be "remembered" between method executions.

    Now, let's say there's a TextBox on the form. Add one. Make the Button1_Click() method look like this:
    Code:
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim value As Integer = Integer.Parse(TextBox1.Text)
        value += 10
        Button1.Text = value.ToString()
    End Sub
    There's actually a lot happening here, even though it's just 3 lines.

    The value in the TextBox is represented as the Text property, which is a String. A String can be a number like "13", but it could also be not a number like "pony". So you can't safely treat "any string" as an Integer and do math on it.

    Integer.Parse() is a method that takes a String as a parameter. If that String is a number, it will return that number as an Integer value. If it's not a number, it will throw an exception and crash your program. We could deal with that severla ways, but your assignment doesn't require it so let's not worry about it. Always type numbers in the text box.

    If you typed a number, Integer.Parse() will return that number, and it will get stored in value. Then 10 gets added to the value and the Button's text is updated. An Integer isn't quite a String, so I used the .ToString() method to convert it. It turns out just about anything in .NET wants to be converted to a String, so the .ToString() method is always safe.

    So if you type 123 in the TextBox, this will change the button's text to 133.

    If you want a sum, you need a variable that doesn't get reset every time the method is called. That's a field! Check out this code:
    Code:
    Public Class Form1
    
        Private _sum As Integer = 0
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim value As Integer = Integer.Parse(TextBox1.Text)
            _sum += value
            Button1.Text = _sum.ToString()
        End Sub
    
    End Class
    It's such a small change, but it behaves so different!

    value is the number in the text box. _sum is the accumulated sum. If you type 123 in the box, then click the button, its text should change to 123. Click it again. Its Text should be 246. Type 1 in the text box and click the button. Its text should be 247.

    Now.

    I have showed you an example that counts the number of times you push a button. I've showed you an example that keeps a running sum of things you typed in a text box.

    It's your job to do those two things at the same time. Your form will have two different fields, and one method to handle the button click. The click handler should update the count, and it should also Parse() the value in the TextBox and add it to the field that represents the sum. I used a button. You have to use a menu item. Conceptually, they are the same thing: "I click on it and the event handler is called."

    Try something. See if it works. If you can't get it working, post what you tried and at some point in the next 24 hours, 2 things will happen:

    • JMC will have some new smart aleck remark to make you feel stupid, and won't be able to demonstrate he even understands the problem.
    • I'll try and have some new insight to help you get closer.


    Also remember: the programming community is full of JMCs and doesn't have very many of me. You might consider a different job path if that upsets you. Being "a programmer" is a cruddy job. Being "anything else but able to program" is often exciting.
    Last edited by Sitten Spynne; Dec 10th, 2017 at 09:51 PM.
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

  9. #9

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

    Re: How to use counters and accumulators to add a running total and display on a new

    Thank you Sitten Spynne! All I was asking for was an explanation of the processes of it! Unfortunately I had to submit the project because it was due. But I would like to continue working on it with your suggestions and help and see if I cant make it work. It is not about the grade for me, its about getting it done right! Also I have no desire to be a computer programmer. This class is required because i'm aiming for a degree in Computer Information System!

  10. #10
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    97,359

    Re: How to use counters and accumulators to add a running total and display on a new

    Quote Originally Posted by VBAppleCoder View Post
    All I was asking for was an explanation of the processes of it!
    Which seems curious, after having said that you had it working in two previous threads.
    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

  11. #11

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

    Re: How to use counters and accumulators to add a running total and display on a new

    I never said I had it working I only said that was the code I had. How am I suppose to know whether it works or not when it does not show in a label as I explained it needed to above. Maybe if you could get up off your high horse and learn how to explain things at a beginners level and stop criticizing those who are beginners you could understand what someone is asking.

  12. #12
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    97,359

    Re: How to use counters and accumulators to add a running total and display on a new

    Quote Originally Posted by VBAppleCoder View Post
    I never said I had it working
    You said that you had it working here:

    http://www.vbforums.com/showthread.p...t=#post5242135

    and here:

    http://www.vbforums.com/showthread.p...t=#post5242139

    You showed here that you know how to display something in a Label:

    http://www.vbforums.com/showthread.p...t=#post5223479

    Based on that, the only issue was how to pass data from one form to another and I provided you with information on how to do that in post #6. I'm really not seeing where the issue is. I'm also not quite sure when criticising someone became such a terrible thing.
    Quote Originally Posted by VBAppleCoder View Post
    How am I suppose to know whether it works or not when it does not show in a label as I explained it needed to above.
    We've already established that you do know how to display something in a Label that would be how you were supposed to know whether it was working or not. You could use a Label on the same form to at least confirm that the counting and summing part was working, so the second form part isn't relevant to that.

    Of course, nothing in Sitten Spynne's rather lengthy post addresses displaying data in a Label on any form, let alone a different form, which is what you insist you were asking about. It's rather ironic that that same post that doesn't address the actual question contains the suggestion that I don't understand the problem. You can check out the blog post I mentioned or not. It's good information on the specific topic that I you insist is at issue, but you might prefer not to accept help for someone so critical.
    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

  13. #13

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

    Re: How to use counters and accumulators to add a running total and display on a new

    Well it no longer matters anymore. I have failed the assignment and that’s that. Sorry to have wasted all of your times when I clearly can’t expalin what I’m looking for due to that fact that I have no idea what I am doing! Thanks!

  14. #14
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    97,359

    Re: How to use counters and accumulators to add a running total and display on a new

    I don't think that you should give up so easily. One of the most important lessons that a beginner can learn is one that I mentioned earlier, i.e. divide and conquer. The reason that problems can seem daunting is that they are just too complex to get your head around in one go. The same applies to more experienced programmers, except that with greater experience comes the ability to handle bigger problems in one go. The sort of problems we tackle in a professional setting are still too much to handle in one go though, so we break them down into more manageable parts. That's what you need to do too, except that the parts need to be smaller. That's really a big part of problem solving in any setting. That original problem can be broken up into lots of little parts:
    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.
    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
    Each one of those parts can be tackled in isolation and the partial solutions combined into a single solution that addresses the whole assignment. If you have an issue with any specific part then you can ask questions about that part specifically, without muddying the waters for you or for us by bringing multiple parts into the same thread. The part about counting and summing was addressed in a previous thread. You said that what you wanted was an explanation and you got it there. I said that you should declare a variable outside of any method and then add to it inside the method. You didn't say then that you didn't understand what that meant. What you did say was that you had it working based on someone else's advice to use a Static variable, so I assumed that that was that, which it apparently wasn't. If you ask a question and someone provides a response and you don't understand what they say, if you explain that at the time then it can likely be addressed at the time. If you say that it's working and then ask the same question later, is it fair to say that it's we who don't understand the question?

    Actually, now that I break it down like that, it's worth noting that this doesn't mention anything about a second form, although you seem to be saying that the Label it refers to is on another form. Was that detailed elsewhere? Regardless, you can do everything there without another form, simply using a Label on the same form. If you can get that far, the question then becomes simply how do you get data from one form to another, which is something that I addressed in post #6.

    In short, if you break a problem down then both you and we can concentrate on a single aspect of it at a time. There's then far less chance that you will become confused or that we will misunderstand the question or think that the question has already been answered when what you are actually asking different things with the same words at different times. The smaller and more specific the problem domain, the less the chance of any confusion on anyone's part.
    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

  15. #15

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

    Re: How to use counters and accumulators to add a running total and display on a new

    I understand! Next time I’ll write it down and really think about what I am having issues with. I was being to broad and I’m sorry. Also sorry about my frustration, it’s just that this was a for a Final Exam and I bombed it. But it’s all good now! I’ll continue working on it and look at the previous threads and see where I need to fix and if I have any real questions. Thank you!

  16. #16
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    97,359

    Re: How to use counters and accumulators to add a running total and display on a new

    Quote Originally Posted by VBAppleCoder View Post
    Also sorry about my frustration
    No problem. I don't take offence easily. It would be a bit hypocritical if I did . Sorry you didn't get the mark you wanted but I wouldn't have done anything different regardless. Hopefully you understand why but it is what it is either way.
    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

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

Survey posted by VBForums.