Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27514

assistance reducing amount of code

$
0
0
I'm working an assignment to help with learning creating, saving, reading data from text files. It's also covering setting up Structures too. The book (an intro to VB) gives the form design. My assignment is not completed yet. The text button is more to make sure input validation and computation were working. I'm likely to break some of this up into MenuStrip items.

Name:  9404881369_4a2b17f8eb_o.png
Views: 4
Size:  10.4 KB

But what I'm seeking some advise or assistance is that I have close to five hundred lines of code just all the input validation and error message popup window for the sixty six TextBoxes.

There is a structure setup:
Code:

    Structure StudentData
        Dim strStudentName As String
        Dim intTestScore() As Integer
        Dim decAverageScore As Decimal
    End Structure

My input validation:
Code:

        Dim strError1 As String = "must be a numeric value"                'error for non-numeric input
        Dim strError2 As String = "must be a value between 0 and 100"      'error for scores outside of 0 to 100
        Dim strErrorMessage As String = String.Empty                        'reset error message variable holder
        Dim blnError As Boolean = False                                    'reset error message trigger to false

'STUDENT 1 INFORMATION
        'name validate
        If txtStudent1.Text <> String.Empty Then
            If Not IsNumeric(txtStudent1.Text) Then
                studentInfo(0).strStudentName = txtStudent1.Text
            Else
                strErrorMessage += ControlChars.CrLf & "Student 1 name cannot be a number"
                studentInfo(0).strStudentName = "Student 1"
                blnError = True
            End If
        Else
            strErrorMessage += ControlChars.CrLf & "Student 1 name cannot be a number"
            studentInfo(0).strStudentName = "Student 1"
            blnError = True
        End If

        'score 1 validate
        If Integer.TryParse(txtStudent1Score1.Text, studentInfo(0).intTestScore(0)) Then
            If studentInfo(0).intTestScore(0) > -1 AndAlso studentInfo(0).intTestScore(0) < 101 Then
            Else
                strErrorMessage += ControlChars.CrLf & studentInfo(0).strStudentName & "'s test score 1 " & strError2
                blnError = True
            End If
        Else
            strErrorMessage += ControlChars.CrLf & studentInfo(0).strStudentName & "'s test score 1 " & strError1
            blnError = True
        End If
 [code remove for scores 2, 3, 4, and 5]

There are five scores, and this repeats each test score TextBox. I assuming there is a way to do this will significantly less code. I made the error message list every error for any and all textboxes into one MessageBox dialog, rather than having the user cycle through multiple popup messages. And yes, I just noticed I have the number error message for an empty TextBox for the name entry.

I attached a zip of the application, thus far completed, in case it is easier to see and advise. Thank you in advance.

Rich
Attached Images
 
Attached Files

Viewing all articles
Browse latest Browse all 27514

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>