2

我正在尝试将 mySubClass.vb 文件实现为另一个主类的嵌套子类。似乎这个Partial Class想法是我需要的,但是当我尝试将 mySubClass.vb 作为另一个主类的嵌套子类拉入时,实现不起作用。

我直接使用此代码的原始实现,mySubClass因此我知道该功能有效。我只想mySubClassclsMain.

主班

Public Class clsMain

    Public Property myIntProp as Integer
    Public property myStrProp as String
    'other properties

    Partial Public Class MySubClass
        'I want this functionality to be accessible via clsMain.MySubClass
        'Just like any other property or function of clsMain
        'Partial would keep things organized nicely
    End Class
End Class

子类

我想用作 clsMain.MySubClass 的 class.vb。文件:MySubClass.vb

Partial Public Class MySubClass
    Inherits BaseCollection

    Private Class MySubSubClass '(Used for custom properties and functions)
        'More properties and Functions
    End Class
End Sub

Public Class BaseCollection 'functionality of MySubClass
    Public Function MyFunction1()
        'Return Data
    End Function
End Class

如何使用主类

Public Class UsageClass
    Private myMainDataStructure as new clsMain

    Private Sub GetSubClassList()
        dim MyData as ArrayList = myMainDataStructure.MySubClass.MyFunction1() 
        '^^^ error on this line: MyFunction1() is not a member of project.clsMain.MySubClass^^^
    End Sub
End Class
4

1 回答 1

2

而不是试图使它成为一个部分类,你应该只创建一个包含该类的实例的属性。

嵌套类必须创建并具有实例,就像顶级类一样。通过在主类中创建一个属性,您可以在主类构造函数中自动创建该实例,并且您的代码将按预期工作。

Public Class clsMain

    Public Property myIntProp as Integer
    Public property myStrProp as String
    'other properties

    Public Property OtherFunctionality as MyOtherClass = New MyOtherClass()

然后只需在单独的文件中定义类:

Public Class MyOtherClass
    Public Sub MyFunction1()
于 2013-05-27T17:52:23.243 回答