【发布时间】:2014-08-06 04:27:07
【问题描述】:
我在文件MainFile.vb 中有一个Partial Class,其构造函数如下:
Partial Class MyAwesomeClass
' The constructor - Name it MainConstructor
Public Sub New(Dim x As Integer)
' Some awesome code here
Line1_of_code()
Line2_of_code()
Line3_of_code()
End Sub
End Class
现在我想在同一个构造函数中添加更多代码行,即MainConstructor,但我的问题是:
- 我无法编辑文件
MainFile.vb - 我无法创建另一个构造函数
- 我能做的就是——因为
MyAwesomeClass是Partial Class;我可以创建另一个文件,例如ExtendedFile.vb并编写我的代码行
所以我正在尝试这样做,这在.NET 中是不允许的:
Partial Class MyAwesomeClass
' The extended constructor - Name it ExtConstructor
Public Sub New(Dim x As Integer) ' Boom!!!! Error: Duplicate constructor with same kind of arguments
' my extended awesome code here
Line4_of_code()
Line5_of_code()
Line6_of_code()
End Sub
End Class
最终我想做一些类似的事情 - 当我创建 object 的 MyAwesomeClass 时;它应该执行Line1_of_code() 到Line6_of_code()。即
Dim objAwesome As New MyAwesomeClass(5) ' Any Integer will do
应该为objAwesome 执行以下所有行(并且以相同的顺序)
Line1_of_code()
Line2_of_code()
Line3_of_code()
Line4_of_code()
Line5_of_code()
Line6_of_code()
我正在使用 .NET Fx 4.0 -- 有什么变通方法或解决方案吗?任何帮助将不胜感激。
【问题讨论】:
标签: asp.net vb.net oop constructor partial-classes