Click here to Skip to main content
15,899,754 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
down vote favorite


I have a Xmpp client connection named x in my Form1.I want to use that in other Form of the project.

i have 7 Forms.But i have defined the connection in Form1 only.

how can i use that in other Form..

Following is connection i defined in Form1

C#
public Form1()
    {
        InitializeComponent();
    }
       XmppClientConnection x;


When i click a button on Form1 it opens Form2.I also defined to use x in Form2 using:

C#
public static string sendtext = "";
        private void button3_Click(object sender, EventArgs e)
    {
        sendtext = textBox1.Text;
        Form2 form = new Form2(x);
        form.Show();

    }
Posted
Comments
Member 10579673 1-May-14 23:16pm    
the error i am getting is

{"Object reference not set to an instance of an object."}

1 solution

when you call second form your object may be null, you can create new object if it is null.

C#
if(x==null)
{
   x= new XmppClientConnection();
}

Form2 form = new Form2(x);
form.Show();
 
Share this answer
 
Comments
Member 10579673 2-May-14 0:33am    
Thanks For your solution.It helped me but not as u defined.
i defined the new connection in Form2.

public partial class Form2 : Form
{
private XmppClientConnection ansh;
public Form2(XmppClientConnection ansh)
{
InitializeComponent();

if (x == null)
{
x = new XmppClientConnection();
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900