title: declaring global variables from within a function?
body: <p>Here's my code:</p>
  
  <pre><code>    private void connect()
      {
          try
          {
              textBox1.Text += "connecting... \r\n";
              button1.Enabled = false;
  
              DuplexChannelFactory&lt;IfaceClient2Server&gt; cf =
                      new DuplexChannelFactory&lt;IfaceClient2Server&gt;(
                          new CallbackImpl(),
                          new NetTcpBinding(),
                          new EndpointAddress("net.tcp://localhost:9080/service"));
  
              IfaceClient2Server srv = cf.CreateChannel();
              srv.StartConnection(name); /// need to make this one global!
              textBox1.Text += name + "connected!\r\n"; 
          }
          catch
          {
              button1.Enabled = true;
              textBox1.Text += "error connecting!\r\n";
          }
      }
  
  
      private void sendMsg()
      {
          srv.Message_Cleint2Server("Hello!");
          /// srv doesn't exist here.
      }
  </code></pre>
  
  <p>as you can see, the server object ( srv ) is being declared from with the connect() function, yet of course I need this object to be global, so that I would be able to access it from other functions, such as sendMsg as well.</p>
  
  <p>What do you think might be the best way to do this?</p>
  
  <p>Thanks!</p>