Results 1 to 15 of 15

Thread: [RESOLVED] How to implement dynamic objects like RC6.DynObj?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2020
    Posts
    357

    Resolved [RESOLVED] How to implement dynamic objects like RC6.DynObj?

    RC6 provides a very magical object DynObj, which can dynamically add properties and methods like JavaScript objects, for example:

    Code:
    Private Sub Form_Load()
      Dim oDyn As Object
      Set oDyn = New_c.DynObj
          oDyn.a = 123
          oDyn.b = "xyz"
          oDyn.c = True
      
      'Ok, readout-tests:
      Debug.Print oDyn.a, oDyn.b, oDyn.c
      
    End Sub
    If I could use VB6 to achieve a feature/class similar to RC6.DynObj too, it'll greatly promote my work. I'd like to know if anyone other than Olaf has implemented a similar feature? If anyone could provide some suggestions and help, it would be greatly appreciated.

  2. #2
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Posts
    3,094

    Re: How to implement dynamic objects like RC6.DynObj?

    Wait for Olaf to publish RC6 source code as promised then you can freely copy/paste it from his implementation.

    It's a non-trivial IDispatch customization, probably requiring a very fast container class like the hashsets implemented in RC6, so your waiting from the first point above will be double rewarded.

    cheers,
    </wqw>

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2020
    Posts
    357

    Re: How to implement dynamic objects like RC6.DynObj?

    Quote Originally Posted by wqweto View Post
    Wait for Olaf to publish RC6 source code as promised then you can freely copy/paste it from his implementation.

    It's a non-trivial IDispatch customization, probably requiring a very fast container class like the hashsets implemented in RC6, so your waiting from the first point above will be double rewarded.

    cheers,
    </wqw>
    I don't need to consider the speed/performance of the code now, I just want to know how to implement "dynamic property names".

  4. #4
    PowerPoster
    Join Date
    Jun 2013
    Posts
    5,492

    Re: How to implement dynamic objects like RC6.DynObj?

    Quote Originally Posted by wqweto View Post
    Wait for Olaf to publish RC6 source code as promised then you can freely copy/paste it from his implementation.
    Well, not *that* freely - because RC6 will be opened under LGPL - later when a platform-independent compiler is ready...
    (so, any copied and adapted code would have to be published under the same license - or a compatible one)

    Quote Originally Posted by wqweto View Post
    It's a non-trivial IDispatch customization...
    It is relatively trivial, when the vbFriendly-Interfaces will be used
    (there is a CodeBank-entry for that, not dependent on RC5 or RC6).
    https://www.vbforums.com/showthread....BaseInterfaces

    Olaf

  5. #5
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Posts
    3,094

    Re: How to implement dynamic objects like RC6.DynObj?

    Quote Originally Posted by Schmidt View Post
    It is relatively trivial, when the vbFriendly-Interfaces will be used
    No idea what vbFriendly-Interfaces means here but obviously we have different definitions for "trivial" too :-))

    Trivial IMO would be to use VBA.Collection keyed on "property name". Reimplementing IDispatch as a light-weight object is non-trivial in this regard.

    cheers,
    </wqw>

  6. #6
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    4,596

    Re: How to implement dynamic objects like RC6.DynObj?

    This is what I would advice for a simple solution

    Just as a simple class: Class1
    Code:
    Option Explicit
    
    Public NamedValues As Collection
    
    Private Sub Class_Initialize()
      Set NamedValues = New Collection
    End Sub
    
    Private Sub Class_Terminate()
      Set NamedValues = Nothing
    End Sub
    In a form
    Code:
    Private Sub Form_Load()
      Dim c As Class1
      
      Set c = New Class1
      
      c.NamedValues.Add 123, "a"
      c.NamedValues.Add "xyz", "b"
      c.NamedValues.Add True, "c"
      
      Debug.Print c.NamedValues("a")
      Debug.Print c.NamedValues("b")
      Debug.Print c.NamedValues("c")
    End Sub

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2020
    Posts
    357

    Re: How to implement dynamic objects like RC6.DynObj?

    Hi Arnoutdv,

    At present, RC6.Collection can provide a better solution than your method, but it still cannot achieve the effect of RC6.DynObj.

    Code:
    Private Sub Form_Load()
        Dim obj As cCollection
        
        Set obj = New_c.Collection
        
        obj.Prop("a") = 123
        obj.Prop("b") = "xyz"
        obj.Prop("c") = True
        
        Debug.Print obj.Prop("a")
        Debug.Print obj.Prop("b")
        Debug.Print obj.Prop("c")
        
    End Sub
    The reason why I want to implement my own dynamic object is because RC6.DynObj still cannot meet my needs. I hope to achieve more features than RC6.DynObj.
    Last edited by SearchingDataOnly; May 27th, 2021 at 06:49 AM.

  8. #8
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    4,596

    Re: How to implement dynamic objects like RC6.DynObj?

    I still think you are using the wrong programming tool.
    You need functionality which is not available in VB6 to mimic JS.

    I would certainly spend more time in investigating how to develop in JS then trying to build a tool to mimic and validate JS functionality in runtime.

  9. #9
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    4,596

    Re: How to implement dynamic objects like RC6.DynObj?

    The props method is not to much code to simulate on a normal VB6 class
    Code:
    Private Sub Form_Load()
        Dim obj As Class1
        
        Set obj = New Class1
        
        obj.Prop("a") = 123
        obj.Prop("b") = "xyz"
        obj.Prop("c") = True
        
        Debug.Print obj.Prop("a")
        Debug.Print obj.Prop("b")
        Debug.Print obj.Prop("c")
    
    
        obj.Prop("a") = obj.Prop("a") + 100
        obj.Prop("b") = "abc"
        obj.Prop("c") = Not obj.Prop("c")
    
        Debug.Print "--"
        Debug.Print obj.Prop("a")
        Debug.Print obj.Prop("b")
        Debug.Print obj.Prop("c")
    End Sub
    Code:
    Option Explicit
    
    Private Declare Sub PokeLong Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, Optional ByVal Length As Long = 4)
    
    Private mProps As Collection
    
    Public Property Let Prop(Key As String, Value As Variant)
      If Exists(Key) Then mProps.Remove Key
      mProps.Add Value, Key
    End Property
    
    Public Property Get Prop(Key As String) As Variant
      If Exists(Key) Then Prop = mProps(Key)
    End Property
    
    Private Sub Class_Initialize()
    Set mProps = New Collection
    End Sub
    
    ' Based on "Get collection index by key" by LaVolpe
    Public Function Exists(Key As String) As Boolean
      Dim Ptr   As Long
      Dim CurKey  As String
      Dim PointerKey  As Long
      Dim Index As Long
      
      If Not mProps Is Nothing Then
        If mProps.Count > 0 Then
          PointerKey = StrPtr(CurKey)                 'save StrPtr
          PokeLong Ptr, ByVal ObjPtr(mProps) + 24     'first Ptr
          Index = 0                                   'walk items upwards From First
          Do
            PokeLong ByVal VarPtr(CurKey), ByVal Ptr + 16
            If CurKey = Key Then
              Exists = True
              Exit Do
            End If
            Index = Index + 1                         'next Index
            PokeLong Ptr, ByVal Ptr + 24              'next Ptr
          Loop Until Ptr = 0                          'end of chain
          PokeLong ByVal VarPtr(CurKey), PointerKey   'restore original StrPtr
        End If
      End If
    End Function
    Quote Originally Posted by SearchingDataOnly View Post
    RC6 provides a very magical object DynObj, which can dynamically add properties and methods like JavaScript objects, for example:

    Code:
    Private Sub Form_Load()
      Dim oDyn As Object
      Set oDyn = New_c.DynObj
          oDyn.a = 123
          oDyn.b = "xyz"
          oDyn.c = True
      
      'Ok, readout-tests:
      Debug.Print oDyn.a, oDyn.b, oDyn.c
      
    End Sub
    If I could use VB6 to achieve a feature/class similar to RC6.DynObj too, it'll greatly promote my work. I'd like to know if anyone other than Olaf has implemented a similar feature? If anyone could provide some suggestions and help, it would be greatly appreciated.
    You want to mix the functionality of DynObj with a normal class with "fixed" properties and methods?

  10. #10
    PowerPoster
    Join Date
    Feb 2006
    Posts
    22,501

    Re: How to implement dynamic objects like RC6.DynObj?

    While it may not lead to a solution in pure VB, maybe see IDispatchEx Interface.

  11. #11
    PowerPoster
    Join Date
    Feb 2006
    Posts
    22,501

    Re: How to implement dynamic objects like RC6.DynObj?


  12. #12
    PowerPoster
    Join Date
    Jun 2013
    Posts
    5,492

    Re: How to implement dynamic objects like RC6.DynObj?

    Quote Originally Posted by wqweto View Post
    No idea what vbFriendly-Interfaces means here but obviously we have different definitions for "trivial" too :-))
    With "relatively trivial", I meant *using* the functionality of the vbFriendlyInterfaces.dll, as demontrated here:
    https://www.vbforums.com/showthread....=1#post5397267

    Non-trivial is the Code which makes up vbFriendlyInterfaces.dll -
    "just using it" (Implementing vbIUnknown and vbIDispatch) is quite a bit easier - comparable to "using stable SubClassing-Helpers".

    @SDO
    If you want to "roll your own" DynObj-like thing - vbFriendly-Interfaces are integrated into the RC-libs directly as well

    So no need for self-compiling your own vbFriendlyStuff.dll from the CodeBank-Sources, when the RC-lib will be included in your Project anyways.
    The Main-VBI-Object is available via New_c.VBI.

    Olaf
    Last edited by Schmidt; May 27th, 2021 at 08:04 PM.

  13. #13

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2020
    Posts
    357

    Re: How to implement dynamic objects like RC6.DynObj?

    Quote Originally Posted by Arnoutdv View Post
    You want to mix the functionality of DynObj with a normal class with "fixed" properties and methods?
    I mistakenly thought that RC6.DynObj could not read multi-layer JSON strings, but now I found that my JSON string format was wrong, so now RC6.DynObj can meet my requirements.

    Quote Originally Posted by dilettante View Post
    While it may not lead to a solution in pure VB, maybe see IDispatchEx Interface.
    Very useful study materials, thank you very much, dilettante.

    Quote Originally Posted by dilettante View Post
    My memory is so bad, I completely forgot that I followed and participated in similar topics 2 years ago, and Olaf already gave me the answers I needed 1 year ago.

    Not long ago, I spent a month writing a code converter. After the project was completed, when I checked the code of my old projects occasionally, I found that I wrote an exact same code converter 2 years ago. This is really frustrating.
    Last edited by SearchingDataOnly; May 28th, 2021 at 06:02 AM.

  14. #14

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2020
    Posts
    357

    Re: How to implement dynamic objects like RC6.DynObj?

    Quote Originally Posted by Schmidt View Post
    @SDO
    If you want to "roll your own" DynObj-like thing - vbFriendly-Interfaces are integrated into the RC-libs directly as well

    So no need for self-compiling your own vbFriendlyStuff.dll from the CodeBank-Sources, when the RC-lib will be included in your Project anyways.
    The Main-VBI-Object is available via New_c.VBI.

    Olaf
    I mistakenly thought that RC6.DynObj could not read multi-layer JSON strings, but now I found that my JSON string format was wrong, so now RC6.DynObj can meet my requirements.

    In addition, it would be great if you could add RC6.Collection.SerializeToJSON5 and DynObj.toJSON5.
    Last edited by SearchingDataOnly; May 29th, 2021 at 07:31 AM.

  15. #15

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2020
    Posts
    357

    Re: [RESOLVED] How to implement dynamic objects like RC6.DynObj?

    Deleted ...
    Last edited by SearchingDataOnly; May 29th, 2021 at 07:28 AM.

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