C#
C#

help

Root Question Message

Pdawg
Pdawg10/24/2022
Calling a function from a string.

Hey, everyone! I have a timer that will run every 100ms, but I have a list of functions named "f1", all the way up to "f255". I know it sounds impractical to even have all of those functions, but I need them. Anyway, is it possible to have an int++ to call the next function on the list? I want it to do this every time the timer runs:

1: add 1 to the framei int.
2: add the 'f' at the start of the function name, then attach framei after it
3: call the function

Expected behavior:
100ms elapsed,
f1();
another 100ms elapsed
f2();
and so on.

Here is an example of the code that doesn't work:
Ero
Ero10/24/2022
why would you possibly need all those functions?
Pdawg
Pdawg10/24/2022
I'm doing a challenge. Run bad apple by manually updating a pixel grid that was created in XAML
Pdawg
Pdawg10/24/2022
Each frame is a function, setting pixels that need to be set
Pdawg
Pdawg10/24/2022
I told you it's impractical, but I want to do it
Pdawg
Pdawg10/24/2022
So is it possible?
Ero
Ero10/24/2022
i feel like this would be better of in one single method honestly
mrphil2105
mrphil210510/24/2022
What do these methods do?
Ero
Ero10/24/2022
print a bunch of full size blocks i guess
mrphil2105
mrphil210510/24/2022
If they just print the same thing but with a different number then use one method with a parameter
Pdawg
Pdawg10/24/2022
Manually updates each pixel on a pixel grid
Pdawg
Pdawg10/24/2022
I haven't actually implemented that yet...since I need this to work first
mrphil2105
mrphil210510/24/2022
It is possible to call a function knowing its string, but I am not sure that's the solution here
Ero
Ero10/24/2022
"bad apple" is a song with a music video. people have started playing said music video on weird stuff
Ero
Ero10/24/2022
just like "running DOOM on a calculator" or whatever
Pdawg
Pdawg10/24/2022
Yup. Precisely. I have the frontend working, just not the backend
Pdawg
Pdawg10/24/2022
How would I do that?
Pdawg
Pdawg10/24/2022
mrphil2105
mrphil210510/24/2022
Don't in this case
Pdawg
Pdawg10/24/2022
This is the app btw. That black square is actually made up of many small squares
mrphil2105
mrphil210510/24/2022
Calling a function by it's name is only necessary when you have no other option
mrphil2105
mrphil210510/24/2022
It's slow and kind of "bad code"
Pdawg
Pdawg10/24/2022
Would it still hit that 100ms mark?
Pdawg
Pdawg10/24/2022
Like execute within 1ms
Ero
Ero10/24/2022
you probably want like
Ero
Ero10/24/2022
a list
Ero
Ero10/24/2022
or if you must, a dictionary
Pdawg
Pdawg10/24/2022
This is a 3532 frame video or something like that
Pdawg
Pdawg10/24/2022
So fun
Ero
Ero10/24/2022
and just access the frame via the index/key
Pdawg
Pdawg10/24/2022
Oh, so like
framei++;
var dict = new Dictionary<int, Action>
    {
        { 1, () => Console.WriteLine("Case 1") }
        { 2, () => Console.WriteLine("Case 2") }
        { 3, () => Console.WriteLine("Case 3") }
    }
Pdawg
Pdawg10/24/2022
Kinda
Ero
Ero10/24/2022
List<string> frames = new()
{
  "",
  ...
}
Ero
Ero10/24/2022
i would just do this?
Ero
Ero10/24/2022
and then do
Console.WriteLine(frames[frame]);
Pdawg
Pdawg10/24/2022
Wouldn't going through a list every time the timer hits be slower though?
Ero
Ero10/24/2022
what do you mean?
Pdawg
Pdawg10/24/2022
Than just directly generating the call every time
Ero
Ero10/24/2022
no?
Ero
Ero10/24/2022
it will be so much faster
Ero
Ero10/24/2022
you don't create the list anew every time
Pdawg
Pdawg10/24/2022
Ohh, okay. That makes sense
Pdawg
Pdawg10/24/2022
I'll try this out
Pdawg
Pdawg10/24/2022
Thank you
pip
pip10/24/2022
just for education's sake, the way that you're suggesting(the very bad way) might look something like
classObject.GetType().GetMethod("namehere").Invoke(classObject, null); or something
Ero
Ero10/24/2022
private static int _frame;
private static readonly List<string> _frames = new List<string>()
{
  // ...
};

private void Timer_Tick(object sender, EventArgs e)
{
  Console.WriteLine(_frames[_frame++]);
}
Ero
Ero10/24/2022
it's called reflection, if you still care to look it up
Ero
Ero10/24/2022
you probably don't need the GetType call
typeof(YourClass)
.GetMethod($"f{_frame++}", BindingFlags.Static | BindingFlags.NonPublic)
.Invoke(null, null);
pip
pip10/24/2022
oh cool, that works too
Ero
Ero10/24/2022
but yeah, reflection is incredibly slow, and incredibly bad code
ContactFrequently Asked QuestionsJoin The DiscordBugs & Feature RequestsTerms & Privacy