我正在尝试将mySubClass.vb文件实现为另一个主类的嵌套子类。似乎Partial Class
想法是我需要的,但是当我尝试将mySubClass.vb作为另一个主类的嵌套子类拉出时,实现不起作用。
此代码的原始实现直接使用mySubClass
,因此我知道功能有效。我只想在mySubClass
中使用clsMain
作为数据结构。
主要类
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
答案 0 :(得分:2)
您应该只创建一个包含该类实例的 Property ,而不是尝试将其设为Partial Class。
必须创建嵌套类并拥有实例,就像顶级类一样。通过在主类中创建一个属性,您可以在主类构造函数中自动创建该实例,并且您的代码将按预期工作。
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()