I have recently discovered the singleton approach to mdi child forms and I was trying to integrate this singleton stuff into my "mChildform" class but I keep getting InvalidTypeCastException when I do so. Not sure where I am messing this up.
My mChildform class inherits Form and adds a few properties that are required by all child forms. All of my other project forms inherit this mChildform as their base type instead of form.
When I go directly to one of these forms (the logs form for example) and add
and then call that from my MDIParentCode:Private Shared aForm As logs = Nothing Public Shared Function Instance() As logs If aForm Is Nothing Then aForm = New logs() End If Return aForm End Function
I get the exact results that I want. Naturally, since this is something I want all child forms to be able to do I try to add it to my mChildform base class.Code:Dim f_logs As logs = logs.Instance() f_logs.MdiParent = Me f_logs.Show() f_logs.Activate()
The code compiles, but since the reference is of mChildForm type I want to cast it to my usable form class, but it throws the InvalidCastException.
I'm not sure why I can't cast it to the other form type, but i'm hoping someone here can explain why I cannot. The Instance() check would always fail if I do not cast my instance to another one of the types.Code:Dim fmdichild As PortMonitor = CType(mChildForm.Instance(), PortMonitor) fmdichild.MdiParent = Me fmdichild.Show() fmdichild.Activate()
Since my mChildForm class only holds minimal info to do any real work I have to cast it to the other type. As a workaround I can add the instance method to each new form but I would really like to get it into the base class.
thanks to anyone that can help me get it working or flat-out show me why it wont ever work, lol. Thanks!





Reply With Quote
