Forcing forms to be declared & constructed as opposed to frm.Show

Is there a way to force declaration of a form, rather than going Form.Show?

What I mean is that if someone types in code like this:

frmForm.Show

I would like either an exception to be raised or come up with build error because it has to be instantiated first..

Rather, I would like to force forms to be declared and constructed so that code is more maintainable, and unseen errors are avoided.

Dim frm as frmForm = New frmForm(var1, var2, ...)
frm.Show()

Is there a way to do this?
khoocAsked:
Who is Participating?
 
flavoCommented:
Yeap.. If you use your own New method, you can no longer use FormName.Show

Example:

  '// frmForm
    Public Sub New(ByVal dummy As Boolean)
        InitializeComponent()
    End Sub

  '// Other form

   frmForm.Show()
'// Compile error above - Reference to non-stanard member requires an object reference


Tested with VS 2005 Pro.

Dave
0
 
appariCommented:
what version are you using?

in VS2003 without declaration you cannot use frm.Show
in VS2005 this is removed and you can call show method directly without creating the instance, it creates default instance.
0
 
khoocAuthor Commented:
oh, im using vs2005 (i've never used 2003).

is there a way to stop that in 2005?
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
appariCommented:

as far as i know i think its not possible.
Whats the problem if you use default instance? are facing any issues?
0
 
flavoCommented:
You can overload the New method:

'// Form 1
        Dim f As New Form2("test")
        f.Show

  '// Form 2
    Public Sub New(ByVal s As String)
        InitializeComponent()
        Me.Label1.Text = s
    End Sub

Dave
0
 
khoocAuthor Commented:
Dave, I was asking if I could prevent 'FormName.Show' from working... this is so every form must be declared with a constructor before it can be used.

Not facing issues - this is more of a preventative measure for better programming practice.  I would rather it not working in the first place so the developers won't be able to use it.

Thoughts from anyone?
0
 
khoocAuthor Commented:
Oh really?

I apologise for biting your head off then, I'm not around a .NET environment at the moment, so I just assumed it wouldn't work.

When I get to one, I'll have a go and then credit you the points if it works.

Thanks again :-)
0
 
flavoCommented:
:P
0
 
appariCommented:
learned new point today:)

but using this we will loose the functionality to instatiate a form without parameters as
dim f as Form2 = new Form2()

is there any work around?
0
 
khoocAuthor Commented:
can't u just create another constructor:

Public Sub New()

?
0
 
flavoCommented:
Not that I know of :-(
0
 
flavoCommented:
Yeah., I thought that to:

   Public Sub New()
        InitializeComponent()
    End Sub

  '// Other form
   frmForm.Show()
  '// No Exceptions thorwn into the mix

Dave
0
 
khoocAuthor Commented:
Hmm about that question, the only workaround I could figure out was to change the declaration to

Friend Sub New()

... although that won't help if you want to reference the form from another assembly.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.