VB.net和接口 [英] VB.net and Interfaces
问题描述
我需要使我的MasterDetail对象更加灵活.
实际上,我的MasterDetail对象是一个由3个子对象组成的UserDefineObject:
1)包含``MasterRows''的DataGridView
2)包含''DetailRows''的DataGridView
3)TreeView包含主选定对象及其所有子级"
现在,每个对象都有自己的代码,这些代码可以完美地用于特定种类的类,
但是我需要使其与许多类一起使用.
我开始在MasterDetail中创建一个Interface,然后在需要使用MasterDetail Object管理的所有类中实现Interface.
界面
Hi,
I need to make my MasterDetail Object more flexible.
Actualy my MasterDetail Object is a UserDefineObject composed by 3 subObjects:
1) DataGridView containing ''MasterRows''
2) DataGridView containing ''DetailRows''
3) TreeView containing the master selected object with all his own ''childs''
Each object now have it own code that works perfectly for a specific kind of class,
but I need to make it work with many classes.
I start creating an Interface in the MasterDetail and then I implement the Interface in all the classes I need to manage with MasterDetail Object.
INTERFACE
Interface iMasterDetail
Property TableMaster As String
Property TableDetail As String
Property SQLstring As String
Property DetailVisible As Boolean
Property TreeVisible As Boolean
Function ToTreeView(ByVal trw As TreeView) As Boolean
End Interface
发票
INVOICE
Public Class cInvoice
Implements iMasterDetail
#Region "PROPERTIES"
Private _DetailVisible As Boolean = False
Public Property DetailVisible As Boolean Implements iMasterDetail.DetailVisible
Get
Return _DetailVisible
End Get
Set(ByVal value As Boolean)
_DetailVisible = value
End Set
End Property
Private _SQLstring As String = String.Empty
Public Property SQLstring As String Implements iMasterDetail.SQLstring
Get
Return _SQLstring
End Get
Set(ByVal value As String)
_SQLstring = value
End Set
End Property
Private _TableDetail As String = String.Empty
Public Property TableDetail As String Implements iMasterDetail.TableDetail
Get
Return _TableDetail
End Get
Set(ByVal value As String)
_TableDetail = value
End Set
End Property
Private _TableMaster As String = String.Empty
Public Property TableMaster As String Implements iMasterDetail.TableMaster
Get
Return _TableMaster
End Get
Set(ByVal value As String)
_TableMaster = value
End Set
End Property
Public Function ToTreeView(ByVal trw As System.Windows.Forms.TreeView) As Boolean Implements iMasterDetail.ToTreeView
'...not yet implemented
Return True
End Function
Private _TreeVisible As Boolean = False
Public Property TreeVisible As Boolean Implements iMasterDetail.TreeVisible
Get
Return _TreeVisible
End Get
Set(ByVal value As Boolean)
_TreeVisible = value
End Set
End Property
#End Region
End Class
在MasterDetail对象中,我创建一个表示MASTER对象的属性:
In the MasterDetail Object I create a property that rappresents the MASTER object:
Private _Master As Object = Nothing
<Description("...")> <Category("CeR")> _
Public Property Master As Object
Get
Return _Master
End Get
Set(ByVal value As Object)
_Master = value
If TypeOf _Master Is iMasterDetail Then
''MsgBox("YES!")
End If
End Set
End Property
我的第一个问题是如何使它适合"外部链接的对象(在这种情况下为发票")以及如何使其实现iMasterDetail接口.
这样,我将能够使用IntelliSense中所有已实现的属性,函数等.
预先感谢您的帮助
并请让我知道我是否完全错误
My first problem is how to make it to ''FIT'' the external linked object (in that case the Invoice) and how to make it to implement iMasterDetail Interface.
In that way I''ll be able to use all the implemented Properties, Functions, etc by IntelliSense
thanks in advance for the help
and please let me know if I''m completely wrong
推荐答案
您提供的代码似乎有效.如果问题是当您在代码中使用_Master
变量时,您将无法看到接口的属性,这是因为将其存储为对象并且仅看到了这些属性. br/>
解决此问题的一种方法是使用强制转换.例如,尝试更改代码,例如:
The code you provided seems valid. If the problem is that when you use the_Master
variable in your code, you''re not able to see the properties of the interface, it''s because you store it as an object and you see only those properties.
One way to handle this is using casting. For example, try changing your code like:
Private _Master As Object = Nothing
Public Property Master As Object
Get
Return _Master
End Get
Set(ByVal value As Object)
_Master = value
If TypeOf _Master Is iMasterDetail Then
MsgBox(DirectCast(_Master, iMasterDetail).SQLstring)
End If
End Set
End Property
另一种方法是更改变量的类型:
Another way could be that you change the type of the variable:
Private _Master2 As iMasterDetail = Nothing
Public Property Master2 As iMasterDetail
Get
Return _Master2
End Get
Set(ByVal value As iMasterDetail)
_Master2 = value
MsgBox(_Master2.SQLstring)
End Set
End Property
Mika,
非常感谢您提供解决方案.
我现在不可以将未定义的对象定义为接口:
``Property Master2作为iMasterDetail''
这是我第一次使用Interfaces.
再见
Hi Mika,
thanks a lot for the solution.
I didn''t now I could define a undefined object as an interface:
''Property Master2 As iMasterDetail''
It was my first time with Interfaces.
bye
这篇关于VB.net和接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!