我的项目中有一个类声明了一些属性(控件)。
除了这些属性外,我还有大约500个奇数的子类,每个子类都填充了它们自己的一组属性。
我知道它疯了,对吧?
我想要实现的是在提供变量时调用主类的新实例。这反过来将通过Select Case
方法仅创建与变量关联的子类,从而有效地避免将500个奇数SubClasses及其默认值加载到内存中。
以下是我目前的结构:
Public Class SysproBoVariables
'Control Variables
Public Property CTRL_ValidFlag As Boolean = False
Public Property CTRL_StockType As String
Public Property CTRL_SetupType As Char
Public Property CustomerQuery As New ARSQRY
Public Property SetupInvMaster As New INVSSTDOC
Public Property SetupInvWarehouse As New INVSWSDOC
Public Property PostJob As New WIPTJBDOC
Public Property PostMaterialAllocations As New WIPTJMDOC
'...Emagine another 495 of these...
End Class
'===========================================================================
Public Class ARSQRY
'CustomerQuery Variables (ARSQRY)
Public Property Customer As String = ""
Public Property IncludeFutures As String = "N"
Public Property IncludeTransactions As String = "N"
'...Plus some more...
End Class
Public Class INVSSTDOC
'SetupInvMaster Variables (INVSSTDOC)
Public Property StockCode As String = ""
Public Property Description As String = ""
'...again add some more...
End Class
'...Continue the above 2 another 498 times...
我知道这是过度杀伤但是我正在构建的集成工具需要
以下是我想要构建的结构(我知道下面的内容是永远不可能的,但纯粹是为了清楚我需要的内容):
Public Class SysproBoVariables
'Control Variables
Public Property CTRL_ValidFlag As Boolean = False
Public Property CTRL_StockType As String
Public Property CTRL_SetupType As Char
Public Sub New(ByRef Optional BoNameRef as string="")
Select Case BoNameRef
Case "ARSQRY"
Public Property CustomerQuery As New ARSQRY '***
Case "INVSSTDOC"
Public Property SetupInvMaster As New INVSSTDOC '***
Case "INVSWSDOC"
Public Property SetupInvWarehouse As New INVSWSDOC '***
'...Continue with this until all Subclasses are covered...
Case Else
'...Declare all Properties (Not Recommended)...
End Select
End Sub
End Class
'....SublClasses goes here....
我知道***区域是不可能的,但这是我希望实现的过程
任何建议或评论家欢迎
答案 0 :(得分:2)
我意识到你已经有了一个你接受的答案,但老实说这种模式似乎很疯狂。一个简单的接口与通用类型相结合将大大清理这个混乱。
首先定义一个接口。不需要任何实现方法,因为它只是一种强制类型约束的工具。
Public Interface IFred ' name is not important
' no implementation needed
End Interface
现在使`SysproBoVariables通用但受约束到接口并用单个属性替换所有数据属性。我还将data属性更改为只读,因为考虑到您的条件,能够分配新实例看起来很奇怪。如果需要,您可以将其更改为读写。
Public Class SysproBoVariables(Of T As {IFred, New, Class})
Public Sub New()
DataProp = New T
End Sub
'Control Variables
Public Property CTRL_ValidFlag As Boolean = False
Public Property CTRL_StockType As String
Public Property CTRL_SetupType As Char
Public ReadOnly DataProp As T
End Class
将界面添加到各种类定义中。由于没有实现方法或属性,这是无痛的。
Public Class ARSQRY : Implements IFred
Public Property Customer As String = ""
Public Property IncludeFutures As String = "N"
Public Property IncludeTransactions As String = "N"
End Class
Public Class INVSSTDOC : Implements IFred
Public Property StockCode As String = ""
Public Property Description As String = ""
End Class
现在使用变得干净并支持智能感知检查。
Sub Example()
Dim sbvARSQRY As New SysproBoVariables(Of ARSQRY)
sbvARSQRY.DataProp.Customer = "Jim"
Dim sbvINVSSTDOC As New SysproBoVariables(Of INVSSTDOC)
sbvINVSSTDOC.DataProp.StockCode = "ABC"
' Intellisense will not allow the next line since INVSSTDOC does not have a Customer prop
' sbvINVSSTDOC.DataProp.Customer = "Jim"
End Sub
编辑:由于您表达了使用接口的不安,这里是一个使用基类来实现相同功能的版本。这只是一个设计选择,我倾向于选择接口。
Public Class Base
' no implementation needed
End Class
Public Class SysproBoVariables(Of T As {Base, New})
Public Sub New()
DataProp = New T
End Sub
'Control Variables
Public Property CTRL_ValidFlag As Boolean = False
Public Property CTRL_StockType As String
Public Property CTRL_SetupType As Char
Public ReadOnly DataProp As T
End Class
Public Class ARSQRY : Inherits Base
Public Property Customer As String = ""
Public Property IncludeFutures As String = "N"
Public Property IncludeTransactions As String = "N"
End Class
Public Class INVSSTDOC : Inherits Base
Public Property StockCode As String = ""
Public Property Description As String = ""
End Class
答案 1 :(得分:1)
您可以保留属性声明它在顶部块中的位置,除非没有New关键字,然后在构造函数中填充这些属性的值:
Public Class SysproBoVariables
'Control Variables
Public Property CTRL_ValidFlag As Boolean = False
Public Property CTRL_StockType As String
Public Property CTRL_SetupType As Char
Public Property CustomerQuery As ARSQRY
Public Property SetupInvMaster As INVSSTDOC
Public Property SetupInvWarehouse As INVSWSDOC
Public Property PostJob As WIPTJBDOC
Public Property PostMaterialAllocations As WIPTJMDOC
Public Sub New(ByRef Optional BoNameRef as string="")
Select Case BoNameRef
Case "ARSQRY"
CustomerQuery = New ARSQRY() '***
Case "INVSSTDOC"
SetupInvMaster = New INVSSTDOC() '***
Case "INVSWSDOC"
SetupInvWarehouse = New INVSWSDOC() '***
'...Continue with this until all Subclasses are covered...
Case Else
'...Declare all Properties (Not Recommended)...
End Select
End Sub
End Class
除非达到Case Else,否则其他属性将无效。如果您在引用时需要填充其他属性,则可以执行此操作:
Private m_CustomerQuery As ARSQRY
Public Property CustomerQuery As ARSQRY
Get
If m_CustomerQuery Is Nothing Then
m_CustomerQuery = New ARSQRY
End If
Return m_CustomerQuery
End Get
Set(value As ARSQRY)
m_CustomerQuery = value
End Set
End Property