2015-10-28 63 views
0

我有一些XML值,我想将它分配给一个类,我想知道是否可以遍历类的某些元素(而不是创建类中的数组)。VBA - 为某个类的某些元素赋值

所以参照下面的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 and 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> 
+0

如果这就是你想要做的,那么你可以做到这一点。目前尚不清楚究竟是什么阻止了你? –

+0

这是我的问题 - 我想知道如何遍历类的某些元素,其中的元素不是特定的数组 – brebbles

+0

这通常是为什么这种类型的任务会使用数组:这样做会让你的生活变得更加困难。在你的情况下,答案可能是使用'CallByName' –

回答

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(无需for getter/setter)

+0

非常感谢,这正是我所追求的。我注意到,当遵循get/set方法时,“v”数组会创建重复条目,因此它会创建mT1/2/3以及Total1/2/3,但这应该很容易处理。 – brebbles

+0

mT1等是用于支持公共属性Total1等的私有变量,这通常是您实现属性的方式 - 需要在某个地方将值存储在类中。 –