Results 1 to 34 of 34

Thread: [RESOLVED] Creating subset Collection of objects

  1. #1

    Thread Starter
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,852

    Resolved [RESOLVED] Creating subset Collection of objects

    Basically, my question is ... when we add an item to a collection that's a Variant containing an object, are we:

    1) Adding the object to the Collection's internal Variant? or...
    2) Nesting our Variant with our object inside the Collection's internal Variant.

    Hopefully, I'm just adding the object and not my Variant.

    Here's the loop, with the middle line being the one I'm worried about:

    Code:
    
        Dim v As Variant
        For Each v In cFile
            If v.Text = "geometry" Then cGeom.Add v
        Next
    
    Specifically, I'm trying to make sure what that cGeom.Add v is doing.

    Both cFile and cGeom are Collections (of tree nodes). cGeom should wind up being a sub-collection of cFile.

    (Also, as an aside, I sure wish Collections had an AddSet property to make things more clear. However, I guess Add never looks at the default property, so it's sort of clear as it is.)
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  2. #2
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    5,872

    Re: Creating subset Collection of objects

    To my knowledge you can add anything to a collection and they don't have to be of the same type for a single collection.
    So you can add some primitives and then add a collection to the collection.
    I never ever use variants so whether the variant is check by the Add method of the collection, I really don't know.

  3. #3

    Thread Starter
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,852

    Re: Creating subset Collection of objects

    Quote Originally Posted by Arnoutdv View Post
    I never ever use variants so whether the variant is check by the Add method of the collection, I really don't know.
    That's my whole point. Each item of a collection has two pieces (ignoring all the internal pointer), a Variant for the data, and a String for the key. And I'm not using the key so it's just a collection of variants.

    And, to iterate a collection, I always use a Variant because that's what the collection item actually is.

    And, in this case, all those collection variants are set with objects.

    So, I just want to make sure that, when I'm moving my object reference from one collection to the other, am I nesting variants or am I just creating a new object reference?

    p.s. I don't just willy-nilly use Variants either, but this is one case where you must.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  4. #4

    Thread Starter
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,852

    Re: Creating subset Collection of objects

    I was having some other problems too, specifically passing a variant with a node, when the called procedure wanted an actual node. It wasn't letting me do that.

    So, I just wrote the following to solve everything:

    Code:
    
    Public Function V2Node(v As Variant) As Node
        ' Moves a Variant with a Node into an actual Node.
        Set V2Node = v
    End Function
    
    
    When adding to collections, and it's a variant with a node, I use that wrapper there as well. So, now there's no doubt about what I'm doing.

    I'm still curious though, so I'll leave the thread open.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  5. #5
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: Creating subset Collection of objects

    This should clear up any confusion:-
    Code:
    Private Sub Form_Load()
    
        Dim myObj As Class1
        Dim v As Variant
        
        Set myObj = New Class1
        
        Set v = myObj
        
        Debug.Print "External variant address: " & CStr(VarPtr(v)) & ", Object address: " & CStr(ObjPtr(v))
        
        FakeAdd v
    
    End Sub
    
    Private Sub FakeAdd(item As Variant)
        
        Dim internalVar As Variant
        
        Debug.Print "Variant argument address: " & CStr(VarPtr(item)) & ", Object address: " & CStr(ObjPtr(item))
        
        Set internalVar = item
        
        Debug.Print "Internal Variant address: " & CStr(VarPtr(internalVar)) & ", Object address: " & CStr(ObjPtr(internalVar))
        
        Debug.Print TypeName(internalVar)
        
    End Sub
    Output:-
    Code:
    External variant address: 1701828, Object address: 128072688
    Variant argument address: 1701828, Object address: 128072688
    Internal Variant address: 1701632, Object address: 128072688
    Class1
    Basically what this shows is that a Variant inside a Variant is not a thing. When a Variant is passed to the Add function, it will simply copy the object address to a new Variant assuming the Collection utilizes Variants internally.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  6. #6
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Location
    Sofia, Bulgaria
    Posts
    5,120

    Re: Creating subset Collection of objects

    Quote Originally Posted by Elroy View Post
    It wasn't letting me do that.
    Try changing signature to ByVal MyParam As MyClass so that you can pass Variant typed variables for this MyClass typed parameter too.

    Quote Originally Posted by Elroy View Post
    I'm still curious though, so I'll leave the thread open.
    You are adding the object reference to the collection in both cases. It's not trivial to create a Variant which points to a Variant which contains an object reference.

    cheers,
    </wqw>

  7. #7
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: Creating subset Collection of objects

    Quote Originally Posted by Elroy View Post
    I was having some other problems too, specifically passing a variant with a node, when the called procedure wanted an actual node. It wasn't letting me do that.
    You were probably passing it by reference. For example:-
    Code:
    Private Sub Form_Load()
    
        Dim c As Class1
        
        Dim v As Variant
        
        Set c = New Class1
        
        Set v = c
        
        ObjFunc v
    
    
    End Sub
    Private Sub ObjFunc(ByRef c As Class1)
        Debug.Print c.Value
    End Sub
    The above would fail. However if you passed the object by value:-
    Code:
    Private Sub ObjFunc(ByVal c As Class1)
        Debug.Print c.Value
    End Sub
    With the above change it would work.

    EDIT:

    wqweto beat me to it
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  8. #8

    Thread Starter
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,852

    Re: Creating subset Collection of objects

    Quote Originally Posted by Niya View Post
    Basically what this shows is that a Variant inside a Variant is not a thing. When a Variant is passed to the Add function, it will simply copy the object address to a new Variant assuming the Collection utilizes Variants internally.
    Thanks Niya. That's what I was thinking/hoping.

    Also, I just learned (and tested) something else I didn't know. You can use a specific class (not a Variant) to iterate a Collection so long as you're sure there's nothing else in the Collection. This fact solves a lot of problems for me:

    For a Class1:
    Code:
    
    Option Explicit
    
    Friend Property Get num() As Double
        num = Rnd
    End Property
    
    
    To test what I said (in Form1):
    Code:
    
    Option Explicit
    
    Private Sub Form_Load()
        Dim c As New Collection
        Dim num As Class1
        Set num = New Class1:   c.Add num
        Set num = New Class1:   c.Add num
        Set num = New Class1:   c.Add num
        Set num = New Class1:   c.Add num
        Set num = New Class1:   c.Add num
        'c.Add "asdf" ' If this is included, it will cause the iteration to error.
    
    
        For Each num In c
            Debug.Print num.num
        Next
    
    End Sub
    
    
    p.s. No puns about eating cookies.
    Last edited by Elroy; Jan 13th, 2022 at 12:05 PM.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  9. #9

    Thread Starter
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,852

    Re: Creating subset Collection of objects

    Quote Originally Posted by wqweto View Post
    Try changing signature to ByVal MyParam As MyClass so that you can pass Variant typed variables for this MyClass typed parameter too.
    Thanks for the tip. However, figuring out that I can use a variable declared with a specific class to iterate my collections is solving all of these problems.

    This one really is resolved now.
    Last edited by Elroy; Jan 13th, 2022 at 12:06 PM.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  10. #10
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: Creating subset Collection of objects

    Quote Originally Posted by Elroy View Post
    Thanks for the tip. However, figuring out that I can use a variable declared with a specific class to iterate my collections is solving all of these problems.
    Even if that were not possible, you could still iterate the Collection:-
    Code:
    Option Explicit
    
    Private Sub Form_Load()
        Dim c As New Collection
        Dim num As Class1
        Dim item As Variant
        
        Set num = New Class1:   c.Add num
        Set num = New Class1:   c.Add num
        Set num = New Class1:   c.Add num
        Set num = New Class1:   c.Add num
        Set num = New Class1:   c.Add num
        c.Add "asdf"
    
    
        For Each item In c
            If TypeOf item Is Class1 Then
                Set num = item
                Debug.Print num.num
            
            'Handle non-Class1 type objects
            ElseIf TypeName(item) = "String" Then
                Debug.Print item
            End If
        Next
    
    End Sub
    You can even handle unexpected types in the Collection in whatever way you like.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  11. #11

    Thread Starter
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,852

    Re: Creating subset Collection of objects

    Quote Originally Posted by Niya View Post
    Even if that were not possible, you could still iterate the Collection:-

    You can even handle unexpected types in the Collection in whatever way you like.
    Yeah, I understand ... that's basically what I was doing. But figuring out that I can iterate with a class (rather than a variant) just solves all kinds of things. It vastly simplifies things, and also makes calling other procedures within the iteration loop much easier (and clearer). And I love clarity.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  12. #12
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: [RESOLVED] Creating subset Collection of objects

    Fair enough
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  13. #13

  14. #14

    Thread Starter
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,852

    Re: Creating subset Collection of objects

    Quote Originally Posted by The trick View Post
    The collection class always accepts Variant variables. So if you pass a non-variant variable VB6 converts it to Variant and passes to the method. So if you pass already Variant variable VB6 doesn't perform this conversion.
    Ahhh, that's a nice way to explain it ... very clear and sensible.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  15. #15
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    5,872

    Re: Creating subset Collection of objects

    Quote Originally Posted by Elroy View Post
    Thanks Niya. That's what I was thinking/hoping.

    Also, I just learned (and tested) something else I didn't know. You can use a specific class (not a Variant) to iterate a Collection so long as you're sure there's nothing else in the Collection. This fact solves a lot of problems for me:

    For a Class1:
    Code:
    
    Option Explicit
    
    Friend Property Get num() As Double
        num = Rnd
    End Property
    
    
    To test what I said (in Form1):
    Code:
    
    Option Explicit
    
    Private Sub Form_Load()
        Dim c As New Collection
        Dim num As Class1
        Set num = New Class1:   c.Add num
        Set num = New Class1:   c.Add num
        Set num = New Class1:   c.Add num
        Set num = New Class1:   c.Add num
        Set num = New Class1:   c.Add num
        'c.Add "asdf" ' If this is included, it will cause the iteration to error.
    
    
        For Each num In c
            Debug.Print num.num
        Next
    
    End Sub
    
    
    p.s. No puns about eating cookies.
    This what always used without thinking about it.
    I only ever have collections which have the same datatype.
    Most often with a mother Class and child Classes

  16. #16

    Thread Starter
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,852

    Re: Creating subset Collection of objects

    Quote Originally Posted by Arnoutdv View Post
    I only ever have collections which have the same datatype.
    I use collections for all kinds of things, but yeah, the TypeName() of the items is typically all the same.

    I just knew you couldn't iterate them with native types, for instance...

    Code:
    
    Option Explicit
    
    Private Sub Form_Load()
        Dim c As New Collection
        c.Add "asdf"
        c.Add "qwer"
        c.Add "zxcv"
    
        Dim s As String
        For Each s In c
            Debug.Print s
        Next
    End Sub
    
    ...won't work. I just never thought to try it with an object variable of a specific class (which does work, so long as that's all that's in the collection).

    Never too late to learn something new.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  17. #17
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Location
    Sofia, Bulgaria
    Posts
    5,120

    Re: Creating subset Collection of objects

    Quote Originally Posted by Elroy View Post
    ...won't work. I just never thought to try it with an object variable of a specific class (which does work, so long as that's all that's in the collection).
    This is a compiler shortcoming (pure lazyness). There is no reason As Object typed iter variable working ok but As String not to.

    Probably TB will allow this as it already has to provision failure with type coercion on As Object iterators so allowing any type with IEnumVARIANT should not be too different/difficult.

    cheers,
    </wqw>

  18. #18
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: Creating subset Collection of objects

    Quote Originally Posted by wqweto View Post
    This is a compiler shortcoming (pure lazyness).
    Hmmm...I'm actually not sure it's laziness. I think MS backed themselves into a corner with certain design choices in VB6. This idea that primitives and objects are fundamentally different is what I believe is the problem here. Think about the following code:-
    Code:
        Dim v As Variant
        
        If TypeOf v Is TextBox Then
        
            Debug.Print "TextBox"
        
        ElseIf TypeOf v Is Integer Then
            
            Debug.Print "Integer"
        
        End If
    The above code is invalid because VB6's type system is not symmetrical and this has the potential for pure mischief. I don't know if you're aware but back when I first tested TwinBASIC, I ran into a huge problem with this when testing generics in TwinBASIC. Long story short, there was no way to write a generic class that caters to all types in TwinBASIC. Either you can cater to primitives like Strings and Integers or you could cater to Objects but not both. Microsoft promptly got rid of this problem when they created VB.Net. The above code actually works in VB.Net:-
    Code:
            Dim v As Object = 89I
    
            If TypeOf v Is TextBox Then
    
                Debug.Print("TextBox")
    
            ElseIf TypeOf v Is Integer Then
    
                Debug.Print("Integer")
    
            End If
    It should come as no surprise that you could also iterate over lists of any type in VB.Net.

    Going back to my experience trying to make generics work with all types in TwinBASIC, I found myself trying all kinds of different hacks to get around the problem but failing in the end. It is entirely probably Microsoft knew they painted themselves into a corner and decided to just accept that limitation in the end. The asymmetrical nature of VB6's type system is one of it's biggest failings. I would also point out that most other OO languages do not treat Objects and primitives as fundamentally different. For example in C++, you can use something like sizeof against a class/structure just as you could an int or a char.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  19. #19
    PowerPoster
    Join Date
    Jun 2013
    Posts
    7,219

    Re: Creating subset Collection of objects

    Quote Originally Posted by Niya View Post
    Hmmm...I'm actually not sure it's laziness.
    Anything one can fix with just one line of additional UserCode -
    would be equally easy to fix at compiler-level (basically).


    The "User-Fix" for a String-Typed enumeration in a For Each loop would be:
    Code:
    For Each vStrItem In ColWithStrings
         sStrItem = vStrItem 'it's just this coercion-call which the compiler would have to emit
         ...
    And if the compiler-writer is at it, reducing the efforts of "explicit Pre-Dimming of Loop-Vars" at this occasion,
    would be another, relatively easy to apply addition.

    But Wayne is aware of that already, IIRC...

    Olaf

  20. #20
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: Creating subset Collection of objects

    Quote Originally Posted by Schmidt View Post
    Anything one can fix with just one line of additional UserCode -
    would be equally easy to fix at compiler-level (basically).
    Yes, I'm aware of this work around. But it's just a band aid. It doesn't address the real problem which is the type system. Going back to my generics problem in TwinBASIC, if I remembered correctly, I could have used Variants there to solve my issues as well. However, it just felt wrong. I can't really explain it but it feels wrong having to perform internal coercions like that is hidden from the users of the class, not to mention it completely defeats the purpose of having generics in the first place. The idea behind generics is not having to sap performance by performing unnecessary coercions. Microsoft probably felt a similar way about iterating over a collection using a non-Variant control variable. If I were in their shoes to be honest, I would have done the same thing. Let the programmers work around it. I would not have the compiler implement hidden coercions like that because when they start looking at it's performance, it's going to frustrate them that the compiler is doing voodoo that they can't control. It's better to let the programmer decide for themselves if they want sacrifice a bit of performance for flexibility. That's just my take on it.
    Last edited by Niya; Jan 14th, 2022 at 06:27 AM.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  21. #21
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Location
    Sofia, Bulgaria
    Posts
    5,120

    Re: Creating subset Collection of objects

    Quote Originally Posted by Schmidt View Post
    Anything one can fix with just one line of additional UserCode -
    would be equally easy to fix at compiler-level (basically).


    The "User-Fix" for a String-Typed enumeration in a For Each loop would be:
    Code:
    For Each vStrItem In ColWithStrings
         sStrItem = vStrItem 'it's just this coercion-call which the compiler would have to emit
         ...
    And if the compiler-writer is at it, reducing the efforts of "explicit Pre-Dimming of Loop-Vars" at this occasion,
    would be another, relatively easy to apply addition.

    But Wayne is aware of that already, IIRC...

    Olaf
    Yes, the funny thing is this already happens for reference iterators (As Object or other) but somehow stumbled codegen for non-reference types.

    Quote Originally Posted by Niya View Post
    The above code actually works in VB.Net:-
    . . . but the price to pay is ginormous performance penalty on *boxing* primitive types.

    Yes, VB6 does not do boxing and we are grateful this was not conceived as an idea by MS until later Java implemented something similar.

    cheers,
    </wqw>

  22. #22
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: Creating subset Collection of objects

    Quote Originally Posted by wqweto View Post
    . . . but the price to pay is ginormous performance penalty on *boxing* primitive types.
    I'll have to look into it but I don't think you pay any boxing penalty for using the Is operator on a primitive type in VB.Net. My point is that fundamentally the type system in .Net doesn't treat primitives and classes as different as it does in VB6. Boxing/unboxing is a consequence of this design, that is true, but it is never forced on you. In my opinion it is a very small price to pay to avoid having to write code like this:-
    Code:
    Public Property Get Item(ByVal Index As Long) As Variant
        'Use zero based indexing because 1 based indexing
        'is ****in retarded
        
        Assign g_items.Item(Index + 1), Item
    End Property
    
    
    Private Sub Assign(ByVal value As Variant, ByRef var As Variant)
        'This is necessary to handle both objects and non-objects
        'because of the massive retardedness that is the Set statement.
        If IsObject(value) Then
            Set var = value
        Else
            var = value
        End If
        
    End Sub
    
    The above is an extract from the Pratt Parser demo I uploaded to the CodeBank last year. It's part of a Collection backed ArrayList class I wrote for easier handling of array-like data. Highlighted in red are the things I wish I didn't have to do.

    EDIT:

    For contrast, this is how this would look in VB.Net:-
    Code:
        Public ReadOnly Property Item(ByVal Index As Integer) As Object
            Get
                Return g_items.Item(Index)
            End Get
        End Property
    The above incurs a boxing penalty for primitive types but I'd rather pay that price for this simplicity.

    However, we don't have to pay that boxing penalty if we don't want to. We can use generics:-
    Code:
    Public Class ArrayListClass(Of T)
    
        Private g_items As T()
    
        Public ReadOnly Property Item(ByVal Index As Integer) As T
            Get
                Return g_items(Index)
            End Get
        End Property
    
    End Class
    T can be anything. It can be a primitive or it can be a class, structure or even a delegate. There is no need to be concerned with this because all types are treated equally at a fundamental level.
    Last edited by Niya; Jan 14th, 2022 at 08:43 AM.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  23. #23
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Location
    Sofia, Bulgaria
    Posts
    5,120

    Re: Creating subset Collection of objects

    Quote Originally Posted by Niya View Post
    My point is that fundamentally the type system in .Net doesn't treat primitives and classes as different as it does in VB6.
    That would be hard to believe. Treating everything homogenously as a reference type has its drawbacks in performance.

    I thought they made some progress in reverse directions with value types in recent versions following performance demands from users.

    In VB6 you can invoke Type Of operator on a value type only by wrapping it in a Variant and ultimately getting a run-time error. Another option is to use VarType and TypeName both with their own warts.

    .Net type system is so much more modern and versatile that there is no point comparing it.

    Btw, TB has Return statement and it sort of emulates your Assign impl.

    cheers,
    </wqw>

  24. #24

    Thread Starter
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,852

    Re: [RESOLVED] Creating subset Collection of objects

    Well, if we really wanted to, it's easy enough to "fix" in VB6 code.

    For a Class1:
    Code:
    
    Option Explicit
    
    Public str As String
    
    
    For a Form1 test:
    Code:
    
    Option Explicit
    
    Private Sub Form_Load()
        Dim coll As New Collection
        Dim sObj As Class1
    
        Set sObj = New Class1: sObj.str = "asdf": coll.Add sObj
        Set sObj = New Class1: sObj.str = "qwer": coll.Add sObj
        Set sObj = New Class1: sObj.str = "zxcv": coll.Add sObj
    
        For Each sObj In coll
            Debug.Print sObj.str
        Next
    
    
    End Sub
    
    
    Now that I know I can iterate with an object variable, this becomes easy.

    EDIT1: In fact, it becomes even a bit easier if we set the class's default property:

    Name:  Default.png
Views: 188
Size:  20.0 KB

    Then we can just do something like the following in our Form1:
    Code:
    
    Option Explicit
    
    Private Sub Form_Load()
        Dim coll As New Collection
        Dim s As Class1
    
        Set s = New Class1: s = "asdf": coll.Add s
        Set s = New Class1: s = "qwer": coll.Add s
        Set s = New Class1: s = "zxcv": coll.Add s
    
        For Each s In coll
            Debug.Print s
        Next
    
    
    End Sub
    
    Last edited by Elroy; Jan 14th, 2022 at 09:46 AM.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  25. #25
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Location
    Sofia, Bulgaria
    Posts
    5,120

    Re: [RESOLVED] Creating subset Collection of objects

    You can even mark str as default property and use sObj = "asdf" and Debug.Print sObj straight.

    cheers,
    </wqw>

  26. #26

    Thread Starter
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,852

    Re: [RESOLVED] Creating subset Collection of objects

    Quote Originally Posted by wqweto View Post
    You can even mark str as default property and use sObj = "asdf" and Debug.Print sObj straight.

    cheers,
    </wqw>
    haha, beat'cha to it.

    EDIT: Actually, I'm not sure I did. My modify time is 8:46 and your post time is 8:40. I must have hung onto editing my post too long.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  27. #27
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: Creating subset Collection of objects

    Quote Originally Posted by wqweto View Post
    That would be hard to believe. Treating everything homogenously as a reference type has its drawbacks in performance.
    Hmmm..I'm talking about it at a more fundamental level. All types, whether they are value types or reference types are descended from the Object type in .Net. This means at the most basic level all types are the essentially made up of the same stuff. Now they can be different. Some types are value types, some are reference types, this is true and you can't treat those the same. However when it comes to very basic operations like comparing types, this is where the foundation of the type system is important.

    Going back to an earlier example. This:-
    Code:
            Dim v As Object = 89I
    
            If TypeOf v Is TextBox Then
    
                Debug.Print("TextBox")
    
            ElseIf TypeOf v Is Integer Then
    
                Debug.Print("Integer")
    
            End If
    Is the same as doing this:-
    Code:
            Dim v As Object = New Integer
    
            If v.GetType = GetType(TextBox) Then
    
                Debug.Print("TextBox")
    
            ElseIf v.GetType = GetType(Integer) Then
    
                Debug.Print("Integer")
    
            End If
    The above shows that beyond the nuanced differences between different types like objects and primitives, they are all built on the exact same foundation, one of which is having a singleton Type object associated with it. This is what I think the VB6 type system is missing. I get the feeling that there is a big disconnect between primitives, objects and UDTs in VB6 and it's a source of many problems that don't exist in a more unified type system.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  28. #28

    Thread Starter
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,852

    Re: [RESOLVED] Creating subset Collection of objects

    In my somewhat repeated dabblings in .NET, I've also noticed that ... it prefers to have everything as an object.

    To my way of thinking, that's another example of how .NET bloats everything. If I just want a Single or a Long, all I want is four bytes for storage, and whatever memory pointers are necessary to find them again, nothing else.

    And, as illustrated above, if we want them as an object, it's easy enough to wrap them.

    EDIT: Oh dear , I've fallen into the trap.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  29. #29
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: [RESOLVED] Creating subset Collection of objects

    Quote Originally Posted by Elroy View Post
    In my somewhat repeated dabblings in .NET, I've also noticed that ... it prefers to have everything as an object.

    To my way of thinking, that's another example of how .NET bloats everything. If I just want a Single or a Long, all I want is four bytes for storage, and whatever memory pointers are necessary to find them again, nothing else.

    And, as illustrated above, if we want them as an object, it's easy enough to wrap them.

    EDIT: Oh dear , I've fallen into the trap.
    Not that I want to turn this into another VB6/VB.Net debate or anything but I feel compelled to point out that this is a huge misconception. When you look at the disassembly that represents the machine code produced by the compiler, you don't actually see any bloat. For example if you did this:-
    Code:
            Dim a As Integer = 2
            Dim b As Integer = 3
            Dim c As Integer = a + b
    The actual assembly for the addition looks like this:-
    Code:
    00DB1FED  mov         dword ptr [ebp-44h],2  
    00DB1FF4  mov         dword ptr [ebp-48h],3  
    00DB1FFB  mov         eax,dword ptr [ebp-44h]  
    00DB1FFE  add         eax,dword ptr [ebp-48h]
    If someone told you that was generated by a C compiler, there is nothing in there to prove otherwise.

    What most people don't realize is that a lot of what you call "bloat" is just an illusion meant to facilitate you in expressing your ideas in code more easily. A lot of it is stripped out of the actual compiled code that executes on your processor.

    Just to drive home the point about this being an illusion. Lets see how the illusion actually works. Look at this code:-
    Code:
            Dim i As Integer = 9
            Dim b As String
    
            b = i.ToString
    The above code would lead one to suspect that Integers in VB.Net are these heavy bloated objects. Right? I mean you're calling a ToString method so it has to be that every Integer in memory is this huge object with methods and properties right? No. It's an illusion. If you look at the disassembly, you will see this:-
    Code:
            Dim i As Integer = 9
    02F81FE3  mov         dword ptr [ebp-44h],9  
            Dim b As String
    
            b = i.ToString
    02F81FEA  lea         ecx,[ebp-44h]  
    02F81FED  call        System.Int32.ToString() (7047E1D0h)  
    02F81FF2  mov         dword ptr [ebp-4Ch],eax  
    02F81FF5  mov         eax,dword ptr [ebp-4Ch]  
    02F81FF8  mov         dword ptr [ebp-48h],eax
    As the above shows, highlighted in red, the Integer is just a DWORD value in memory. Nothing more. Highlighted in green is how ToString is actually implemented. It puts the pointer to the integer value in the ECX register and makes a call to some piece of code at address 7047E1D0h which is what performs the conversions. This is typical of what any compiler would produce for any normal function call in any language whether it's VB6, C or VB.Net. Whoever led you to believe otherwise has lied to you.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  30. #30

    Thread Starter
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,852

    Re: [RESOLVED] Creating subset Collection of objects

    Ok, I'm done. I could go on, but it's really not where I want my head to be. So, for anyone who wants to continue debating difference (advantages, disadvantages, bloat, etc) between VB6 and .NET, let's take it here, because, no matter what's said, I'm not taking this project to .NET.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  31. #31
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: [RESOLVED] Creating subset Collection of objects

    I'm not trying to convert you Elroy. I'm just pointing out some facts. Nothing more.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  32. #32

    Thread Starter
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,852

    Re: [RESOLVED] Creating subset Collection of objects

    Niya, your IMs appear to be full again. I was going to thank you for your last IM, but I couldn't send it. So, thank you.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  33. #33
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: [RESOLVED] Creating subset Collection of objects

    Ah yea. I probably need to clear out some old messages from years ago once more.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  34. #34
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: [RESOLVED] Creating subset Collection of objects

    Aite. I cleared out a bunch of stuff from my Inbox and Outbox.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

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