Results 1 to 11 of 11

Thread: Why like this??

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    May 2002
    Posts
    1,602

    Why like this??

    Im managing an application written by a consultant and I have come across a strange thing... on each class he is writing

    VB Code:
    1. Public Sub New()
    2.        myDb = New clsDb
    3. End Sub

    where db is a database access layer class written by him
    and when he is using the class objects he is using
    VB Code:
    1. dim db as clsDb

    just creating a reference, not instanciating a new object


    Can anyone give a decent explaination? I can't understand why?

    why not just write

    VB Code:
    1. dim db as new clsDb

    A class which anyway is refrerenced this often should have shared methods I think, don't you agree?????

    kind regards
    Henrik - strange things are happening....

  2. #2
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    I see no reason to initialize the variable in the constructor especially since New shouldn't be fired until the variable is in fact initialized anyway. Maybe its just some personal quirk.

    As for it being shared or not I don't think that the frequency of use should determine whether or not it's shared. I think the determining factor for a shared method should be whether or not it relates to or needs an instance of the object in question to work or not. So if the method constantly refers to instance members or requires instance related resource (to check state) then it shouldn't be Shared. Although if you wanted it to then you could just pass in the instance in question. I guess it just depends on preference.

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    May 2002
    Posts
    1,602
    Thanks..

    You are absolutly right about shared.. a thought glitch on my account...

    I noticed that in the code I am working on (about 15 classes) that option strict was set to off, I turned it on(company policy) and looked at about 5 hours of extra work to correct things

    * variables declared without type

    * functions which doesn't return something (should be type)

    * all different crazy types of cast errors object->string etc etc

    * global variables that isn't needed

    * References to Me where it doesn't make sense at all

    * I checked how much this dude was payed, and it was a lot more than they pay me each month...


    Henrik - hate digging into other peoples code when it looks like this

  4. #4
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    HI,

    I thought that outside of instancing, referencing an object through a variable was used in subs which were accessed from several different locations, which required the same sequence of code, but relating to different instances of the same class( or even different classes in some circumstances). The same effect is, of course, achieved through passing parameters to a sub, but if several properties are involved, a reference is simpler.

    Perhaps that is what the original programmer was thinking about.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    May 2002
    Posts
    1,602
    I understand what you mean, but it doesn't make sense in this case... or

    perhaps you are thinking about the Singleton GoF pattern. where you set the default contructor to private and write your own, like

    GetInstance()

    But for that to work (and be professional) you must first check if there already is an instance created (is the object nothing?) and the make it thread save by using a mutex object...

    The author of the code Im working on has done nothing like that...

    If you are thinking about any other programming technique im not aware of, perhaps some other forum member can response to this?


    kind regards
    Henrik
    Last edited by MrNorth; Feb 23rd, 2004 at 07:48 AM.

  6. #6
    Fanatic Member VBCrazyCoder's Avatar
    Join Date
    Apr 2003
    Posts
    681
    I don't think that I would have that consultant do any future work for your company.

  7. #7
    Fanatic Member VBCrazyCoder's Avatar
    Join Date
    Apr 2003
    Posts
    681
    Just a thought - maybe he was attempting to make it difficult to maintain so that your company would have to hire him / her back for any changes . Although I would hope that this is not the case, as it makes all consultants and programmers look bad.

  8. #8
    PowerPoster
    Join Date
    Jul 2002
    Location
    Dublin, Ireland
    Posts
    2,148
    VB Code:
    1. Public Shared Sub New()
    2.        myDb = New clsDb
    3. End Sub

    This is a singleton constructor - it will be run only once for each class, not once for each instance as per a non-shared "new()".

    Presumably all instances of the class share the same db connection object to reduce resource usage.

  9. #9

    Thread Starter
    Frenzied Member
    Join Date
    May 2002
    Posts
    1,602
    I had no idea about that... in every book I have read about patterns, the have never implemented it like that... okies


    /henrik

  10. #10
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Although the code he posted was not shared but an Instance method.

  11. #11

    Thread Starter
    Frenzied Member
    Join Date
    May 2002
    Posts
    1,602
    Oops, didn't notice that he had written a shared... then it might work as a singleton constructor, but in my example it should not...

    kind regards
    Henrik

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