whenever I tried to get the answer from the Validatescores() class it always display 0 as an answer. I can't calculate in the same class as it is part of my assignment.
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim Student As New myStudent
Student.ValidateScores(TextBox1.Text, TextBox2.Text, TextBox3.Text)
Label8.Text = Student.getAverage()
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
TextBox1.ResetText()
TextBox2.ResetText()
TextBox3.ResetText()
TextBox4.ResetText()
Label8.ResetText()
End Sub
End Class
Public Class myStudent
Public Name As String
Public Average As Double
Public Sub ValidateScores(ByVal No1 As Integer, ByVal No2 As Integer, ByVal No3 As Integer)
If (No1 >= 0 And No1 <= 100) And (No2 >= 0 And No2 <= 100) And (No3 >= 0 And No3 <= 100) Then
Average = (No1 + No2 + No3) / 3
Average = getAverage()
Else
MsgBox("Please enter the correct value from 0 to 100")
End If
End Sub
Function getAverage() As Decimal
Return getAverage
End Function
End Class
textbox1,2 and 3 are for entering the average number. label8 is for displaying the average total. textbox4 is for input name.
You will save yourself a lot of headaches if you set Option Strict On in project properties and in Options (VB Defaults) for future projects.
Don't do your validation in the myStudent
class. You want to show messages boxes in the User Interface code, not the myStudent
class. This same class could be used with a User Interface that doesn't recognize message boxes. Validation of user input has nothing to do with the student. Only properties and methods directly related to the student should be in the myStudent class.
I made your fields properties. I added a Sub New so you can't set all the number properties of the class when a student is created.
Before you can find out if the input is in range you need to find out if it is really a number. The .TryParse
methods will do this. https://docs.microsoft.com/en-us/dotnet/api/system.decimal.tryparse?view=net-5.0
I know you know how to calculate an average. Add them up and divide by the count of the numbers.
We only actually create an instance of the myStudent
class after the validation.
Public Class myStudent
Public Property Name As String
Public Property Average As Decimal
Public Property Test1 As Decimal
Public Property Test2 As Decimal
Public Property Test3 As Double
Public Sub New(t1 As Decimal, t2 As Decimal, t3 As Decimal)
Test1 = t1
Test2 = t2
Test3 = t3
End Sub
Function getAverage() As Decimal
Dim Avg = (Test1 + Test2 + Test3) / 3
Return CDec(Avg)
End Function
End Class
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim Student As myStudent = Nothing
Dim t1, t2, t3 As Decimal
If Decimal.TryParse(TextBox1.Text, t1) OrElse Decimal.TryParse(TextBox2.Text, t2) OrElse Decimal.TryParse(TextBox3.Text, t3) Then
If t1 >= 0 AndAlso t1 <= 100 AndAlso t2 >= 0 AndAlso t2 <= 100 AndAlso t3 >= 0 AndAlso t3 <= 100 Then
Student = New myStudent(t1, t2, t3)
Else
MessageBox.Show("Please enter the correct value from 0 to 100")
End If
Else
MessageBox.Show("Please enter the correct value from 0 to 100")
End If
Label8.Text = Student.getAverage().ToString("##0.00")
End Sub
You would do yourself a favor if you gave your controls meaningful names.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.