Vb.net Property使用另一个属性

时间:2017-04-03 11:13:38

标签: vb.net oop properties

这是我的计划:

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

所以问题是:

  1. 在我这样做的另一个属性中使用属性是否正确?如果不是,我该如何做同样的功能呢?
  2. 如果在Molesum类中提及变量Component,则它不起作用。这有什么问题?

1 个答案:

答案 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