2017-04-26 12 views
1

我的第一個入口在這裏。VBA:通過子例程調用參數的轉發類型

編碼VBA,相當陌生。

============================================== ===================

目標:將座標輸入到對象/類中。

...裝置 「等中的」

現在

解決方案:使用陣列,諸如

===================== ============================================

class1:

sub start() 
    Dim c2 as new Class2 
    Dim points() as Double 
    Redim points(7) 
    point(0) = ... 
    ... 
    point(7) = ... 
    cs.draw points 
end sub 

等級2:

public Sub draw(points() as double) 
    ... 
end sub 

================================================ =================

問題是:硬跟蹤女巫陣列插槽代表一個特定的興趣值。

我想要做的,是一樣的東西:

類1:

Type Properties 
    length As Double 
    keygripp As Double 
    diameter As Double 
    tapdiameter As Double 
    steerlength As Double 
    distance As Double 

    plateau As Double 
End Type 


sub start() 
    Dim c2 as new Class2 
    Dim points as Properties 
    point.length = ... 
    ... 
    point.plateau = ... 
    cs.draw points 
end sub 

等級2:

public Sub draw(p as Properties) '<---- Class 2 also need access to properties? 
    ... 
    doCoolStuff(p.length) 
    doOtherCoolStuff(p.keygripp, p.diameter) 
    ... 
end sub 

我聲明類型屬性的全球莫名其妙,這樣的Class1和Class2中都會知道它是關於什麼的?

問候//馬丁

+2

VB.NET,VBA和VBScript都是不同的。請[編輯]並刪除不相關的標籤。也請看[問]並參加[遊覽]。 – Bugs

+0

不知道你希望我編輯什麼,只有「VBA」和「類型」作爲標籤。 – AutoMartin

+0

它看起來已經[完成](http://stackoverflow.com/posts/43628561/revisions)由Lankymart AutoMartin。 – Bugs

回答

0

據我理解你的問題,你可以創建例如Point類並將其用於集合中,然後將此集合的實例傳遞給Draw方法。 HTH

注意:名稱Point對應於您的問題中名稱Properties

點類模塊

Option Explicit 

Private m_length As Double 

Public Property Get Length() As Double 
    Length = m_length 
End Property 

Public Property Let Length(ByVal vNewValue As Double) 
    m_length = vNewValue 
End Property 

' Tha same as by lenght for the next properties 
' keygripp As Double 
' diameter As Double 
' tapdiameter As Double 
' steerlength As Double 
' distance As Double 
' plateau As Double 

處理器類模塊

Option Explicit 

Public Sub Draw(pts As VBA.Collection) 
    Dim p As Point 
    For Each p In pts 
     ' ... 
     ' doCoolStuff p.Length 
     ' doOtherCoolStuff p.keygripp, p.diameter 
     ' ... 
    Next p 
End Sub 

標準模塊

Sub Start() 
    Dim pt As Point 
    Set pt = New Point 
    pt.Length = 123 
    ' pt.plateau = ... 

    Dim points As VBA.Collection 
    Set points = New VBA.Collection 
    points.Add pt 

    Dim proc As Processor 
    Set proc = New Processor 
    proc.Draw points 
End Sub 

如果你想留下由用戶定義的類型,則在標準模塊聲明的類型,然後用陣列使用它(UDT不能與VBA.Collection使用)。

標準模塊

Type PointType 
    length As Double 
    keygripp As Double 
    diameter As Double 
    tapdiameter As Double 
    steerlength As Double 
    distance As Double 
    plateau As Double 
End Type 

Sub start() 
    Dim pt As PointType 
    pt.length = 555 

    Dim pt2 As PointType 
    pt2.length = 666 

    Dim pts(1 To 2) As PointType 
    pts(1) = pt 
    pts(2) = pt2 

    Dim proc As Processor 
    Set proc = New Processor 
    proc.Draw pts 
End Sub 

類處理器

Public Sub Draw(pts() As PointType) 
' use for-next isnatead of for-each-next here