Results 1 to 6 of 6

Thread: [RESOLVED] mdi app singleton issue

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2008
    Location
    Charlotte, NC USA
    Posts
    70

    Resolved [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!

  2. #2
    Hyperactive Member guyvdn's Avatar
    Join Date
    Oct 2002
    Location
    Belgium
    Posts
    336

    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

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Mar 2008
    Location
    Charlotte, NC USA
    Posts
    70

    Re: mdi app singleton issue

    Quote Originally Posted by jmcilhinney View Post
    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.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: mdi app singleton issue

    Quote Originally Posted by vegeta4ss View Post
    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.
    Quote Originally Posted by vegeta4ss View Post
    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Mar 2008
    Location
    Charlotte, NC USA
    Posts
    70

    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
  •  



Click Here to Expand Forum to Full Width