组织VB.Net Mehods

时间:2012-06-13 02:38:07

标签: vb.net organization

假设我有一个包含多种方法的类。我想将方法​​组织成分组,每次都可以访问而无需构建新对象。目的是将类的方法分组为逻辑桶

例如:

Dim myclass as MyCustomClass
myclass.Shipping.Get_List()
myclass.Production.Get_List()

最好的方法是什么?我尝试了嵌套类,但VB.NET不允许我访问上面显示的方法。

3 个答案:

答案 0 :(得分:0)

所以这就是我想做你想做的事情

这不是世界上最好的设计,但它会起作用

我建议你将实际的get_list和其他类型的方法/属性移动到特定的类中,同时保持父类中的公共类,在本例中是test

但是,我不知道你的代码是什么样的,从那时起,这是你的选择

Module Module1

    Sub Main()
        Dim test As New test
        test.Production.Get_List()
        test.Shipping.Get_List()
    End Sub

End Module


Public Class Shipping
    Private parent As test

    Public Sub New(ByRef parent As test)
        Me.parent = parent
    End Sub

    Public Function Get_List() As List(Of Integer)
        Return parent.GetShipping_List
    End Function
End Class

Public Class Production
    Private parent As test

    Public Sub New(ByRef parent As test)
        Me.parent = parent
    End Sub

    Public Function Get_List() As List(Of Integer)
        Return parent.GetProduction_List
    End Function
End Class

Public Class test
    Public Property Production As Production
    Public Property Shipping As Shipping


    Public Function GetShipping_List() As List(Of Integer)
        Return Nothing
    End Function

    Public Function GetProduction_List() As List(Of Integer)
        Return Nothing
    End Function

    Public Sub New()
        Production = New Production(Me)
        Shipping = New Shipping(Me)
    End Sub
End Class

答案 1 :(得分:0)

请注意,您很可能应该重新评估您的架构,您可以像这样实现您的模式:

Public Class MyCustomClass
    Private _shippingList As List(Of String)
    Private _productionList As List(Of String)

    Public Production As ProductionClass
    Public Shipping As ShippingClass

    Public Sub New()
        Production = New ProductionClass(Me)
        Shipping = New ShippingClass(Me)
    End Sub

    Public Class ShippingClass
        Private _owner As MyCustomClass

        Public Sub New(owner As MyCustomClass)
            _owner = owner
        End Sub

        Public Function Get_List()
            Return _owner._productionList
        End Function
    End Class

    Public Class ProductionClass
        Private _owner As MyCustomClass

        Public Sub New(owner As MyCustomClass)
            _owner = owner
        End Sub

        Public Function Get_List()
            Return _owner._productionList
        End Function
    End Class

End Class

但是,如果您的真实意图只是以更易于理解的方式组织方法,我建议您考虑:

Public Class MyCustomClass
    Public Sub ShippingListGet()
    End Sub

    Public Sub ShippingListAddTo()
    End Sub

    Public Sub ShippingThatDO()
    End Sub

    Public Sub ShippingThisDo()
    End Sub

    Public Sub ProductionListGet()
    End Sub

    Public Sub ProductionListAddTo()
    End Sub

    Public Sub ProductionThisDo()
    End Sub

    Public Sub ProductionThatDo()
    End Sub
End Class

请记住,有些人认为难以阅读。我个人更喜欢按照这些方式组织,所以当按字母顺序对方法进行排序时,它们会按逻辑分组

答案 2 :(得分:0)

我找到了我正在寻找使用接口的解决方案

Public Interface ICompany
    Function Company_List() As DataTable
End Interface

Public Class MainClass
    Public Company As ICompany = New CompanyClass

    Public Sub New()
        MyBase.New()
    End Sub

    Private Class CompanyClass
        Public Sub New()
            MyBase.New()
        End Sub

        Public Function Company_List() As DataTable
              My code....
        End Function
End Class
End Class