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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Is Public sub new needed?

Status
Not open for further replies.

SMURF75

Programmer
Aug 28, 2004
87
GB
I see "public sub new" every once in a while in some classes, without any code else than "mybase.new" inside.
So why does one create such a sub when it obviously work without it? Am I missing something vital here.. or is it just old habit or for readability some people put it there?

- - - - - - - - - - - - - - - - - -
Im three apples high, Im blue, and i most certainly like that cold beer that should be every mans right after a hard days work!
 
When you instantiate a class the code in the sub NEW runs. As you rightly say the default NEW code created does the bare minimum needed to instantiate the class.

However it can of course be amended/overloaded, so that the New event can be passed parameters, which is sometimes far easier than instantiating a class and then setting class properties. You can also have more than one sub NEW, to allow overloading.





Sweep
...if it works dont f*** with it
...if its f****ed blame someone else
...if its your fault that its f***ed, say and admit nothing.
 
Well, what my real question was... ;)

When I create a class I leave out the public sub new because the class seems to work just as fine without it. Altough, (and now im not taking about the wizard genrated classes created by VS/VB) sometimes i find examples where people seems to have written in that sub just for fun. Why? Readability? Hidden benfits?



- - - - - - - - - - - - - - - - - -
Im three apples high, Im blue, and i most certainly like that cold beer that should be every mans right after a hard days work!
 
if it has mybase.new in it will serve a purpose because it will call the new of the class you inerited from. It's not because your class has nothing in it that the class you inherited from has nothing in it. If your class has properties that can be set I would always create several new's. at least one with nothing in it and one with several parameters in it. It is never a good idea to instantiate a class and leave all the properties empty. You should fill them with something. the only class i wouldnt use a new in, is a shared class (earthandfire would use a module for that).

Christiaan Baes
Belgium

If you want to get an answer read this FAQ faq796-2540
There's no such thing as a winnable war - Sting
 
Thank you, Chrissie, for reminding everyone of the right? way to do things. [wink]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top