2015-10-28 99 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等的私有變量,這通常是您實現屬性的方式 - 需要在某個地方將值存儲在類中。 –