2012-04-15 72 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