Results 1 to 6 of 6

Thread: Call a function depending on the variable name

  1. #1
    Join Date
    Mar 2010
    Posts
    200

    Call a function depending on the variable name

    Hello,
    I have a problem I can not solve it. When I'm in a loop, call a function based index of the loop. Is it possible to "concatenate" a generic name method with a variable to call a function? Or is there a function that calls the function whose name is passed as a parameter? I know it would have been easier to do (int count), but this is not the solution for me. If you know any more solution then please let me know.

  2. #2
    Join Date
    Nov 2009
    Posts
    518

    Re: Call a function depending on the variable name

    Hello,
    I think you can use the reflection, it is a good means to make this call dynamically (during program execution). But your question may be hiding another problem, why did not you choose the alternate way round? If you have a source code which you have tried then it would be great. The source code will help us to debug your problem quickly.

  3. #3
    Join Date
    Dec 2009
    Posts
    202

    Re: Call a function depending on the variable name

    Hello,
    I am also interested in this topic. I have no choice because in my loop, I have an object that contains a number of fields named String. In my loop, I run a number of times Y (Y is less than X). I could write a function that would test the value and return the good (method name (X)), but this would limit me because this is not 100% dynamic. If you can prove me some more API's then it would be great. Thanks in advance.

  4. #4
    Join Date
    Nov 2009
    Posts
    333

    Re: Call a function depending on the variable name

    Hello,
    By calling this function (String somestring) you pass as parameter the result of the function somestring and it is nothing other than a password parameter. And it works. But what you want to do is what I think is called "method pointer" that exists in languages like Delphi, but not java. And introspection allows you Inquiring for a class on its own type (reflectivity). I guess you understand what I am trying to say.

  5. #5
    Join Date
    Nov 2009
    Posts
    335

    Re: Call a function depending on the variable name

    Hello,
    I think a lot depends on the design? You have the right to change it? You recoveries where the "String" appointed to your function or method. Why can not you make a getter named methodname(int x), and then simply use integers in your loop? I think if you implement this in your code it will surely work. I hope this is the solution for your problem and if you have any more problems then you can post here.

  6. #6
    Join Date
    Nov 2009
    Posts
    583

    Re: Call a function depending on the variable name

    Hello,
    Otherwise another solution anyway as you've got to write your method, you made an abstract class that has a method. You create subclasses with the code that goes for your method and you Day1, Day2, etc.. You create an array of objects day and each cell of the table receives an instance of the class that corresponds to the index.
    Instance in Day1 box 1
    Mechanism Day2 in box 2.
    I hope this will help you.

Similar Threads

  1. How to pass PHP variable value to jquery function?
    By Kasper in forum Software Development
    Replies: 5
    Last Post: 03-03-2010, 06:41 PM
  2. Accessing a variable outside a function without arg
    By BoanHed in forum Software Development
    Replies: 3
    Last Post: 04-12-2009, 01:35 PM
  3. Function with a variable number of arguments
    By Chrisch in forum Software Development
    Replies: 3
    Last Post: 20-11-2009, 02:03 PM
  4. If variable is not initialized in main function
    By Jaisudha in forum Software Development
    Replies: 3
    Last Post: 18-11-2009, 02:13 PM
  5. How to Call a variable subroutine or function
    By Sayam in forum Software Development
    Replies: 2
    Last Post: 16-04-2009, 09:33 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,751,748,954.16515 seconds with 16 queries