2012-04-15 76 views
1

遠遠沒有,這個舊的vb6應用程序正在殺死我。我在.NET之前是如何開發這些東西的。vb6類屬性作爲一個類的數組

我想創建一個屬性成員是UDT或其他類的數組的vb6類。

例如

我有一個名爲監視器類暴露出一些屬性:

  • 分辨率
  • 旋轉
  • 名稱
  • 寬度
  • 身高

在我的主程序mo愚蠢我有一個名爲SystemConfig的類,它有一個名爲MonitorConfig的屬性,但以前它只提供了一個項目。因爲我們現在在多個監視器的世界中運行,所以我需要此屬性來支持多個項目。

不幸的是vb6不給我List(Of T),所以我需要下一個最好的東西。我的第一個想法是使用一個數組。

這裏是我的嘗試:

Private m_MonitorConfig() As Monitor 

Public Property Get MonitorConfig() As Monitor() 
    MonitorConfig = m_MonitorConfig 
End Property 
Public Property Let MonitorConfig(val() As Monitor) 
    m_MonitorConfig = val 
End Property 

如何獲得的財產,以識別進出MonitorConfig財產的數組值?

感謝

+0

與「現在」是15年前,上千年:) – Deanna 2012-04-16 15:24:35

回答

3

你的代碼是好的,但它不是很高效。如果你需要對顯示器進行只讀訪問,但不想實現一個完整的集合,那麼簡單的訪問器屬性和計數屬性就足夠了。

事情是這樣的:

Option Explicit 

Private Declare Function EmptyMonitorsArray Lib "oleaut32" Alias "SafeArrayCreateVector" (Optional ByVal vt As VbVarType = vbObject, Optional ByVal lLow As Long = 0, Optional ByVal lCount As Long = 0) As Monitor() 

Private m_MonitorConfig() As Monitor 

Property Get MonitorConfig(ByVal Index As Long) As Monitor 
    Set MonitorConfig = m_MonitorConfig(Index) 
End Property 

Property Get MonitorConfigs() As Long 
    MonitorConfigs = UBound(m_MonitorConfig) + 1 
End Property 

Private Sub Class_Initialize() 
    m_MonitorConfig = EmptyMonitorsArray 
End Sub 
+1

也看看這個問題:http://stackoverflow.com/questions/9243461/vb6-and-net-array-差異/ 9243917#9243917 – Dabblernl 2012-04-15 19:25:01

+0

@Dabblernl感謝您的鏈接。大。 – Ben 2012-04-17 11:05:13

3

要麼改變屬性接受一個「指數」的說法,所以你可以把它當作「數組像」語法,或者考慮使用一個集合,而不是一個數組。

+0

+1「因爲我們現在在多臺顯示器的世界運營」。集合應該是陣列恕我直言的首選。財產與「索引」arg值得考慮,但我發現自己使用收藏9 10倍。 – MarkJ 2012-04-17 08:00:24