5 Replies - 6311 Views - Last Post: 18 September 2010 - 07:49 PM Rate Topic: -----

#1 xterminal01  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 83
  • Joined: 18-September 10

Rainfall Statistics calculation using loops

Posted 18 September 2010 - 04:13 PM

Attached File  rainfall.bmp (748.31K)
Number of downloads: 189

I am suppose to calculate in inches the "total" and "average" rainfall for a year.
I know how to add the values of textboxes and average them using txt1.text + txt2.text.
I am not sure how to approach this problem using loops in VB.
If anyone can get met started on how to start with the form above would be great.


This is what i have right now

Public Class RainfallStats

    Private Sub btmExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btmExit.Click
        Me.Close()

    End Sub

    Private Sub btmClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btmClear.Click
        txtJanuary.Clear()
        txtFebruary.Clear()
        txtMarch.Clear()
        txtApril.Clear()
        txtMay.Clear()
        txtJune.Clear()
        txtJuly.Clear()
        txtAugust.Clear()
        txtSeptember.Clear()
        txtOctober.Clear()
        txtNovember.Clear()
        txtDecember.Clear()
        lblAverage.Text = ""
        lblTotal.Text = ""

    End Sub

    Private Sub btmCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btmCalculate.Click
        Dim TotalRain As Integer
        Dim Avg As Integer








    End Sub
End Class


This post has been edited by xterminal01: 18 September 2010 - 04:15 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Rainfall Statistics calculation using loops

#2 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: Rainfall Statistics calculation using loops

Posted 18 September 2010 - 04:32 PM

You could use a For...Each to iterate through the forms control collection.
Another way would be to create an array and store each textbox value to an element of the array and then iterate through them.

An example would be
Dim TotalRainFall as Decimal
For Each txtBox as Control in Me.Controls 'Iterate through the controls on the form
  if TypeOf txtBox is TextBox Then 'Check if the current control is a textbox and if so...
     TotalRainFall += txtBox.Text 'Add the value to the TotalRainFall variable.
    
  End If
Next


Of course, you should really use .TryParse to ensure that the textbox contains values that are valid.

You can either set another variable to keep track of the count or in the case of your example, divide TotalRainFall by 12 to get the average (since you're using monthly)

Again... just an example but it should get you started.

This post has been edited by CharlieMay: 18 September 2010 - 04:34 PM

Was This Post Helpful? 0
  • +
  • -

#3 xterminal01  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 83
  • Joined: 18-September 10

Re: Rainfall Statistics calculation using loops

Posted 18 September 2010 - 05:44 PM

Thanks for the help,
I tryed it like this and get a zero for some reason as the total

Public Class RainfallStats

    Private Sub btmExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btmExit.Click
        Me.Close()

    End Sub

    Private Sub btmClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btmClear.Click
        txtJanuary.Clear()
        txtFebruary.Clear()
        txtMarch.Clear()
        txtApril.Clear()
        txtMay.Clear()
        txtJune.Clear()
        txtJuly.Clear()
        txtAugust.Clear()
        txtSeptember.Clear()
        txtOctober.Clear()
        txtNovember.Clear()
        txtDecember.Clear()
        lblAverage.Text = ""
        lblTotal.Text = ""

    End Sub

    Private Sub btmCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btmCalculate.Click


        Dim TotalRainFall As Decimal

        For Each txtBox As Control In Me.Controls

            If TypeOf txtBox Is TextBox Then

                TotalRainFall += txtBox.Text



            End If

        Next

        lblTotal.Text = TotalRainFall









    End Sub
End Class




View PostCharlieMay, on 18 September 2010 - 03:32 PM, said:

You could use a For...Each to iterate through the forms control collection.
Another way would be to create an array and store each textbox value to an element of the array and then iterate through them.

An example would be
Dim TotalRainFall as Decimal
For Each txtBox as Control in Me.Controls 'Iterate through the controls on the form
  if TypeOf txtBox is TextBox Then 'Check if the current control is a textbox and if so...
     TotalRainFall += txtBox.Text 'Add the value to the TotalRainFall variable.
    
  End If
Next


Of course, you should really use .TryParse to ensure that the textbox contains values that are valid.

You can either set another variable to keep track of the count or in the case of your example, divide TotalRainFall by 12 to get the average (since you're using monthly)

Again... just an example but it should get you started.

Was This Post Helpful? 0
  • +
  • -

#4 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: Rainfall Statistics calculation using loops

Posted 18 September 2010 - 06:47 PM

What you have should work.

Can you give an example of what you're entering into the textboxes?

Of course, if it wasn't able to convert the value, it would throw an error since we're not checking to ensure it can be calculated.

For example, if you entered 2.5" in the January textbox. The program would crash trying to convert a string to double. But just in case, give me an example of what you're entering.

EDIT:
One thing I just thought of... are the textboxes on the form or are they in a panel or some other control? Because if they're inside a container, we will have to specify that in our For Each statement.

EDIT AGAIN:
Sorry, didn't notice the .bmp at the beginning of your post... You need to include the name of your groupbox in the controls

so say your groupbox is groupbox1 then you would change to

For Each txtBox As Control In Me.Groupbox1.Controls

This post has been edited by CharlieMay: 18 September 2010 - 06:55 PM

Was This Post Helpful? 0
  • +
  • -

#5 xterminal01  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 83
  • Joined: 18-September 10

Re: Rainfall Statistics calculation using loops

Posted 18 September 2010 - 07:38 PM

Thanks it worked now, when I did the groupbox, exactly as expected.
I have the average calculating correctly. The only thing left is non numerical values & if not all the values are entered.
For example if one enters only five months instead of 12 I need a messegebox to show "You need to enter a numeric value for each month" Same thing if a letter rather then a number is entered. After that Clear all the fields textbox'es and labels, so they can re-enter ever thing again. I tryed using an else statement but it got me into a loop.


 If TypeOf txtBox Is TextBox Then


                TotalRainFall += txtBox.Text





            Else
                MessageBox.Show("You need to enter a numeric value in each text box")
                Me.ResetText()










View PostCharlieMay, on 18 September 2010 - 05:47 PM, said:

What you have should work.

Can you give an example of what you're entering into the textboxes?

Of course, if it wasn't able to convert the value, it would throw an error since we're not checking to ensure it can be calculated.

For example, if you entered 2.5" in the January textbox. The program would crash trying to convert a string to double. But just in case, give me an example of what you're entering.

EDIT:
One thing I just thought of... are the textboxes on the form or are they in a panel or some other control? Because if they're inside a container, we will have to specify that in our For Each statement.

EDIT AGAIN:
Sorry, didn't notice the .bmp at the beginning of your post... You need to include the name of your groupbox in the controls

so say your groupbox is groupbox1 then you would change to

For Each txtBox As Control In Me.Groupbox1.Controls

This post has been edited by xterminal01: 18 September 2010 - 07:46 PM

Was This Post Helpful? 0
  • +
  • -

#6 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: Rainfall Statistics calculation using loops

Posted 18 September 2010 - 07:49 PM

Look up TryParse, it is a way to determine if the string is able to be converted to your numeric value.

As far as the average. If it can be parsed to the value, then increment a counter and add to the TotalRainFall variable. This way, you have a divisor for your average.

Basically, you can use the tryparse for nothing more than the boolean comparison.

since you already have a declaration of TotalRainFall as decimal, you could then just do
If Decimal.TryParse(txtBox.Text, TotalRainFall) then
  mycounter +=1 
  TotalRainFall += txtBox.Text
End If


this is inside the for...each block and replaces what you have.

So if the value in the txtbox is able to be converted to a decimal then mycounter is incremented (used later for average)
and the value is added to totalrainfall.

Another thing you could do is use my snippet to keep the user from entering anything other than a number. Look at the SingleDecimal section of my snippet to see if it's something you're interested in.

EDIT:
Actually, You will need to declare a second Decimal to compare for the tryParse

Dim dec as Decimal
then use that in the TryParse
If Decimal.TryParse(txtBox.Text, dec) then

this way you don't affect the TotalRainFall variable from the TryParse result.

This post has been edited by CharlieMay: 18 September 2010 - 07:57 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1