0

I'm writing a small CLI program in C# using Object Oriented Programming. I have some Logic Classes as well as interfaces and Controllers to update the User Interface. As far as I unserstand I should add a reference in the UI class for the controller, is this considered common and safe to do?

My Controller uses both the main program Stregsystem as well as the UI StregsystemCLI, If I want to create a reference of the controller I need both an instance of the program and UI, which need a copy from the controller... getting stuck here.

static void Main(string[] args)
    {
       IStregsystem stregsystem = new Stregsystem(); 
       IStregsystemUI ui = new StregsystemCLI(stregsystem);
       StregsystemController controller = new StregsystemController(stregsystem, ui);

        controller.StregsystemUI.Start();
    }

Stregsystem is the name of the application. My Questions goes as follows: How do I use the functions inside the controller after I started the program in the last line of main?

I don't want to create a new controller opject, since I already have that in main. I want to use a ParseCode() Function in the StregsystemCLI class (UI). Neither do I want a static class, as far as i know, because I work with non-static functions in that function.

public void Start()
{
    Console.WriteLine("Welcome");

    ParseCommand(); 'StregsystemController.ParseCommand() Dosent Work'
}

The Controller:

class StregsystemController
    {
        public IStregsystem Stregsystem { get; }
        public IStregsystemUI StregsystemUI { get; }

        private readonly Dictionary<string, Action<string[]>> adminCommands;

        public StregsystemController(IStregsystem stregsystem, IStregsystemUI stregsystemUI)
        {
            Stregsystem = stregsystem;
            StregsystemUI = stregsystemUI;

            //Add admin commands
            adminCommands = new Dictionary<string, Action<string[]>>();
            adminCommands.Add(":q", args => stregsystemUI.Close());
        }
Lilith
  • 11
  • 2
  • Does this answer your question? [What's a "static method" in C#?](https://stackoverflow.com/questions/4124102/whats-a-static-method-in-c) – Jesse Good Dec 29 '21 at 21:24
  • Sounds like you are just starting out with programming. If I understand your question correctly, you want to call a method without creating an instance, which is why I linked to the question about static methods. – Jesse Good Dec 29 '21 at 21:26
  • Can you post your controller too pls? – Serge Dec 29 '21 at 21:42
  • Added the Controller to the question. The function I want to call will perform some string formatting and call other functions, So as far as I know I can't mark it as static? – Lilith Dec 29 '21 at 21:55
  • A StregsystemUI knows nothing of a StregsystemContoller; the UI cannot call a method of the controller. The controller knows of the UI, but not the other way round – Caius Jard Dec 29 '21 at 21:58
  • Would it be common/secure to led the UI class have a reference to the controller module? – Lilith Dec 29 '21 at 22:00

0 Answers0