SHARE
TWEET

Untitled

a guest Jan 20th, 2019 51 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // this code works fine.
  2. public sealed class MyObject
  3. {
  4.     static ExtDeviceDriver devDrv;
  5.  
  6.     public MyObject()
  7.     {
  8.         devDrv = new ExtDeviceDriver();
  9.     }
  10.  
  11.     public void Connect()
  12.     {
  13.         devDrv.connect();  
  14.     }
  15. }
  16.  
  17. //this code causes exception.
  18. public sealed class MyObject
  19. {
  20.     static ExtDeviceDriver devDrv;
  21.  
  22.     public MyObject()
  23.     {
  24.         // do not create devDrv here.
  25.         //devDrv = new ExtDeviceDriver();
  26.     }
  27.  
  28.     public void Connect()
  29.     {
  30.         if (devDrv == null)
  31.             devDrv = new ExtDeviceDriver();
  32.         devDrv.connect();  
  33.     }
  34. }
  35.    
  36. public class MyObject
  37.  {
  38.    static ExtDeviceDriver devDrv;
  39.  
  40.   static MyObject()
  41.   {
  42.     devDrv = new ExtDeviceDriver();
  43.   }
  44.  
  45.   public void Connect()
  46.   {
  47.     devDrv.connect();  
  48.   }
  49.  }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top