I can´t load a function through a String array but only with "hardcoded" name, why?

a_la_l
edited November 2013 in Programming Questions

Hi,

i built a menu and need to load different functions in different classes. All works well when i load the function e.g. menu.showContent();

But when i want to load the function through an array like menuNames[i].toString().showContent(); i get The function does not exist.

With println i got the exact String (menu), i dont´t really know why this don´t work. Any suggestions?

Thanks, Andreas

Answers

  • Answer ✓

    this is like writing 'apple' on a piece of paper and expecting it to taste like an apple - what you have is the name of a class, not the class itself.

    you can instantiate a class by name and then use the methods of that class, but it's complicated and slow if you do it a lot.

    alternatively, store classes in your menuNames array. this may not be as flexible as you'd like.

  • thanks koogs, your example solved it, i was kind of blind...

  • Nice metaphor, reminds me of Magritte... "Ceci n'est pas une pipe"...

Sign In or Register to comment.