简体   繁体   中英

Access Form Method from another static class

So, I'm pretty much out of clues by now, not even sure if it's possible after all. I have a Visual C# Form, which gets run by the Program.cs (Standard way - VS did all the setup work of course).

In addition to that, I have a class with a static method in a seperate C# file, just because I like keeping one class in one file.

My form code has a public function:

public void print(String text)
{
    rtb_log.appendText("\n" + text);
}

At a certain point of time, I'm calling the static function from the other class.

Is it possible, to actually access that print method from my other class? Since it's referring to rtb_log ( a rich text box), it's only availible if instanced, and of course not static. But since static methods can only access static members, I'm a little out of ideas here on how to append some text on my form from another class.

Any help here?

But since static methods can only access static members, I'm a little out of ideas here on how to append some text on my form from another class.

Static members can access instance members - they just need to know which instance to call the method on. So you could write:

public static void Foo(OtherForm myOtherForm)
{
    // Do some stuff...
    myOtherForm.Print(); // Case changed to comply with naming conventions
}

Then when you call the method, you need to supply a reference to the relevant form. Basically something has to determine which instance you want to call Print on. Work out who has that information, and pass it on from there. I would recommend against using a static variable to hold this information. (Global state makes code less reusable, harder to reason about, and harder to test.)

EDIT: Given the comments, it sounds like you want:

// Within the form
private void HandleClick(object sender, EventArgs e)
{
    SomeClass.StaticMethod(this);
}

See below

class SomeMainClass
{
    private ClassB form = null;

    private void SomeMethod()
    {
        form = new ClassB();
        form.Show();
        ClassA foo = new ClassA(this);
    }

    // Use an accessor.
    public ClassB Form
    {
        get { return this.form; }
    }
}

class ClassA
{
    private SomeMainClass mainClass = null;

    // Constructor.
    public ClassA(SomeMainClass _mainClass)
    {
        this.mainClass = _mainClass;
    }

    private void SomeMethod()
    {
        this.mainClass.Form.Print("Something to print");
    }
}

class ClassB : Form
{
    // Constructor.
    public ClassB()
    {
        InitializeComponent();
    }

    public void Print(String text) 
    {     
        rtb_log.appendText("\n" + text); 
    } 
}   

Edit: This is a basic methodology in response to your comment. It is not that efficent in terms of resources but does what you want...

I hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM