Virtual Constructor or dynamic new()

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi everyone.

This is more of a curiosity question than anything, because there are
several ways to work around this problem (including another similiar way I've
used before), but the way I'm doing this is the way I find most "clean".

Essentially, I have an MDI form. All of the forms one may open up in my
application are mdi children, but the user never opens up more than one
instance of the same form (meaning that there is never multiples of the same
form type). So when the user opens up a form that is already visible, instead
of Show() ing again, I just set the windowstate to normal to "restore" the
window and bring it to the front.

Now the way I implemented this routine in the past was with the following
class (slimmed down):


Public Class AllForms
Inherits System.Windows.Forms.Form

'this will give the user the mdi parent.
Public ReadOnly Property frmMainMDI() As MainMDI
Get
Return Me.MdiParent
End Get
End Property

'Already active is a boolean that handles whether the form is currently
'shown
Protected _AlreadyActive As Boolean = False

'Handles the form being loaded. It just does everything that it would
normally do,
'except tells it that it's now active
Protected Overrides Sub OnLoad(ByVal e As EventArgs)
_AlreadyActive = True
MyBase.OnLoad(e)
End Sub

'Handles the form being closed. It Sets already active to false, since
it's
'now closed.
Protected Overrides Sub OnClosed(ByVal e As EventArgs)
_AlreadyActive = False
MyBase.OnClosed(e)
End Sub
End Class

Ok, then each of the child forms would simply inherit this form. The
application would check the already active bit and either restore() or show()
based on this.

But now I'd like to take it a step further. I'd like to implement a function
like such:

Public Sub ShowChildForm(ByRef theform As AllForms)

'check to see if the form is already visible
If (theform.AlreadyActive) Then

'set it to normal visibility again. (restore it)
theform.WindowState = FormWindowState.Normal


Else

theform = New AllForms
theform.show()
End If
End Sub

Then for each inherited form would be able to call it. For example:
ShowChildForm(NewTicketForm)

Now the problem, obviously, is that the AllForms class' constructor will be
called, and not the inherited form's. Is there a way to call the child form's
constructor, so the form will be initialized properly?

The way I originally thought to solve this was to call a pure virtual
function in the child class, and have the class be abstract. But there are
issues with this with the WinForms Designer, and lets just leave it at this
is basically impossible. And if it's a virtual function that isn't an
abstract function, it will call Allform's virtual function.

Any ideas? Obviously, I could just do this check on an individual basis, but
I like code reuse :)
 
The way I originally thought to solve this was to call a pure virtual
function in the child class, and have the class be abstract. But there are
issues with this with the WinForms Designer, and lets just leave it at this
is basically impossible. And if it's a virtual function that isn't an
abstract function, it will call Allform's virtual function.

No it won't - not if the method is overridden in the child form, and
called on an instance of the child form.

Alternatively, just call GetType() on the child form and then use
reflection to create a new instance. I'm not sure why you want to
create a new instance rather than just working with the one passed in.
 
Back
Top