-1

I have a Partial Class in a file MainFile.vb with a constructor like this:

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

Now I want to add some more lines of code in the same constructor i.e. MainConstructor but my problem(s) is/are:

  1. I can't edit the file MainFile.vb
  2. I can't create another constructor
  3. All I can do is -- since the MyAwesomeClass is a Partial Class; I can create another file e.g. ExtendedFile.vb and write my lines of code

So I'm trying to do like this which is not allowed in .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

Ultimately I want to do something like - When I create an object of MyAwesomeClass; it should execute Line1_of_code() to Line6_of_code(). I.e.

Dim objAwesome As New MyAwesomeClass(5) ' Any Integer will do

should execute all the below lines for objAwesome (and in the same order, too)

Line1_of_code()
Line2_of_code()
Line3_of_code()
Line4_of_code()
Line5_of_code()
Line6_of_code()

I'm using .NET Fx 4.0 -- Is there any workaround or solution? Any help would be appreciated.

Ruchir Gupta
  • 990
  • 3
  • 10
  • 20

1 Answers1

-1

You can think of a partial class as code in separate files. Then compiler will merge it in one class file.

One solution is to just make new inherited class and override constructor.

Second solution is to make Shared (static) method builder:

Partial Class MyAwesomeClass
    Public Shared Function Create() As MyAwesomeClass
    ' your code goes here
    ' calling base instance creation

Third solution is to make different signature for second constructor, because you cannot have two methods in class with same name and signature, e.g.

Partial Class MyAwesomeClass
    Public Sub New(Dim x As Integer, Dim buildWithNewAwesomeImplementation as Boolean) ' 
        Me.New(x) ' calling base constructor
        If(buildWithNewAwesomeImplementation)
          Line4_of_code()
          Line5_of_code()
          Line6_of_code()
        End if
    End Sub
End Class

First solution seems more reasonable.

Artur A
  • 7,115
  • 57
  • 60
  • Well, I know about Partial Class very much. As I have already said, I can neither edit the `MainFile.vb` nor create another constructor. So first two solutions won't work. The third solution is also useless for me because my question clearly says that I want something like "partial constructors". Anyways, thanks for trying to help me. – Ruchir Gupta Aug 06 '14 at 08:05
  • Hm, is the class sealed? or why can not you just make a good wrapper for this class? One more solution is very tricky and requires interceptor for constructor (AOP), e.g. using PostSharp http://www.codeproject.com/Articles/337564/Aspect-Oriented-Programming-Using-Csharp-and-PostS – Artur A Aug 06 '14 at 17:19