|
-
Feb 25th, 2009, 06:25 PM
#1
Thread Starter
New Member
[RESOLVED] [2008] Adding elements in array
Hi,
I am just starting VB at 60 years old and with no programming experience and nobody to ask for help...except here, of course. So needless to say I am hitting brickwalls. I am using "Murach's Visual Basic 2008" and it seems to be a good book. This is what I am up to -
I have to declare an array with 5 elements with an index. I think that part is OK. Then add code that adds each element to the next one every time a user clicks a button. I have had many tries but the best I can get is just the grand total of the elements. Here is the code I came up with:-
Code:
Private Sub btnCalculate_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnCalculate.Click
Dim elements As Decimal
Dim sum As Decimal
Dim numbers() As Decimal = {350.49D, 546.73D, 123.45D, 56.85D, 752.69D}
For Each elements In numbers
sum += elements
elements = 0
Next
txtTotal.Text = FormatCurrency(sum)
Is anyone able to show me how the button click would reveal a progressive total of the elements each time a user clicked on it? It would be good if the book came with answers or had a forum for their readers. Grateful for any help but be gentle.
-
Feb 25th, 2009, 07:01 PM
#2
Re: [2008] Adding elements in array
What do you mean by a "progressive" total? Otherwise your code looks fine as it should give you a total of all the elements in the array. What is the "progressive" that your looking for?
Perhaps an example of what you are trying to do?
-
Feb 25th, 2009, 07:20 PM
#3
Re: [2008] Adding elements in array
This code starts with a CurrentIndex of 0, so the first time the button is clicked, it adds number(0) to the CurrentTotal, then next time it adds number(1) etc. until the CurrentIndex is greater than the numbers.Length-1. Arrays are zero based, so you have to remember to subtract 1, so numbers has 5 elements, the last is numbers(4).
Code:
Dim numbers() As Decimal = {350.49D, 546.73D, 123.45D, 56.85D, 752.69D}
Dim CurrentIndex As Integer = 0
Dim CurrentTotal As Decimal = 0
Private Sub btnCalculate_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnCalculate.Click
If CurrentIndex < numbers.Length Then
CurrentTotal += numbers(CurrentIndex )
txtTotal.Text = FormatCurrency(CurrentTotal).ToString
CurrentIndex += 1
End If
End Sub
Last edited by Bulldog; Feb 25th, 2009 at 07:42 PM.
-
Feb 25th, 2009, 08:04 PM
#4
Thread Starter
New Member
Re: [2008] Adding elements in array
Bulldog has nailed it for me. Thanks to Vectris and Bulldog for their time.
-
Nov 22nd, 2009, 10:03 AM
#5
New Member
Re: [RESOLVED] [2008] Adding elements in array
Sparker
I have started Murach's VB 2008 too! is this the example in chapter 8 exercise? i got stuc there too and started googling all over the place and finally cam here. This looks like a gr8 place. Looks like ur way ahead on ur vb course. cani buzz u if im stuck anywhere else.
-
Nov 22nd, 2009, 07:22 PM
#6
Thread Starter
New Member
Re: [RESOLVED] [2008] Adding elements in array
 Originally Posted by turtlet
Sparker
I have started Murach's VB 2008 too! is this the example in chapter 8 exercise? i got stuc there too and started googling all over the place and finally cam here. This looks like a gr8 place. Looks like ur way ahead on ur vb course. cani buzz u if im stuck anywhere else.
Sorry Turtlet but I have given up (again) for now. My problem is that I need to have a reason to persevere and I was just doing it for a hobby. There are lots of others here that are very helpful and more knowledgeable. Stick with it and best of luck with your VB. I'll have another go later on....probably call on you for help!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|