C# Let multiple class share the same instance of another class

Viewed 12964

Is there any way to let multiple class share the same instance of another class? So in my c# program i've got three class Gamelogic(A), FPSController(B), and SpawnPlayer(C).

In my case class B and C would use and alter some variables in A, in order to use the variables i'm currently instantiating A in two different classes and then use dot notation to access the variable from the instance of A, but the problem is that after A instantiated in different classes, the change in instance.variable does not share between B and C class at all.

Would static type be a good way of solving this? Or writing a main would be better.

Any suggestions would be appreciated.

4 Answers

show, this my code.

public interface IMyInterface
{
    void SetData(String Data);
}


    public partial class Form1 : Form, IMyInterface
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Form2 frm = new Form2(this as IMyInterface);
        frm.Show();
    }

    public void SetData(String Data)
    {
        textBox1.Text = Data;
    }
}


public partial class Form2 : Form
{
    private IMyInterface frm = null;

    public Form2(IMyInterface frm)
    {
        InitializeComponent();

        this.frm = frm;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        frm.SetData(textBox1.Text);
    }
}
Related