hi,
I need some help to store each textbox value into each variables.
This is my class assignment.

Q.Create an applicaiton that allows the user to enter each month's amount of rainfall and calculates the total and average rainfall for a year. (user doesn't have to input all the data, but cannot skip the month) Input validation: nonnumeric value and value less than zero.

This is what I write.

Dim dblJan, dblFeb, dblMarch, dblApril, dblMay, dblJune As Double
Dim dblJuly, dblAug, dblSept, dblOct, dblNov, dblDec As Double

        If Not IsNumeric(txtJan.Text) Then
            MessageBox.Show("Please enter numerical value")
            txtJan.Clear()
            txtJan.Focus()
            Return
        End If
        dblJan = CDbl(txtJan.Text)
        If dblJan < 0 Then
            MessageBox.Show("Please enter positive value")
            txtJan.Clear()
            txtJan.Focus()
            Return
        End If

        If (txtFeb.Text = Nothing) Then
            lblTotal.Text = dblJan.ToString
            lblAvg.Text = dblJan.ToString
            Return
        ElseIf Not IsNumeric(txtFeb.Text) Then
            MessageBox.Show("Please enter numerical value")
            txtFeb.Clear()
            txtFeb.Focus()
            Return
        End If
        dblFeb = CDbl(txtFeb.Text)
        If dblFeb < 0 Then
            MessageBox.Show("Please enter positive value")
            txtFeb.Clear()
            txtFeb.Focus()
            Return
        End If

The green part above repeated over and over again with different textbox and variable names until to store the txtDec.text value into the dblDec.
And at the end,

lblTotal.Text = (dblJan + dblFeb + dblMarch + dblApril + dblMay + dblJune + dblJuly + dblAug + dblSept + dblOct + dblNov + dblDec).ToString
 lblAvg.Text = ((dblJan + dblFeb + dblMarch + dblApril + dblMay + dblJune + dblJuly + dblAug + dblSept + dblOct + dblNov + dblDec) / 12).ToString

This worked....
However, it is too much writing (just repeating same code over and over again using different textbox name and variables....)

Is there any way to rewrite this code using a loop?
Does vb.net have something similer to the pointers used in C++?

thank you.

hi,
Is there any way to rewrite this code using a loop?

Does vb.net have something similer to the pointers used in C++?
thank you.

Try this for reducing code in your example.
I don't know C++ so I cannot answer your question about pointer.

Dim nTotal As Double
For Each oTextBox As Object In Me.Controls
    If TypeOf (oTextBox) Is TextBox Then
        If Not IsNumeric(oTextBox.Text) Then
            MessageBox.Show("Please enter numerical value")
            oTextBox.clear()
            oTextBox.Focus()
            Exit Sub
        End If
        nTotal = nTotal + oTextBox.Text
        lblTotal.Text =nTotal.ToString
    End If
Next

Thank you samir_ibrahim!
I'll try.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.