VBA - 为 class 的某些元素赋值
VBA - Assigning values to certain elements of a class
我有一些 XML 的值我想分配给 class,我想知道是否可以循环遍历 class 的某些元素(而不是在 class).
中创建数组
因此,参考下面的 XML 片段,我想将 class 设置为从 XML 传递值,如下所示:
event.homeTeamName
event.awayTeamName
event.homeSpread1
event.homeSpread2
event.homeSpread3
event.homeSpread4
event.homeSpread5
event.totalPoints1
event.totalPoints2
event.totalPoints3
event.totalPoints4
event.totalPoints5
相对于
event.homeTeamName
event.awayTeamName
event.homeSpread(1)
event.homeSpread(2)
event.homeSpread(3)
event.homeSpread(4)
event.homeSpread(5)
event.totalPoints(1)
event.totalPoints(2)
event.totalPoints(3)
event.totalPoints(4)
event.totalPoints(5)
有没有办法遍历 homeSpread1 - homeSpread5 和 totalPoints1 - totalPoints5 个元素的 class 当从 XML 中赋值时?我知道 属性 Get 和 属性 Let 模块中的 class 功能,但是据我所知,这会导致不希望的 class 涉及数组。据我所知,无论如何我都需要为每个数组创建一个 属性 Let/Get。
下面是 XML 片段的示例:
<homeTeam type="Team1">
<name>Brisbane Roar</name>
<rotNum>2151</rotNum>
</homeTeam>
<awayTeam type="Team2">
<name>Adelaide United</name>
<rotNum>2152</rotNum>
</awayTeam>
<periods>
<period lineId="234921091">
<spreads>
<spread>
<awaySpread>0.25</awaySpread>
<awayPrice>2.01</awayPrice>
<homeSpread>-0.25</homeSpread>
<homePrice>1.909</homePrice>
</spread>
<spread altLineId="1893988627">
<awaySpread>0.75</awaySpread>
<awayPrice>1.549</awayPrice>
<homeSpread>-0.75</homeSpread>
<homePrice>2.59</homePrice>
</spread>
<spread altLineId="1893988629">
<awaySpread>0.5</awaySpread>
<awayPrice>1.751</awayPrice>
<homeSpread>-0.5</homeSpread>
<homePrice>2.21</homePrice>
</spread>
<spread altLineId="1893988631">
<awaySpread>0</awaySpread>
<awayPrice>2.47</awayPrice>
<homeSpread>0</homeSpread>
<homePrice>1.598</homePrice>
</spread>
<spread altLineId="1893988633">
<awaySpread>-0.25</awaySpread>
<awayPrice>2.91</awayPrice>
<homeSpread>0.25</homeSpread>
<homePrice>1.444</homePrice>
</spread>
</spreads>
<totals>
<total>
<points>2.75</points>
<overPrice>2.02</overPrice>
<underPrice>1.884</underPrice>
</total>
<total altLineId="1893988628">
<points>2.25</points>
<overPrice>1.571</overPrice>
<underPrice>2.49</underPrice>
</total>
<total altLineId="1893988630">
<points>2.5</points>
<overPrice>1.793</overPrice>
<underPrice>2.12</underPrice>
</total>
<total altLineId="1893988632">
<points>3</points>
<overPrice>2.36</overPrice>
<underPrice>1.632</underPrice>
</total>
<total altLineId="1893988634">
<points>3.25</points>
<overPrice>2.69</overPrice>
<underPrice>1.49</underPrice>
</total>
</totals>
</period>
</periods>
示例:
Sub Tester()
Dim t As New clsTest
Dim i As Long
For i = 1 To 3
CallByName t, "Total" & i, VbLet, i * 10
Next i
Debug.Print t.Total1, t.Total2, t.Total3 '--> 10, 20, 30
End Sub
clsTest:
Option Explicit
Private mT1 As Double
Private mT2 As Double
Private mT3 As Double
Property Let Total1(v As Double)
mT1 = v
End Property
Property Get Total1() As Double
Total1 = mT1
End Property
Property Let Total2(v As Double)
mT2 = v
End Property
Property Get Total2() As Double
Total2 = mT2
End Property
Property Let Total3(v As Double)
mT3 = v
End Property
Property Get Total3() As Double
Total3 = mT3
End Property
您也可以在 clsTest
中简单地使用 Public 个变量(不需要 getter/setter)
我有一些 XML 的值我想分配给 class,我想知道是否可以循环遍历 class 的某些元素(而不是在 class).
中创建数组因此,参考下面的 XML 片段,我想将 class 设置为从 XML 传递值,如下所示:
event.homeTeamName
event.awayTeamName
event.homeSpread1
event.homeSpread2
event.homeSpread3
event.homeSpread4
event.homeSpread5
event.totalPoints1
event.totalPoints2
event.totalPoints3
event.totalPoints4
event.totalPoints5
相对于
event.homeTeamName
event.awayTeamName
event.homeSpread(1)
event.homeSpread(2)
event.homeSpread(3)
event.homeSpread(4)
event.homeSpread(5)
event.totalPoints(1)
event.totalPoints(2)
event.totalPoints(3)
event.totalPoints(4)
event.totalPoints(5)
有没有办法遍历 homeSpread1 - homeSpread5 和 totalPoints1 - totalPoints5 个元素的 class 当从 XML 中赋值时?我知道 属性 Get 和 属性 Let 模块中的 class 功能,但是据我所知,这会导致不希望的 class 涉及数组。据我所知,无论如何我都需要为每个数组创建一个 属性 Let/Get。
下面是 XML 片段的示例:
<homeTeam type="Team1">
<name>Brisbane Roar</name>
<rotNum>2151</rotNum>
</homeTeam>
<awayTeam type="Team2">
<name>Adelaide United</name>
<rotNum>2152</rotNum>
</awayTeam>
<periods>
<period lineId="234921091">
<spreads>
<spread>
<awaySpread>0.25</awaySpread>
<awayPrice>2.01</awayPrice>
<homeSpread>-0.25</homeSpread>
<homePrice>1.909</homePrice>
</spread>
<spread altLineId="1893988627">
<awaySpread>0.75</awaySpread>
<awayPrice>1.549</awayPrice>
<homeSpread>-0.75</homeSpread>
<homePrice>2.59</homePrice>
</spread>
<spread altLineId="1893988629">
<awaySpread>0.5</awaySpread>
<awayPrice>1.751</awayPrice>
<homeSpread>-0.5</homeSpread>
<homePrice>2.21</homePrice>
</spread>
<spread altLineId="1893988631">
<awaySpread>0</awaySpread>
<awayPrice>2.47</awayPrice>
<homeSpread>0</homeSpread>
<homePrice>1.598</homePrice>
</spread>
<spread altLineId="1893988633">
<awaySpread>-0.25</awaySpread>
<awayPrice>2.91</awayPrice>
<homeSpread>0.25</homeSpread>
<homePrice>1.444</homePrice>
</spread>
</spreads>
<totals>
<total>
<points>2.75</points>
<overPrice>2.02</overPrice>
<underPrice>1.884</underPrice>
</total>
<total altLineId="1893988628">
<points>2.25</points>
<overPrice>1.571</overPrice>
<underPrice>2.49</underPrice>
</total>
<total altLineId="1893988630">
<points>2.5</points>
<overPrice>1.793</overPrice>
<underPrice>2.12</underPrice>
</total>
<total altLineId="1893988632">
<points>3</points>
<overPrice>2.36</overPrice>
<underPrice>1.632</underPrice>
</total>
<total altLineId="1893988634">
<points>3.25</points>
<overPrice>2.69</overPrice>
<underPrice>1.49</underPrice>
</total>
</totals>
</period>
</periods>
示例:
Sub Tester()
Dim t As New clsTest
Dim i As Long
For i = 1 To 3
CallByName t, "Total" & i, VbLet, i * 10
Next i
Debug.Print t.Total1, t.Total2, t.Total3 '--> 10, 20, 30
End Sub
clsTest:
Option Explicit
Private mT1 As Double
Private mT2 As Double
Private mT3 As Double
Property Let Total1(v As Double)
mT1 = v
End Property
Property Get Total1() As Double
Total1 = mT1
End Property
Property Let Total2(v As Double)
mT2 = v
End Property
Property Get Total2() As Double
Total2 = mT2
End Property
Property Let Total3(v As Double)
mT3 = v
End Property
Property Get Total3() As Double
Total3 = mT3
End Property
您也可以在 clsTest
中简单地使用 Public 个变量(不需要 getter/setter)