|
-
Jan 19th, 2010, 02:46 PM
#1
Thread Starter
Lively Member
[RESOLVED] mdi app singleton issue

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
Code:
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
and then call that from my MDIParent
Code:
Dim f_logs As logs = logs.Instance()
f_logs.MdiParent = Me
f_logs.Show()
f_logs.Activate()
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.
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.
Code:
Dim fmdichild As PortMonitor = CType(mChildForm.Instance(), PortMonitor)
fmdichild.MdiParent = Me
fmdichild.Show()
fmdichild.Activate()
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.
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!
-
Jan 19th, 2010, 05:12 PM
#2
Hyperactive Member
Re: mdi app singleton issue
You cannot add this code to the mChildform base class since you can only create an instance of an mChildform and not of a derived form. Maybe if you use Reflection or Generics you can get it to work but I am not sure it is worth the trouble for a method with almost no logic in it.
To deny our own impulses is to deny the very thing that makes us human
-
Jan 19th, 2010, 11:06 PM
#3
Re: mdi app singleton issue
You would have to implement the singleton logic in each and every one of your derived child form classes.
It would be simpler to use the default instance, which inherently forces singleton behaviour if you use ONLY the default instance.
Alternatively, you could use this class of mine, which incorporates the MDI child behaviour too:
http://www.vbforums.com/showthread.php?t=487332
-
Jan 20th, 2010, 10:06 AM
#4
Thread Starter
Lively Member
Re: mdi app singleton issue
 Originally Posted by jmcilhinney
You would have to implement the singleton logic in each and every one of your derived child form classes.
It would be simpler to use the default instance, which inherently forces singleton behaviour if you use ONLY the default instance.
Alternatively, you could use this class of mine, which incorporates the MDI child behaviour too:
http://www.vbforums.com/showthread.php?t=487332
I'll have a look at your class.
Could you elaborate further on the default instance stuff? I'm not following.
Another question, does the my.forms implement singleton? If it does, i'll just use that.
-
Jan 20th, 2010, 07:26 PM
#5
Re: mdi app singleton issue
 Originally Posted by vegeta4ss
Could you elaborate further on the default instance stuff? I'm not following.
Follow the Blog link in my signature and check out my post on default instances.
 Originally Posted by vegeta4ss
Another question, does the my.forms implement singleton? If it does, i'll just use that.
The properties of My.Forms return default instances. Assuming that Form1 is a class that inherits Form, My.Forms.Form1.Show() is the same as Form1.Show(). You access the default instance of a form via its class name, as that blog post of mine explains.
-
Jan 21st, 2010, 09:41 AM
#6
Thread Starter
Lively Member
Re: [RESOLVED] mdi app singleton issue
your blog post was helpful. I dropped a couple questions in my comment on the article if you feel like answering them.
thanks jmcilhinney
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|