PDA

View Full Version : [SOLVED] Calling custom class method through variable



khu
04-07-2014, 12:35 PM
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.


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.

Paul_Hossler
04-07-2014, 12:57 PM
There is the 'CallByName' function that executes a Class method

On line help has pretty good write up

Paul

khu
04-07-2014, 01:23 PM
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.

Paul_Hossler
04-07-2014, 05:02 PM
http://www.jkp-ads.com/articles/CustomFind.asp

Another good write up with examples

Paul

snb
04-08-2014, 09:45 AM
if the macro is part of the userform's codemodule this will suffice:


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

khu
04-08-2014, 10:04 AM
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.

snb
04-08-2014, 11:40 AM
Please do not quote.