943,701 Members | Top Members by Rank

Ad:
  • VB.NET Discussion Thread
  • Unsolved
  • Views: 3801
  • VB.NET RSS
Jun 7th, 2007
0

Constructor overloading problem

Expand Post »
Hi all... I have a problem with constructor overloading in VB.NET and, I know how to solve it, but want to know the best way to do it... so here is the problem.
I have two manually created constructors in a class, like this...
VB.NET Syntax (Toggle Plain Text)
  1.  
  2. Public Sub New(param1 As String)
  3. ...
  4. End Sub
  5.  
  6. Public Sub New(param1 As String, param2 as String)
  7. Me.New(param1)
  8. ...
  9. End Sub
and everything goes fine... until I try to do error catching like this
VB.NET Syntax (Toggle Plain Text)
  1. Public Sub New(param1 As String)
  2. Try
  3. ...
  4. Catch ex As Exception
  5. Throw New Exception(...)
  6. End Try
  7. End Sub
  8.  
  9. Public Sub New(param1 As String, param2 as String)
  10. Try
  11. Me.New(param1)
  12. ...
  13. Catch ex As Exception
  14. Throw New Exception (...)
  15. End Try
  16. End Sub
then, it says that, for the constructor with 2 parameters, the call to the other constructor has to be the first instruction to call... but that's not possible if I want to catch the errors produced by the first constructor (if I move the Me.New(param1) outside the Try then every error thrown by the first constructor will not be catched.
I know that if I create a function (for example _New()) and overload this one, then, from the constructors I can just call the function and everything is solved (no Me.New() called), but I don't know if this is the correct way to go.

Thanks in advance and sorry for my english (Im spanish)
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
pep_castefa is offline Offline
2 posts
since Jun 2007
Jun 8th, 2007
0

Re: Constructor overloading problem

VB.NET Syntax (Toggle Plain Text)
  1. Public Sub New(param1 As String, param2 as String)
  2. Me.New(param1)
  3. Try
  4. ...
  5. Catch ex As Exception
  6. Throw New Exception (...)
  7. End Try
  8. End Sub

i think this will be enough , since the code of Me.New(param1) will be executed first , so u will catch the errors produced by the first constructor , right ?
Reputation Points: 37
Solved Threads: 17
Junior Poster
manal is offline Offline
122 posts
since Mar 2006
Jun 11th, 2007
0

Re: Constructor overloading problem

Click to Expand / Collapse  Quote originally posted by manal ...
VB.NET Syntax (Toggle Plain Text)
  1. Public Sub New(param1 As String, param2 as String)
  2. Me.New(param1)
  3. Try
  4. ...
  5. Catch ex As Exception
  6. Throw New Exception (...)
  7. End Try
  8. End Sub

i think this will be enough , since the code of Me.New(param1) will be executed first , so u will catch the errors produced by the first constructor , right ?
But, if the first constructor throws the exception, as the second has called Me.New(param1) outside the Try Catch Finally structure, the code crashes (the second constructor can't catch the exception produced by the first constructor called)
Reputation Points: 10
Solved Threads: 0
Newbie Poster
pep_castefa is offline Offline
2 posts
since Jun 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in VB.NET Forum Timeline: datagrid grouping question
Next Thread in VB.NET Forum Timeline: OledbConnection.getschema





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC