Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calling overloaded form constructor in 2.0

Status
Not open for further replies.

SBendBuckeye

Programmer
May 22, 2002
2,166
US
Hello all,

In 1.2 I could overload a form's constructor with code similar to the following:
Code:
Public Sub New(ByVal myText As String)
    MyClass.New
    ' Do something
End Sub 'New
How do I do this in 2.0? Thanks in advance for any ideas and/or suggestions!
 
SBendBuckeye,

I program in 2.0 and thats the exact same way I do it as well so it appears to be the same in 2.0 as it is in 1.2. Im assuming you have Class Properties you're setting in your New Construction such as
Code:
Private Sub New (ByVal mText As String)
  MyBase.New
  Me.MyPropertyName = mText
End Sub
In 2.0 you can have as many Overloads as you wish
Code:
Private Sub New ()
  MyBase.New
End Sub

Private Sub New (ByVal mText As String)
  MyBase.New
  Me.MyPropertyName = mText
End Sub

Private Sub New (ByVal mText As String,ByVal mName As String)
  MyBase.New
  Me.MyPropertyName = mText
  Me.MyPropertyName2 = mName
End Sub
....And so on...
Hope this helps

Senior Qik III, ASP.Net, VB.Net ,SQL Programmer

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
SELECT * FROM Users WHERE clue > 0
0 Rows Returned

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
Hey PsychoCoder,

Thanks for the response. The standard New constructor appears to be implicit in both the form and form.Designer code. The New method appears in the Declarations list for both but is not actually emitted.

So if I enter code like my initial post, it gave me an error about needing to call InitializeComponent. It also gave me an error if I tried to reference MyClass.New saying that it was not defined.

Once I clicked on New in the Declarations for form.Designer it explicitly generated the standard New constructor and then my code worked as it would have in 1.2.
 
SBendBuckeye,

Try removing the MyClass.New, you should need this (Just looked through my code and I don't use it anywhere).
Code:
Private Sub New (ByVal mText As String)
  Me.MyPropertyName = mText
End Sub
And see if that helps.

Senior Qik III, ASP.Net, VB.Net ,SQL Programmer

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
SELECT * FROM Users WHERE clue > 0
0 Rows Returned

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
ThatRickGuy,

Thats what I had in my original, and unless I read his postw rong (which is possible at 4am) he said that wasnt working

Senior Qik III, ASP.Net, VB.Net ,SQL Programmer

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
SELECT * FROM Users WHERE clue > 0
0 Rows Returned

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
Thanks, guys! Your examples above is what I have used many times in VS2003. But it doesn't work in VS2005 at first because VS2005 does not explicitly emit the default parameterless constructor. That is why I was getting the error. Once I clicked on the Declarations area and selected New it explicitly emitted the New constructor and then my code was fine as it was before in VS2003. Sorry for the confusion.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top