这是我的计划:
Public Class Form2
Public Class Component
Public Shared Methane, Ethane As New Component
Public Shared ComponentList As New List(Of Component)(New Component( {Methane, Ethane})
Public Property Mole As Double
Public Shared MoleSum = ComponentList.Sum(Function(item) item.Mole)
Public Property NMole As Double = Mole/Molesum
End Class
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
Double.TryParse(TextBox1.Text, Component.Methane.Mole)
End Sub
Private Sub TextBox2_TextChanged(sender As Object, e As EventArgs) Handles TextBox2.TextChanged
Double.TryParse(TextBox2.Text, Component.Ethane.Mole)
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
MsgBox(Component.Methane.NMole)
End Sub
End Class
所以问题是:
Molesum
类中提及变量Component
,则它不起作用。这有什么问题?答案 0 :(得分:0)
对于遇到相同问题的人,请在下方解决:
Public Class Form2
Public Class Component
Public Shared Methane, Ethane As New Component
Public Shared ComponentList As New List(Of Component)(New Component( {Methane, Ethane})
Public Property Mole As Double
Public Shared Function MoleSum() As Double
Return Component.ComponentList.Sum(Function(item) item.Mole)
End Function
Public ReadOnly Property Nmole As Double
Get
Return MoleSum()
End Get
End Property
End Class
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
Double.TryParse(TextBox1.Text, Component.Methane.Mole)
End Sub
Private Sub TextBox2_TextChanged(sender As Object, e As EventArgs) Handles TextBox2.TextChanged
Double.TryParse(TextBox2.Text, Component.Ethane.Mole)
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
MsgBox(Component.Methane.NMole)
End Sub
End Class