0

I'm pretty new to this, so I'm sorry if I screw up any of the lingo. Thanks a bunch for anybody's help or thoughts.

I have the following wrong code:

Sub ExampleSub(text As String)
  ClassObject." & text & "_attribute = 1
End Sub

So if I call ExampleSub("cheese"), I would like it to set ClassObject.cheese_attribute equal to 1.

Any thoughts? I'm not even sure it's possible.

Thanks so much!

2
  • Pretty sure you cannot refer to an object with text qualifier like that. Your question presupposes an answer that is not really possible. Can you describe in more detail the structure of your data, the problem you're trying to solve, etc.? If we know more about what you're actually trying to do, there's probably a better solution available. Commented Apr 5, 2014 at 2:03
  • 2
    Look at CallByName - support.microsoft.com/kb/186143 Commented Apr 5, 2014 at 4:32

1 Answer 1

1

Here is another method that might work. Use a scripting dictionary object as one of the classobject's Properties. The Dictionary Object is pretty neat, storing elements in key/value pairs, where the key is a string and the value can be any other type (object, range, Workbook, integer, variant/array, etc.)

So you can use the dictionary object to contain all of these named attributes. In your class module, add code like:

Private pAttributes as Object

Sub Class_Initialize()
    '## Initialize this object to avoid a 91 error
    Set pAttributes = CreateObject("Scripting.Dictionary")
End Sub

Public Property Get Attributes() As Object
    Set Attributes = pAttributes
End Property
Public Property Let Attributes(lAttributes As Object)
    Set pAttributes = lAttributes
End Property

Then, in your code you can simply do:

Sub ExampleSub(text As String)
  ClassObject.Attributes(text) = 1
End Sub

Calling a dictionary key automatically adds the item if it doesn't already exist, but if you wanted more control you could do:

Sub AnotherExample(text as String)
    If ClassObject.Attributes.Exists(text) Then
        MsgBox text & " already exists!", vbInformation
    Else:
        ClassObject.Attributes(text) = 1
    End If
End Sub
Sign up to request clarification or add additional context in comments.

4 Comments

Very neat. Thank you! I've played around with the example code here and so far I see no reason this won't work. I'm going to give it a go in my actual project over the next couple days. If it works I'll close this case and mark it as solved. Thank you so much!
@Logan have you had a chance to review this? If so, please consider accepting this answer...
Sorry about the delay. Will do! Thanks so much for your help. This is a very powerful method!!
No worries, the reason I asked is because someone else had a similar question and I wanted to suggest this as a possible solution for them, too. Cheers!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.