C# pointer - Static calling
My short code :
class A
{
private B b;
public void b(){};
}
class B
{
private void a()
{
A.b();
}
}
I can make void b in A st开发者_Python百科atic but I has some Controls like this.Controls.Add();
They must be static too, but I don't know how to implement it, can you show me or do we have a better way for this solution :)
You could either do
class B {
private readonly A instance;
public B(A instance) { this.instance = instance; }
private void a() {
instance.b();
}
}
or
class B {
private void a(A instance) {
instance.b();
}
}
depending on what it is that you are trying to do.
(Note that you need to rename either B b
or void b
in A
.)
To use control in static function, the control have to be static too.
private static Button StaticButtonObject = new Button();
public static void AddControl()
{
StaticButtonObject.Text = "Button";
}
精彩评论