/ / mylony przez kod błędu w VB.Net - vb.net

zdezorientowany kodem błędu w VB.Net - vb.net

Mój kod generuje błąd „Błąd typu” Average.Module1.student ”nie można przekonwertować na„ 1-wymiarową tablicę Average.Module1.student. ”W linii 15. Co przeoczam?

Moduł Moduł 1 System importu

Class student
Public id As Integer
Public name As String
Public M1 As Integer
Public M2 As Integer
Public objStudent As String
Public Function average() As Double
Return (M1 + M2) / 2
End Function
End Class
Class Program
Private Shared Sub Main(args As String())
Dim objstudent() As student = New student()

Dim s As student
For i As Integer = 0 To 14
s = New student()
Console.WriteLine("Enter the Student id")
s.id = Convert.ToInt32(Console.ReadLine())
Console.WriteLine("Enter the Student Name")
s.name = Console.ReadLine()
Console.WriteLine("Enter the first mark")
s.M1 = Convert.ToInt32(Console.ReadLine())
Console.WriteLine("Enter the second mark")
s.M2 = Convert.ToInt32(Console.ReadLine())
objstudent(i) = s
Next
For Each ss As student In objstudent
Dim a As Double = ss.average()
If a >= 90 Then
Console.WriteLine(ss.name + "Your grade is A")
ElseIf (a >= 80) AndAlso (a < 90) Then
Console.WriteLine(ss.name + "Your grade is B")
ElseIf (a >= 70) AndAlso (a < 80) Then
Console.WriteLine(ss.name + "Your grade is C")
ElseIf (a >= 60) AndAlso (a < 70) Then
Console.WriteLine(ss.name + "Your grade is D")
Else
Console.WriteLine(ss.name + "Sorry, You failed")
End If

Next

End Sub
End Class

Moduł końcowy

Odpowiedzi:

0 dla odpowiedzi № 1
Dim objstudent() As student = New student()

To nie jest poprawna deklaracja tablicowa. Deklarujesz, że chcesz tablicę studentów, ale używasz konstruktora Student. Więc naprawdę mówisz:

new array = new object

Oto poprawna deklaracja. Zauważ, że tablice są oparte na 0. Więc:

Dim objstudent As student() = New student(14) {}

to tablica 15 studentów.

Class student
Public id As Integer
Public name As String
Public M1 As Integer
Public M2 As Integer
Public objStudent As String
Public Function average() As Double
Return (M1 + M2) / 2
End Function
End Class
Class Program
Private Shared Sub Main(args As String())
Dim objstudent As student() = New student(14) {}

Dim s As student
For i As Integer = 0 To 14
s = New student()
Console.WriteLine("Enter the Student id")
s.id = Convert.ToInt32(Console.ReadLine())
Console.WriteLine("Enter the Student Name")
s.name = Console.ReadLine()
Console.WriteLine("Enter the first mark")
s.M1 = Convert.ToInt32(Console.ReadLine())
Console.WriteLine("Enter the second mark")
s.M2 = Convert.ToInt32(Console.ReadLine())
objstudent(i) = s
Next
For Each ss As student In objstudent
Dim a As Double = ss.average()
If a >= 90 Then
Console.WriteLine(ss.name + "Your grade is A")
ElseIf (a >= 80) AndAlso (a < 90) Then
Console.WriteLine(ss.name + "Your grade is B")
ElseIf (a >= 70) AndAlso (a < 80) Then
Console.WriteLine(ss.name + "Your grade is C")
ElseIf (a >= 60) AndAlso (a < 70) Then
Console.WriteLine(ss.name + "Your grade is D")
Else
Console.WriteLine(ss.name + "Sorry, You failed")
End If

Next

End Sub
End Class