在VBA类

时间:2016-01-19 22:45:24

标签: arrays vba class

我有几个类包含有关现场总机的详细信息。我使用Userform将数据存储在每个类中声明的变量中。我正在努力将数据存储在我的任何数组中。我是否正确地宣布这些变量?这是我的一个课程的一个例子:

Dim pEquipID(3) As String
Dim pIr(3) As Integer
Dim pIm(3) As Integer
Dim pTripCurrent(3) As Integer
Dim pTripTime(3) As Integer
Dim pIsc(3) As Integer

Public Property Let EquipID(index As Integer, value As String)
    pEquipID(index) = value
End Property

Public Property Let Ir(index As Integer, value As Integer)
    pIr(index) = value
End Property

Public Property Let Im(index As Integer, value As Integer)
    pIm(index) = value
End Property

Public Property Let TripCurrent(index As Integer, value As Integer)
    pTripCurrent(index) = value
End Property

Public Property Let TripTime(index As Integer, value As Integer)
    pTripTime(index) = value
End Property

Public Property Let Isc(index As Integer, value As Integer)
    pIsc(index) = value
End Property

Public Property Get EquipID(index As Integer) As String
    EquipID(index) = pEquipID(index)
End Property

Public Property Get Ir(index As Integer) As Integer
    Ir(index) = pIr(index)
End Property

Public Property Get Im(index As Integer) As Integer
    Im(index) = pIm(index)
End Property

Public Property Get TripCurrent(index As Integer) As Integer
    TripCurrent(index) = pTripCurrent(index)
End Property

Public Property Get TripTime(index As Integer) As Integer
    TripTime(index) = pTripTime(index)
End Property

Public Property Get Isc(index As Integer) As Integer
    Isc(index) = pIsc(index)
End Property

这就是我存储数据的方式:

Private Sub Enter1_Click()
    'create class variables
    Dim Transformer1 As cTransformer
    Set Transformer1 = New cTransformer
    Dim Fuse1 As cFuse
    Set Fuse1 = New cFuse
    Dim CircuitBreaker1 As cCircuitBreaker
    Set CircuitBreaker1 = New cCircuitBreaker
    Dim Pump1 As cPump
    Set Pump1 = New cPump
    Dim Cable1 As cCable
    Set Cable1 = New cCable
'store circuit breaker entries
    CircuitBreaker1.EquipID(0) = CB1ID1.value
    CircuitBreaker1.EquipID(1) = CB2ID1.value
    CircuitBreaker1.Ir(0) = Cb1Ir1.value
    CircuitBreaker1.Ir(1) = CB2Ir1.value
    CircuitBreaker1.Im(0) = CB1Im1.value
    CircuitBreaker1.Im(1) = CB2Im1.value
    CircuitBreaker1.Isc(0) = CB1Isc1.value
    CircuitBreaker1.Isc(1) = CB2Isc1.value
    CircuitBreaker1.TripCurrent(0) = CB1trip1.value
    CircuitBreaker1.TripCurrent(1) = CB2trip1.value
    CircuitBreaker1.TripTime(0) = CB1time1.value
    CircuitBreaker1.TripTime(1) = CB2time1.value

当我调试Circuit.print CircuitBreaker数组时,终端打印空白值。我使用这样的循环来打印每个数组:

        Dim count As integer
        For count = 0 To 1 Step 1
        Debug.Print CircuitBreaker1.EquipID(count)
        Next count

请注意,此print语句位于Enter1_click()

1 个答案:

答案 0 :(得分:1)

您的财产获取不正确。而不是:

Public Property Get EquipID(index As Integer) As String
    EquipID(index) = pEquipID(index)
End Property

......应该是:

Public Property Get EquipID(index As Integer) As String
    EquipID = pEquipID(index)
End Property