2

I'm pretty used to VBA, not that much for Objects though and I'm hitting a wall right now...

My config class has almost 100 properties, so I'll not spam them here as the details doesn't really matter for my question.

I hoped to code a duplicate function, to create multiple objects from one and then assign different values for a specific property of each new objects (add new elements to the configurations, so it generates new configs), that would look like this :

Public Function Duplicate(SrcCfg As Config, PropertyName As String, Properties As String) As Collection
Dim Cc As Collection, _
    Cfg As Config, _
    TotalNumber As Integer, _
    A() As String

Set Cc = New Collection
A = Split(Properties, "/")
TotalNumber = UBound(A)

For i = 0 To TotalNumber
    'Create a copy of the source object
    Set Cfg = SrcCfg.Copy
    'Set the property for that particular copy
    Cfg.PropertyName = A(i)
    'Add that copy to the collection
    Cc.Add ByVal Cfg
Next i

    Duplicate = Cc
End Function

But I'm not sure that a collection is the best output (as I'll take the results and incorporate them into another master collection), so I'm open to suggestions.

And I'm pretty sure that we can't pass a Property as an argument (I spent quite some times looking for a solution for this...) and I don't know what to do about it as this would be super practical for me. So if there is a solution or a workaround, I'll gladly try it!

Here is the rest of my methods :

Friend Sub SetConfig(SrcConfig As Config)
    Config = SrcConfig
End Sub

Public Function Copy() As Config
    Dim Result As Config
    Set Result = New Config
    Call Result.SetConfig(Config)
    Set Copy = Result
End Function


Final code to duplicate object :


Working smoothly :

Private Cfg As Config

Friend Sub SetConfig(SrcConfig As Config)
    Set Cfg = SrcConfig
End Sub

Public Function Copy() As Config
    Dim Result As Config
    Set Result = New Config
    Call Result.SetConfig(Cfg)
    Set Copy = Result
End Function

Public Function Duplicate(PropertyName As String, Properties As String) As Collection
Dim Cc As Collection, _
    Cfg As Config, _
    TotalNumber As Integer, _
    A() As String

Set Cc = New Collection
A = Split(Properties, "/")
TotalNumber = UBound(A)

For i = 0 To TotalNumber
    'Create a copy of the source object
    Set Cfg = Me.Copy
    'Set the property for that particular copy
    CallByName Cfg, PropertyName, VbLet, A(i)
    'Add that copy to the collection
    Cc.Add Cfg
Next i

    Set Duplicate = Cc
End Function
R3uK
  • 14,417
  • 7
  • 43
  • 77
  • Maybe see Scott's answer here: http://stackoverflow.com/questions/4805475/assignment-of-objects-in-vb6/4805812#4805812 – Tim Williams Jul 17 '15 at 16:17
  • @TimWilliams : Thx, I got started with this answer, to create the `.copy` method and it was indeed really useful! – R3uK Jul 17 '15 at 16:51

1 Answers1

3

You actually got it right, including the types (String).

Just replace your

Cfg.PropertyName = A(i)

with

CallByName Cfg, PropertyName, vbLet, A(i)

The property name must be passed as a string, not a reference or a lambda or anything, so no type safety or compiler aid here. You will have a runtime error if you misspell the name.

As for the return type, VBA does not have lists, so a collection is generally fine, but because in your particular case you know in advance how many objects you will be returning, you can declare an array:

Dim Cc() As Config
ReDim Cc(1 to TotalNumber)

You could declare an array in any case, but if you didn't know the total number, you'd be reallocating it on every iteration.

R3uK
  • 14,417
  • 7
  • 43
  • 77
GSerg
  • 76,472
  • 17
  • 159
  • 346
  • Thx for your quick answer, I love to read that it IS possible! <3 My weekend will be far more enjoyable! :) I'll try this soon and validate answer then if that's not a problem for you! – R3uK Jul 17 '15 at 16:48
  • I tested it and it worked awesomely! :) Thx for input and I'll post my updated code so that it'll be clearer for people when they come – R3uK Jul 20 '15 at 10:26
  • Just a little question as I'm not sure about this, but I use a custom object as argument and yet it is an object method, so can I change `Set Cfg = SrcCfg.Copy` to `Set Cfg = Me.Copy` and get rid of the argument? It seems pretty logical but it is the first time I'm really working with VBA objects – R3uK Jul 21 '15 at 12:34