View Poll Results: Which is better when initialising classes. Overloaded function or Overloaded Sub New
- Voters
- 1. You may not vote on this poll
-
overloaded sub new
-
overloaded function
-
Nov 2nd, 2004, 04:53 AM
#1
Thread Starter
Fanatic Member
sub new or functions to initialise a class * RESOLVED but vote if you wish*
I am in the process of designing a class.
I can either write an overloaded function to initialise a class or I can overload sub new.
The advantage of the function is I can return a value indicating whether the initilaisation was successful.
The disadvantage is the function will have to be called every time a new instance of the class is created. With sub new the initialisation parameters are in the dimentialisation (dim) statement.
What do you reckon
Last edited by davidrobin; Nov 2nd, 2004 at 06:35 AM.
-
Nov 2nd, 2004, 04:56 AM
#2
What is it about the initialization that makes it so uncertain?
-
Nov 2nd, 2004, 05:01 AM
#3
Thread Starter
Fanatic Member
Re: sub new or functions to initialise a class
[list=1][*]A function (with parameters passing in the initialisation values) can return a value e.g. boolean to indicate success. A sub cannot[*]The function would need to be called for each class instance. For sub new it would not. The parameters to the sub new would pass in the intialisation values.[/list=1]
-
Nov 2nd, 2004, 06:06 AM
#4
The standard way is to use the New(...) standard constructor, override it if you wish.
If the initialization is NOT successful then throw an exception. Otherwise it is safe to assume that it was successful.
Code:
Public Sub New(myArg as integer)
'do some initialising
if Problem then throw new argumentexception()
end sub
I don't live here any more.
-
Nov 2nd, 2004, 06:35 AM
#5
Thread Starter
Fanatic Member
What I didn't want to do was handle an exception in the sub new of the class. I want to tell the calling app of success or failure.
I just hadn't thought about throwing an exception back.
I would prefer to use the Sub new anyway
Thanks
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|