Autor Beitrag
Pfammi
Hält's aus hier
Beiträge: 6

Win 7 ultimate

BeitragVerfasst: Mo 19.10.09 22:35 
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===================================================

ausblenden volle Höhe 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
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Moderator
Beiträge: 4764
Erhaltene Danke: 1052

Win10
C#, C++ (VS 2017/19/22)
BeitragVerfasst: 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:
ausblenden 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.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 20451
Erhaltene Danke: 2264

Win 10
C# (VS 2019)
BeitragVerfasst: Mi 21.10.09 13:12 
Hallo!

Möglich ist das auch noch mittels Reflection:
ausblenden volle Höhe 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

_________________
Zwei Worte werden Dir im Leben viele Türen öffnen - "ziehen" und "drücken".
Pfammi Threadstarter
Hält's aus hier
Beiträge: 6

Win 7 ultimate

BeitragVerfasst: So 25.10.09 21:25 
Danke Christian.

Genau das habe ich gesucht