Constructor overloading problem

Please support our VB.NET advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jun 2007
Posts: 2
Reputation: pep_castefa is an unknown quantity at this point 
Solved Threads: 0
pep_castefa pep_castefa is offline Offline
Newbie Poster

Constructor overloading problem

 
0
  #1
Jun 7th, 2007
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...
  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
  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)
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 122
Reputation: manal is an unknown quantity at this point 
Solved Threads: 17
manal's Avatar
manal manal is offline Offline
Junior Poster

Re: Constructor overloading problem

 
0
  #2
Jun 8th, 2007
  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 ?
"give only what u willing to receive "
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 2
Reputation: pep_castefa is an unknown quantity at this point 
Solved Threads: 0
pep_castefa pep_castefa is offline Offline
Newbie Poster

Re: Constructor overloading problem

 
0
  #3
Jun 11th, 2007
Originally Posted by manal View Post
  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)
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 2662 | Replies: 2
Thread Tools Search this Thread



Tag cloud for VB.NET
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC