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

    1 100.00%
  • overloaded function

    0 0%
Results 1 to 5 of 5

Thread: sub new or functions to initialise a class * RESOLVED but vote if you wish*

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Oct 1999
    Location
    England
    Posts
    982

    Resolved 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.


    Things I do when I am bored: DotNetable

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    What is it about the initialization that makes it so uncertain?

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Oct 1999
    Location
    England
    Posts
    982

    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]


    Things I do when I am bored: DotNetable

  4. #4
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682
    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.

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Oct 1999
    Location
    England
    Posts
    982
    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


    Things I do when I am bored: DotNetable

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width