So basically, my program logs you into a game with sockets, and once you log in the "Control Panel" button appears to modify some stuff in the game, like sending a message in game. The "Control Panel" button brings out all of these features through a new form. I will now provide you with a code snippet of Form1 opening Form2.
private void cpanel_btn_Click(object sender, EventArgs e)
{
Form2 cPanel = new Form2();
cPanel.Show();
}
As you can see, it brings up Form2. I am trying to get Form2 to communicate with Form1. Basically, Form2 can communicate with Form1 as far as running simple voids (functions)-- but that's about all. Here is Form2's constructor class. I gave it the public name "Main", and It's set with a "public Form1 main;" on the top of Form2's class. This is Form2's full class.
namespace WindowsFormsApplication2
{
public partial class Form2 : Form
{
public Form1 Main;
public Form2()
{
this.FormBorderStyle = FormBorderStyle.FixedSingle;
InitializeComponent();
this.Main = new Form1();
}
private void Form2_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
this.Main.sendGameData("Hello world!");
}
}
}
As you can see the button's void on the bottom of this class tries to communicate with Form1 and send some data. However, it runs the function, but let's look at Form1's "sendGameData" void.
public void sendGameData(string data)
{
data += "\x00";
byte[] game_data = Encoding.UTF8.GetBytes(data);
if (this.log_packets_opt) this.log("[SENT]: " + data);
this.game_socket.Send(game_data);
}
It runs the function, but it seems to run into an error with the bottom line of that void. Form2 can't run sendGameData because it doesn't have the "game_socket" public socket. This is a huge issue, because it is stopping my code from working. The game_socket is assigned into Form1 before anything big happens, and I don't want to have to reconstruct it because that'll overlap sockets and make you have to reconnect and stuff.
I'm wondering how I can make Form2 have full access to Form1, and have all of It's public variables that have already been set.
Oh, and the error I'm receiving is: Object reference not set to an instance of an object.