?
Solved

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

Posted on 2006-05-01
13
Medium Priority
?
261 Views
Last Modified: 2010-04-23
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?
0
Comment
Question by:khooc
  • 5
  • 5
  • 3
13 Comments
 
LVL 39

Expert Comment

by:appari
ID: 16582347
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
 

Author Comment

by:khooc
ID: 16582601
oh, im using vs2005 (i've never used 2003).

is there a way to stop that in 2005?
0
 
LVL 39

Expert Comment

by:appari
ID: 16582714

as far as i know i think its not possible.
Whats the problem if you use default instance? are facing any issues?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 34

Expert Comment

by:flavo
ID: 16582722
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
 

Author Comment

by:khooc
ID: 16582745
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
 
LVL 34

Accepted Solution

by:
flavo earned 1000 total points
ID: 16582766
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
 

Author Comment

by:khooc
ID: 16582782
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
 
LVL 34

Expert Comment

by:flavo
ID: 16582788
:P
0
 
LVL 39

Expert Comment

by:appari
ID: 16582904
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
 

Author Comment

by:khooc
ID: 16582909
can't u just create another constructor:

Public Sub New()

?
0
 
LVL 34

Expert Comment

by:flavo
ID: 16582910
Not that I know of :-(
0
 
LVL 34

Expert Comment

by:flavo
ID: 16582914
Yeah., I thought that to:

   Public Sub New()
        InitializeComponent()
    End Sub

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

Dave
0
 

Author Comment

by:khooc
ID: 16583964
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

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Loops Section Overview
Screencast - Getting to Know the Pipeline
Suggested Courses
Course of the Month16 days, 21 hours left to enroll

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question