Entwickler-Ecke

Basistechnologien - Mit return einen Funktionsaufruf machen


Pfammi - Mo 19.10.09 22:35
Titel: Mit return einen Funktionsaufruf machen
Hallo zusammen

Ich bin neu hier und hab gleich ne Frage xD.

Ich würde gerne eine Funktion schreiben, welche mir mit return eine Funktion startet.

Die Idee dahinter ist folgende: ich will in eine Consolenapplication einen Befehl eingeben. Dieser Befehl soll mit einem Array verglichen werden, in welchem alle Befehle gelistet sind. Stimmt der Befehl überein, soll die gleichnamige Funktion geöffnet werden.

Ich dachte mir es wäre am einfachsten, den Befehl mit (); zurück zu geben. Leider funktioniert das nicht.

So habe ich mir das gedacht...(geht aber nicht)

=====================Schnipp===================================================


C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
using System;

namespace funktionstest
{
  class Program
  {
    
  public static void functionA()
    {
      Console.WriteLine("functionA is started");
    }
    
    public static void functionB()
    {
      Console.WriteLine("functionB is started");
    } 
    
    public static void functionC()
    {
      Console.WriteLine("functionC is started");
    }
    
    static String func(string command)
    {
      string[] allCommands = new string[] { "functionA""functionB""functionC"};
      int length = allCommands.Length;
      bool valid = false;
      for(int a=0; a<length; a++)
      {
        if(command == allCommands[a])
        {
          valid = true;
          command = command + "();";
          return command;
        }
      }
        return "default";
    }
  
    public static void Main(string[] args)
    {
      String command;
      Console.Write("Insert command: ");
      command = Console.ReadLine();
      func(command);
      Console.Write("Press any key to continue . . .");
      command = Console.ReadLine();
    }
  }
}
===================schnapp============================================================

Ich hoffe ihr könnt mir helfen. Danke schon im voraus.

Pfammi


Th69 - Di 20.10.09 20:46

Da hast du dir aber etwas für einen Anfänger nicht gerade einfache Sache ausgedacht.

Du mußt erst mal den Unterschied zwischen einem String und den Namen im Source-Code verstehen. So einfach kann man keine Methoden aufrufen.

Die einfachste Möglichkeit wäre ein switch:

C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
for(int a=0; a<length; a++)
{
  if(command == allCommands[a])
  {
     switch(a)
     {
       case 0: functionA(); return;
       case 1: functionB(); return;
       case 2: functionC(); return;
     }
  }
}


Die fortgeschrittenere Variante funktioniert mit einem sog. "delegate". Such mal einfach danach...


Christian S. - Mi 21.10.09 13:12

Hallo!

Möglich ist das auch noch mittels Reflection:

C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
    using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace ConsoleApplication7
{
    public class Program
    {
        public static void functionA()
        {
            Console.WriteLine("functionA is started");
        }

        public static void functionB()
        {
            Console.WriteLine("functionB is started");
        }

        public static void functionC()
        {
            Console.WriteLine("functionC is started");
        }   

        static void Main(string[] args)
        {
            var methods = typeof(Program).GetMethods();

            string input;
            do
            {
                Console.Write("Methodenname eingeben (ESC zum Beenden): ");
                input = Console.ReadLine();

                if (input != "esc") {
                    var method = methods.FirstOrDefault(m => m.Name.ToLower() == input.ToLower() && m.IsStatic);
                    if (method != null)
                        method.Invoke(nullnull);
                }
            } while (input.ToLower() != "esc");
        }
    }
}


Grüße
Christian


Pfammi - So 25.10.09 21:25

Danke Christian.

Genau das habe ich gesucht