Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Experiencing different behavior between object initialization in declaration vs. initialization in constructor

Tags:

c#

winforms

This is a WinForms C# application. The following two snippits show two different ways of initializing an object. They are giving different results.

This works as expected:

public partial class Form1 : Form
{
  private CameraWrapper cam;
  public Form1()
  {
       cam = new CameraWrapper();
       InitializeComponent();           
  }

This does not work (details below):

public partial class Form1 : Form
{
  private CameraWrapper cam = new CameraWrapper();
  public Form1()
  {
       InitializeComponent();
  }

Inside CameraWrapper I am using a third-party SDK to communicate with a camera. I register with an event on the SDK which is called when results are available.

In case 1 (initialization inside constructor), everything works as expected and the event handler inside CameraWrapper gets called. In case 2, the event handler never gets called.

I thought that these two styles of object initialization were identical, but it seems not to be the case. Why?

Here is the entire CameraWrapper class. The event handler should get called after a call to Trigger.

class CameraWrapper
{
    private Cognex.DataMan.SDK.DataManSystem ds;
    public CameraWrapper()
    {
        ds = new DataManSystem();
        DataManConnectionParams connectionParams = new DataManConnectionParams("10.10.191.187");
        ds.Connect(connectionParams);

        ds.DmccResponseArrived += new DataManSystem.DmccResponseArrivedEventHandler(ds_DmccResponseArrived);
    }

    public void Trigger()
    {
        SendCommand("TRIGGER ON");
    }

    void ds_DmccResponseArrived(object sender, DmccResponseArrivedEventArgs e)
    {
        System.Console.Write("Num barcodes: ");
        System.Console.WriteLine(e.Data.Length.ToString());
    }

    void SendCommand(string command)
    {
        const string cmdHeader = "||>";
        ds.SendDmcc(cmdHeader + command);
    }
}
like image 622
Gareth Avatar asked Oct 23 '12 17:10

Gareth


People also ask

Is a constructor and initializer the same?

A constructor is a defined method on a type which takes a specified number of parameters and is used to create and initialize an object. An object initializer is code that runs on an object after a constructor and can be used to succinctly set any number of fields on the object to specified values.

What happens when an object is initialized?

Initialization and inheritance An object incorporates not only the fields explicitly declared in its class, but also those declared in its superclasses. To fully initialize an object, therefore, all instance variables declared in its class and in all its superclasses must be initialized.

What happens when an object is initialized in Java?

Object Initialization in Java The process of assigning a value of the variable is called initialization of state of an object. In other words, initialization is the process of storing data into an object. In the below example, we have initialized variables name and city with “PIET” and “Nagpur” respectively.

Why we initialize object in c#?

Object initializers let you assign values to any accessible fields or properties of an object at creation time without having to invoke a constructor followed by lines of assignment statements.


1 Answers

I thought that these two styles of object initialization were identical, but it seems not to be the case.

Not quite.

In the first case, the CameraWrapper constructor is called after the base class constructor for Form. In the second case, the CameraWrapper constructor is called, then the base class constructor, then the Form1 constructor body.

It's possible that something within the Form constructor affects the execution of the CameraWrapper constructor.

like image 130
Jon Skeet Avatar answered Oct 27 '22 03:10

Jon Skeet