Discussion Locked

Converting string value as Method name

0 helpful
8 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

Suppose I have a object, say "obj". Now, if I declare a variable as "var name=getClients" . Now , I want to call the method specified in the variable "name" on the object "obj"!!!

Wanna resolve a string into method name!

Regards,
Rajiv.

8 replies

by sl-campbell

I would reconsider the way that you going about trying to do something like this.

At most you could create another method, and call this passing the variable value (a constant would be better), and in that method do a Select Case to determine what method of the object needs to be called, and then call it.
If you put this extra method in the object class it self, then you will not need to do anything extra, but if the object is not one that you created yourself, then you will either need to put the object in a class and add the extra method into the class, or put the extra method into a public module and pass the object to it as well.

I would rather consider creating constants or an Enum with a list of actions and assign the variabletype as the Enum.

Enum enumMETHODS
eMETHOD1
eMETHOD2
End Enum

Public myMethod (lMethod as enumMETHODS, obj As myObject)

Select Case lMethod
case eMETHOD1
obj.Method1
'replace Method1 with an actual method
case eMETHOD2
obj.Method2
'replace Method2 with an actual method

End Select
End Sub

by rajspace+newsletter

I get it. The reuqirement is something like this.
In a xml file, I will have requestId, componentName and methodName. for a particular request id, I will create a component as mentioned in componentName and then call the method as mentioned in methodName!!! ur suggestion wud not help this requirement.

by nise_k

Hi Rajiv,

There are atleast 2 ways u could do this :

1. It is sure that the no. of possible methods are limited. Because u have to first declare them before calling. So, as in the previous answer, u have to declare a switch-case construct, andresolve method names through that. This would be a static binding, i.e changes in no. of methods would demand changes in the cases.

2. For dynamism.
a. Assume that there's this file callOne.java which receives var value.

b. And, methodCollection.java has all the methods declared in it.

c. Now create a method resolve(methodName) inside callOne.java, which would create an object of "methodCollection" and using java.lang.reflect find out the proper method

e.g
Method []methodObjs= methodCollectionObject.getClass().getMethods();

Loop through the methodObjs, find the method and execute it.

Use java.lang.reflect.Method and java.lang.Class

Nise

by rajspace+newsletter

Oops! I just read my query again and realised that I hadnt mentioned the language Iam working on! I want this in ASP/VBScript! Incidently, I know it in Java!

by siggih

Well I know how to do this in Visual Basic.
I haven't tried it in VBScript.
Use the function CallByName.

function signature:
Result = CallByName(Object, ProcedureName, CallType, Arguments())

See MSDN help for more specifics..

by alnikolov

In VBScrip/ASP you can use any of the following:

1. Execute , where is any valid string expresion containing VBScript statements or string variable containing VBScript statements.

2. [result = ]Eval(expression), here result is optional and if you do not need it it is better to use Execute.

3. setTimeout(vCode, iMilliSeconds, sLanguage)

If you want to execute the statement in the Global name space you can use ExecutGlobal

In you case if using Execut, you shuol ddo something like this:
Execute "obj.getClients"

This discussion is closed to new replies.