Win a copy of Micro Frontends in Action this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

passing method names as string

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Everyone,

i have a situation where I'm passing method name as a string. i need to call this method on a object.

let me make myself clear..

lets say there is a object bankdetails. it has like 100 methods method1(), method2(),...and so on
i have a string object which gets method names like "method1" or "method2" or..
i need to check this string value and call bankdetails.method1 or bankdetails.method2 or... so on based on this string's value.
one way of doing this would be to use multiple if then else statements to see the values and call the appropriate method. in this case if the object has 100 methods then i need to write 100 statements to check for this...
like (if methodname == "method1" then call bankdetails.method1 else if methodname == "method2" then call bankdetails.method2 ..)

i surely feel there should be another way of doing this..

thanks in advance
arun
 
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You must use the reflection API for this.
Here is a very simple example to execute your methods by name.
Here the bankDetails methods to be executed have no arguments and return nothing.

Regards
 
Popeye has his spinach. I have this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic