VBA - 为类的某些元素赋值

时间:2015-10-28 03:22:08

标签: xml vba class properties module

我有一些带有我想要分配给类的值的XML,我想知道是否可以循环遍历类的某些元素(而不是在类中创建数组)。

因此,参考下面的XML片段,我希望将类设置为从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 类的元素从XML分配值时?我知道类模块中的属性获取属性允许功能,但据我所知,这将导致涉及数组的不需要的类。另外,据我所知,无论如何我都需要为每个阵列创建一个 Property 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>

1 个答案:

答案 0 :(得分:1)

示例:

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中使用公共变量(不需要getter / setter)