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.

Dani AI

Generated

A lot of repeated validation and 12 separate variables can be replaced with a single ordered collection plus one loop. 's For Each is a good quick win, but iterating Me.Controls can pick up unrelated textboxes and the control order is not guaranteed. An explicit array of the month TextBox controls keeps order, lets the code enforce "no skipping" semantics, and uses Double.TryParse for safe numeric validation.

' Example (place in the button click handler)
Dim months() As TextBox = {txtJan, txtFeb, txtMarch, txtApril, txtMay, txtJune, txtJuly, txtAug, txtSept, txtOct, txtNov, txtDec}
Dim sum As Double = 0
Dim countEntered As Integer = 0
Dim seenBlank As Boolean = False

For i As Integer = 0 To months.Length - 1
    Dim tb As TextBox = months(i)
    Dim txt = tb.Text.Trim()
    If txt = String.Empty Then
        seenBlank = True
        Continue For
    End If
    If seenBlank Then
        MessageBox.Show("Fill months in sequence; do not skip a month.")
        tb.Focus()
        Exit Sub
    End If

    Dim val As Double
    If Not Double.TryParse(txt, val) OrElse val < 0 Then
        MessageBox.Show("Enter a non-negative numeric value.")
        tb.Clear()
        tb.Focus()
        Exit Sub
    End If

    sum += val
    countEntered += 1
Next

lblTotal.Text = sum.ToString("F2")
lblAvg.Text = If(countEntered = 0, "0.00", (sum / countEntered).ToString("F2"))

Notes and quick tips: this computes the average over entered months (change the denominator to 12 if the assignment requires average per year). Double.TryParse is preferred over IsNumeric/CDbl. If controls are created at runtime or live inside containers, build the array in Form_Load or set each TextBox.Tag with its month index and sort by Tag. For a cleaner UI, NumericUpDown controls eliminate many validation issues. Regarding pointers: VB.NET does not use C++-style raw pointers in normal managed Windows Forms code; arrays, collections and ByRef parameters are the idiomatic alternatives.

Recommended Answers

All 2 Replies

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.