PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
VS 2010 Not able to call Show method of Default instance of the form-VBForums
Results 1 to 7 of 7

Thread: Not able to call Show method of Default instance of the form

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,133

    Not able to call Show method of Default instance of the form

    I have added two Optional parameter in the Constructor of the form, as follows

    vb Code:
    1. Public Sub New(Optional ByVal A As String = "", Optional ByVal B As String = "")
    2.  
    3.         ' This call is required by the designer.
    4.         InitializeComponent()
    5.  
    6.         ' Add any initialization after the InitializeComponent() call.
    7.  
    8.     End Sub

    Now, the Intellisense is not listing the Show method for this form

    plz help

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,913

    Re: Not able to call Show method of Default instance of the form

    You need your form to have a parameterless constructor in order to have a default instance. Obviously declaring the constructor parameters Optional isn't enough. Use overloading instead.

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,133

    Re: Not able to call Show method of Default instance of the form

    Quote Originally Posted by jmcilhinney View Post
    You need your form to have a parameterless constructor in order to have a default instance. Obviously declaring the constructor parameters Optional isn't enough. Use overloading instead.
    I have tried, but the compiler is showing an error as "cannot overload each other because the differ only by optional parameters"

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,913

    Re: Not able to call Show method of Default instance of the form

    Quote Originally Posted by aashish_9601 View Post
    I have tried, but the compiler is showing an error as "cannot overload each other because the differ only by optional parameters"
    The idea is that you overload the constructor instead of using optional parameters, but you then have the issue of having two parameters of the same type, so you can't have two constructors with one parameter each. Basically, you would have to have one constructor with no parameters and one with two. If you want to have a constructor with one parameter then you can only have one so you'd have to decide which parameter would likely be used on its own more often.

    Having said all that, why not just not use default instances at all? I never use them and you'll find that most experienced developers don't. There's never a need to use them - they don't exists in C# and didn't in VB until 2005 - and they were really only added so VB6 developers migrating to VB.NET would feel more at home. You don't need them and if you don't use them then you can stick with the one constructor with optional parameters and there's no problem.

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,133

    Re: Not able to call Show method of Default instance of the form

    Well , i also avoid using the default instance of the form, but what made me do so i am opening this form(form2) from another form's menu (Like Main Window).

    The form2 is not opened in Modal mode (ShowDialog)
    So when the user clicks the menu again i need to show the same instance which is already opened.

    so I had to use
    Form2.Show()
    Form2.Activate()

    yes i can do the same by declaring an instance of the form
    But i like to avoid declaring all the instances before (for about 20 forms)

    In this case i think i need to..

    Thanks

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    34,056

    Re: Not able to call Show method of Default instance of the form

    There is one place you might use a default instance, which is the startup form. There are lots of simple programs where there isn't any reason to bother with NOT using the default instance of the initial form. However, beyond that initial form, you really don't want to be using default instances, and creating a form in a menu is very much NOT a place to be using the default instance.

    Default instances aren't horrible, because they are just forms, after all. However, they are forms that do have a few little quirks to them that can cause you trouble. I'd say that most people stay away from them because they just don't want to have to remember/deal with the quirks of the default instances. Most of my forms can't have parameterless constructors, so I couldn't use the default instance if I wanted to, but that's a quirk of some of the things I'm working on.

    In this case, just don't use the default instance. There's no need for it, and it gains you nothing good. If you want a globally accessible form, then you could declare it in a module, but you sure don't appear to need a form that is accessible beyond the form that is showing it, so just declare it in that form.
    My usual boring signature: Nothing

  7. #7
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,913

    Re: Not able to call Show method of Default instance of the form

    Quote Originally Posted by aashish_9601 View Post
    Well , i also avoid using the default instance of the form, but what made me do so i am opening this form(form2) from another form's menu (Like Main Window).

    The form2 is not opened in Modal mode (ShowDialog)
    So when the user clicks the menu again i need to show the same instance which is already opened.

    so I had to use
    Form2.Show()
    Form2.Activate()

    yes i can do the same by declaring an instance of the form
    But i like to avoid declaring all the instances before (for about 20 forms)

    In this case i think i need to..

    Thanks
    There's no such thing as "declaring an instance" of anything. Declaring is something you do with variables. Instances of a type are created. They are not the same thing. Doing what you want to do without a default instance is easy. Like I said, there are no default instances in C# and there weren't in VB.NET to begin with either, yet we all survived. It's this simple:
    vb.net Code:
    1. Private f2 As Form2
    2.  
    3. Private Sub ShowForm2()
    4.     If f2 Is Nothing OrElse f2.IsDisposed Then
    5.         f2 = New Form2
    6.     End If
    7.  
    8.     f2.Show()
    9.     f2.Activate()
    10. End Sub
    It's not quite as simple as using a default instance but hardly complex and, to be honest, if you're not using JUST the default instance then I'd suggest that using the default instance at all is a bad idea anyway.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width