title: c# - make public an object created inside a function
body: <p>How can I use the object "communicator" created in main(), in the callback function? The PacketCommunicator class doesn't have a constructor.</p>
  
  <pre><code>class Program
  {
      //public static PacketCommunicator communicator = new PacketCommunicator(); - can't do this; PacketCommunicator constructor doesn't exist
      //public static PacketCommunicator communicator = null; - this gives "Object reference not set to an instance of an object"
      public static void Main(string[] args)
      {
          PacketCommunicator communicator = selectedDevice.Open(...)
          communicator.ReceivePackets(0, PacketHandler);
      }
  
      // Callback function invoked by ReceivePackets for every incoming packet
      private static void PacketHandler(Packet packet)
      {
          communicator.SendPacket(packet);
      }
  }
  </code></pre>
  
  <p>The library I'm using is <a href="https://pcapdotnet.codeplex.com/" rel="nofollow">Pcap.Net</a>.
  Basically, what I'm trying to do is to capture packets on one interface, perform NAT on them, and send them on another interface.</p>