Consulting

Results 1 to 7 of 7

Thread: Calling custom class method through variable

  1. #1
    VBAX Regular
    Joined
    Jun 2013
    Posts
    16
    Location

    Calling custom class method through variable

    I've built a custom class and I'm providing the user a combobox that contains the class methods so they may select the desired output. There are instances of the class stored in a dictionary (nodedata) where I can return data from dictionary(item).method for whatever item, or method in the item class.

    I have a userform with 3 comboboxes that identify what to search for: Node Number, Dataset, and Parameter. The syntax for getting an item from the dicitonary is -- nodedata("Node#_dataset").parameter --. The dictionary works fine and I can manually input the what node, dataset and parameter I want to return (i.e. nodedata("node4_scenarioA").x_coord will give me the corresponding data), but when I try and run this through a userform, I'm having issues calling the method. The code below should show a message box with the data the user requested.

    HTML Code:
    With UserForm1
        LookUpVal = CStr("node" & .TextBox1.Text & "_" & .ComboBox1.Text) 'creates the "Node#_dataset" format for the dictionary key
        parameter = .ComboBox2.value
    End With
    
    MsgBox(nodedata(lookupval).parameter)
    If I manually put the parameter I want (i.e. x_coord), this would work, but leaving the method as a variable "parameter" is giving me problems. I'm wondering if it has to do with the data type being variant/string, but I'm not sure how to get it into a readable method type. I know that when I call a method in the code window I do not put quotes around .parameter, so I'm assuming this is the hangup.

    I'm fairly new to classes as well. Any help is appreciated, thanks.

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    There is the 'CallByName' function that executes a Class method

    On line help has pretty good write up

    Paul

  3. #3
    VBAX Regular
    Joined
    Jun 2013
    Posts
    16
    Location
    Thanks Paul, that worked great. I had come across that function before but the write-up I saw was for adding in runtime, did not realize it also housed a 'get' method.

  4. #4
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    http://www.jkp-ads.com/articles/CustomFind.asp

    Another good write up with examples

    Paul

  5. #5
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    if the macro is part of the userform's codemodule this will suffice:

    LookUpVal = "node" & TextBox1.Text & "_" & ComboBox1.Text

  6. #6
    VBAX Regular
    Joined
    Jun 2013
    Posts
    16
    Location
    Yeah, the LookUpVal variable would work to identify the instance of my class stored in the dictionary, but calling a method through '.parameter' was giving me issues. I'm not sure if it's a datatype problem or what, but typing out '.stress' or '.x_coord' would execute the correct method. I'm sure vba tries to find 'parameter' as the method and can't find it in the class. CallByName seems to handle the variable differently.
    Last edited by khu; 04-08-2014 at 01:27 PM.

  7. #7
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    Please do not quote.

Tags for this Thread

Posting Permissions

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