PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
Collection [Resolved but interesting discussion going on!]-VBForums
Results 1 to 13 of 13

Thread: Collection [Resolved but interesting discussion going on!]

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2003
    Location
    a place called home
    Posts
    64

    Collection [Resolved but interesting discussion going on!]

    Recently started a new project in VB6. I'm facing some problems. In this codeblock I'm running true a collection to show it in a listview. For one item of the listview, I need to get some data from an other collection, which I do in the part between the *** Like you can guess, it doesn't work.

    lev and bb are objects of classes I made. I know object oriŽntated programming is not THE VB6 style, but I'm an oo-addict so if it's possible I use them. To be honest, it's realy interesting to use classes in VB6, certainly when you're using collections.

    Anyway ... someone help me out! My code is here ...

    VB Code:
    1. Public colLevering As New Collection
    2. Public colLevNaam As New Collection
    3.  
    4.     Dim lev As New Leverancier
    5.     Dim bb As New Bestelbon
    6.    
    7.     For Each bb In colLevering
    8.         Set lsiBbon = lsvBbon.ListItems.Add(, , bb.Bestelid)
    9.         lsiBbon.SubItems(1) = bb.Bestelnr
    10.         '   ********************************************************
    11.         For Each lev In colLevNaam
    12.             If bb.LevId = colLevNaam.Item(lev.LevId) Then
    13.                 lsiBbon.SubItems(2) = colLevNaam.Item(lev.bedrijf)
    14.             End If
    15.         Next
    16.         '   ********************************************************
    17.         lsiBbon.SubItems(3) = bb.BestelDatum
    18.         lsiBbon.SubItems(4) = bb.UitersteLevDatum
    19.         lsiBbon.SubItems(5) = bb.EcoGegId
    20.     Next
    Last edited by lazy; Aug 24th, 2005 at 06:02 AM.
    A good programmer is a LAZY programmer!

  2. #2
    Frenzied Member Devion's Avatar
    Join Date
    Sep 2000
    Location
    The Netherlands
    Posts
    1,049

    Re: Collection

    What kind of error are you getting?

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Mar 2003
    Location
    a place called home
    Posts
    64

    Re: Collection

    Run Time Error '9':
    Subscript out of range

    When I debug, he goes to this line ...

    VB Code:
    1. If bb.LevId = colLevNaam.Item(lev.LevId) Then
    A good programmer is a LAZY programmer!

  4. #4
    Frenzied Member Devion's Avatar
    Join Date
    Sep 2000
    Location
    The Netherlands
    Posts
    1,049

    Re: Collection

    Well you are doing something weird though..

    This: (see bold)
    VB Code:
    1. For Each lev In colLevNaam
    2.             If bb.LevId = [b]colLevNaam.Item(lev.LevId)[/b] Then

    Try: (see bold)
    VB Code:
    1. For Each lev In colLevNaam
    2.             If bb.LevId = [b]lev.LevId[/b] Then

    You are already enumerating through the collection and thus Lev is filled with the current item in colLevNaam. Bit weird to actually re'fetching it from the colLevnaam huh?

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Mar 2003
    Location
    a place called home
    Posts
    64

    Re: Collection

    Problem solved! What a big mistake I made! Thanks a lot!
    A good programmer is a LAZY programmer!

  6. #6
    Frenzied Member Devion's Avatar
    Join Date
    Sep 2000
    Location
    The Netherlands
    Posts
    1,049

    Re: Collection [Resolved]

    It's normal to read over such code and not spotting the problem initially.. everyone has those days.

  7. #7
    Lurker
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,651

    Re: Collection [Resolved]

    Quote Originally Posted by lazy
    VB Code:
    1. Dim lev As New Leverancier
    2.     Dim bb As New Bestelbon
    Nooo....
    VB Code:
    1. Dim lev As Leverancier
    2. Dim bb As Bestelbon
    Yes...

    Otherwise, you may (I speak with somewhat less authority here after having been proven wrong in the past) unnecesarily instantiate the objects, when all you are using them for is enumeration variables. You should never use Dim As New, ever, unless you know exactly when it will be automatically instantiated

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Mar 2003
    Location
    a place called home
    Posts
    64

    Re: Collection [Resolved]

    Interesting discussion point ... but I have no point of view that I can try to explain, cause thats the way I learned it. To be honest ... what is the point of view of the other vbforums on this topic? New of no New!
    A good programmer is a LAZY programmer!

  9. #9
    Lurker
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,651

    Re: Collection [Resolved but interesting discussion going on!]

    I take it you don't fully understand the different methods of object declaration and instantiation, and their various perils. Let me try to explain

    Strong binding gives you the most control.
    VB Code:
    1. Dim objMyObject As CMyClass
    2. Dim objMyObject As Object

    Now the difference between these two examples. The first uses early binding, where the variable is bound to the class type at compile-time. This is most efficient, however restricts you to only setting that variable to objects of that type.

    The second example uses late binding. That is less efficient, because the interface (methods and properties) and type description of the object is obtained at run-time. However, it does allow you to use the variable for objects of multiple types.

    Let's look at instantiation. This is where your object instance is created, from the class.

    VB Code:
    1. ' instantiate a new CMyClass object and set the variable to it.
    2. Set objMyObject = New CMyClass
    3.  
    4. ' destroy it
    5. Set objMyObject = Nothing

    Now lastly, here is a strange way to combine binding and instantiation, which I think is unique to Classic VB. As opposed to strong binding, it is called loose binding.
    VB Code:
    1. Dim objMyObject As New CMyClass

    What that means is, at runtime, VB will always ensure that objMyObject points to a valid instance of CMyClass. If you destroy it and then try to access its properties or call one of its methods, VB will automatically instantiate a new object for you and point the variable to it.

    Now this adds a significant overhead at run-time. Why? Because every time you access the object's methods or properties, VB has to first check to see if the object exists.

    So, here is a comparison of the run-time difference between strong and loose binding.
    VB Code:
    1. ' Strong binding:
    2. Dim objMyObject As CMyClass
    3. Set objMyObject = New CMyClass
    4. MsgBox objMyObject.FunctionMethod(objMyObject.SomeProperty)
    VB Code:
    1. ' Here's what happens with loose binding:
    2. Dim objMyObject As New CMyClass
    3.  
    4. ' try to access the property
    5. If (objMyObject Is Nothing) Then _
    6.     Set objMyObject = New CMyClass
    7. propVal = objMyObject.SomeProperty
    8.  
    9. ' try to call the method
    10. If (objMyObject Is Nothing) Then _
    11.     Set objMyObject = New CMyClass
    12. MsgBox objMyObject.FunctionMethod(propVal)

    But, here's an advantage to loose binding. If you have an object that might be referenced, but might not, you can declare it using the "As New" syntax, and if it is never needed, it will never be instantiated. If it is needed, then just reference it normally, and it will be automatically instantiated for you.

    Finally, you cannot combine loose and late binding, to create the evil of all evils , because you cannot create a generic object instance.

    As you can see, each method has advantages and disadvantages. I hope that explanation clears things up for you and everyone
    Last edited by penagate; Aug 24th, 2005 at 06:28 AM.

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Mar 2003
    Location
    a place called home
    Posts
    64

    Re: Collection [Resolved but interesting discussion going on!]

    I know the meaning of strong binding and loose binding, but I never really thought about the consecuences of it, just because I followed the way I learned it, and one's you learned something, you don't change those things. To be honest, I really appreciate your explication and I enjoyed reading it, cause if all this is true and nobody say's different things about what you said, it is quite interesting. So maybe this will change my way of programming in the future.

    Thank you very much! It's a pleasure!
    A good programmer is a LAZY programmer!

  11. #11
    Lurker
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,651

    Re: Collection [Resolved but interesting discussion going on!]

    I know the feeling. But as a professional programmer, it's a good thing to be able to change your style on the fly. I am constantly doing it, because I keep learning new things. As a hobbyist, or part-time programmer, it's a bit harder to do that, and you tend to get fixed into a particular style.

    Thank you very much! It's a pleasure!
    Not a problem

  12. #12
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    32,732

    Re: Collection [Resolved but interesting discussion going on!]

    If at all possible I NEVER, NEVER use the As New when dimming an object of any type. At least in VB6.... .NET is a whole different animal and a topic for another time.

    And yes, this is typical of the VB6 programming world, while it is convenient to dim as new, it's not always the best practice. At least you are willing to learn something new and can see the benefits of being able to change your style. There's a lot of people out there that would just go "well, that's the way I was taught so that's the way I do it, so there." and just blow things off no matter how much better it would be for them.

    It bothers me slightly that people are being taught to do things a certain way w/o explanation as to why it should (or should not) be done that way. Then again, it may explain a lot.

    Tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  13. #13
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Candon City, Ilocos Sur, Phils.
    Posts
    11,038

    Re: Collection [Resolved but interesting discussion going on!]

    I also have a 'Loose Binding' link in my sig, this might be relevant in this discussion.
    Regards,

    ô

    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width