-1

I have 1000 methods called method0001, method0002 , ... ,method1000. I have a variable that takes values between 1 and 1000.

If the value of the variable is x, I'd like to call methodx. For instance, if the value of the variable is 34, I'd like to call method0034. How can I code this in C# please?

Many people are asking what is need for Methodwxyz. Every method is a different type of math question.

i've done this, following the helpful comments but am getting errors (edited the question from earlier)

  using System.Collections.Generic;
 using UnityEngine;
  public class TextControl : MonoBehaviour
{
public static TextControl instance;

void Start()
{
   instance = this;
}
// Update is called once per frame
void Update()
{


        this.GetType().GetMethod("Template00" + "1").Invoke(this, null);





}
public static string Templates001()
{
    // doing something here
} 
 }  

thanks

buffalo1
  • 23
  • 3
  • 2
    Use a switch statement, or a dictionary with a delegate type. However this seems a little suspicious, the first question i would ask, is why do you have 1000 methods called methodN – TheGeneral Sep 22 '19 at 00:26
  • Yeah seems like the design for the application needs a lot of refactoring. I think reflection would be best given this case. – GregN Sep 22 '19 at 00:33
  • Your solution to whatever problem you have seems likely to be wrong if you have 1000 methods called Method0001, MethodXXXX, Method1000. I imagine there is a much better way to achieve whatever you are trying to do. Without seeing your code or knowing what these methods do, it's difficult to advise. – ProgrammingLlama Sep 22 '19 at 01:04
  • Many people are asking what is need for Methodwxyz. Every method is a different type of math question that takes different types of random variables to generate these questions – buffalo1 Sep 22 '19 at 01:11
  • As folks have mentioned, you really have two choices, a big switch statement (I'd use something like Excel to write the code and then copy/paste it into VS) or Reflection. Reflection will be slower. But, it will be faster if you create a `Dictionary`. The int will be your method number, and the MethodInfo will refer to the method you want to call. Reflect over all the methods, building the dictionary, at startup. Then, at run time, use the dictionary to pick out the right MethodInfo to `Invoke` – Flydog57 Sep 22 '19 at 01:34
  • Looks like you misplaced your method name in the string: Templates not Template. Also the comment from @Flydog57 is correct. This does it at runtime which will be slower over multiple interactions. Since Update is called once per frame in unity, you may want to create a Dictionary to search. The performance impacts of this will be noticeable. – GregN Sep 22 '19 at 02:06
  • Another alternative would be to initialize a `Dictionary>`, again using Excel as a code generator. Probably similar in speed to a big switch statement, but perhaps a little cleaner code (since it would look as if it's table driven) _(I believe, but I'm not sure, that once a switch gets big enough, it gets compiled as if it were a dictionary)_ – Flydog57 Sep 22 '19 at 03:41

1 Answers1

1

You could do this through reflection. Edit for a quick sample (forgot invoke parameters). Some tips about reflection:

  • If you get a nullexception that means it can't find the method
  • The method you are invoking needs to be public
  • If you use obfuscation you may not have the same names for the method

Code

public class Program
{
    public static void Main(string[] args)
    {
        Check method1 = new Check(1);
        Check method2 = new Check(2);
    }
}

public class Check
{
    public Check(int x)
    {
        this.GetType().GetMethod("Method" + x).Invoke(this, null); 
    }

    public void Method1()
    {
        Console.WriteLine("Method 1");
    }

    public void Method2()
    {
        Console.WriteLine("Method 2");
    }
}
GregN
  • 142
  • 1
  • 13
  • ty but i get the error: "NullReferenceException: Object reference not set to an instance of an object" can you advise? ty – buffalo1 Sep 22 '19 at 01:29
  • This means the method you are looking to find doesn't exist. I would post additional code showing how you are trying to call the method from what point. More than likely, you are searching for a method in the wrong location. – GregN Sep 22 '19 at 01:30
  • added the code in the question. – buffalo1 Sep 22 '19 at 01:42