-3

I have the following code in the same TSDK class

public Int16 Write_To_Consol_dr(string ConsolCmd)
        {
        textBoxConsol.AppendText(ConsolCmd + "\n");
        textBoxConsol.AppendText("Tena_Consol>");
        }

public static Int16 Write_To_Console(string ConsolCmd)
        {
            TSDK.FormMain Clasi = new TSDK.FormMain();
            Clasi.Write_To_Consol_dr(ConsolCmd);
        }

I am not seeing anything in the Textbox. Thanks

Sharon
  • 31
  • 7
  • Where are you displaying Clasi? – KiwiPiet Dec 08 '15 at 02:12
  • Clasi is just an instantiation for the Write_To_Consol_dr() – Sharon Dec 08 '15 at 02:19
  • yes it is, but you are not displaying it, so how do you expect to see the ConsolCmd text? – KiwiPiet Dec 08 '15 at 02:23
  • textBoxConsol is an open textBox, I am not sure I need to do show() all the time. When I am in a non static method in the same class, I can write into the text box and it will show the text. – Sharon Dec 08 '15 at 02:32
  • You are instantiating a new TSDK.FormMain in Write_To_Console which by the way creates a new instance of textBoxConsol. If you want to see the ConsolCmd in an existing textBoxConsol, you need to use an existing instance of TSDK.FormMain, not create a new one. – KiwiPiet Dec 08 '15 at 02:38
  • You are correct, thats exactly the problem I am trying to solve. I have many static methods that need to be able to write information into a console textBox. How can they all access this textBox? – Sharon Dec 08 '15 at 02:43
  • From the sounds of it your form is a singleton. One solution is to store your form instance in a static variable (be careful though singletons for everything is bad!), there are other solutions of varying complexity. – KiwiPiet Dec 08 '15 at 02:48
  • How do you store a textBox or Form in a singleton? – Sharon Dec 08 '15 at 02:57

2 Answers2

0

A static class is basically the same as a non-static class, but there is one difference: a static class cannot be instantiated. In other words, you cannot use the new keyword to create a variable of the class type. Because there is no instance variable, you access the members of a static class by using the class name itself. (reference to https://msdn.microsoft.com/en-us/library/79b3xss3.aspx)

This one also can help you

Community
  • 1
  • 1
Vijay Kumbhoje
  • 1,401
  • 2
  • 25
  • 44
0

Some code to explain. This is not good practice code, but it will achieve what you are asking for.

public class FormMain : Form {
    public static FormMain Instance = new FormMain();
    public void WriteToConsoldr(string consolCmd)
    {
        textBoxConsol.AppendText(consolCmd + "\n");
        textBoxConsol.AppendText("Tena_Consol>");
    }

    public static void WriteToConsole(string consolCmd)
    {
        Instance.Write_To_Consol_dr(consolCmd);
    }
}    

public class OtherClass 
{
    public void SomeMethod()
    {
        FormMain.WriteToConsole("Some text");
        // Or access the instance method directly
        FormMain.Instance.WriteToConsoldr("Some text");
    }
}
KiwiPiet
  • 1,304
  • 14
  • 21
  • I was trying to send the info through FormMain.Instance.Write_To_Console_dr(Globals.CommandOut); But nothing shows in the textBox, is it possible that the textBox dont belong to the instance we created? – Sharon Dec 08 '15 at 03:16