Syntax question

Supporting Visual Studio on Windows
dondiggy
Fresh meat
Posts: 4
Joined: Thu Nov 05, 2015 4:11 pm
Contact:

Syntax question

Postby dondiggy » Thu Nov 05, 2015 4:27 pm

I'm relatively new to programming in C#, and I was hoping someone might be able to give me some quick pointers. As I was going through the C# example for the 1057_2 quadrature encoder I noticed the following 3 statement listed below.

Phidgets.Encoder encoder; //Declare an encoder object
private ArrayList inputArray;
private ErrorEventBox errorBox;

A few lines later the Encoder, ArrayList, and ErrorEventBox objects are instantiated using the "new" keyword and the following variable names: encoder, inputArray, and errorBox. I don't understand the purpose of the 3 lines listed above if you need the "new" keyword to instantiate an object. I apologize if this isn't the right forum for a question like this, but really do appreciate any help that helps my progression.

Thank you!

User avatar
mparadis
Site Admin
Posts: 643
Joined: Fri Oct 28, 2011 12:17 pm
Contact:

Re: Syntax question

Postby mparadis » Fri Nov 06, 2015 8:40 am

The first line is merely declaring the variable; think of it as setting up an empty container or label for the object. When you use the "new" keyword, that's when the object is actually created and the object's constructor is called.

So, if you copied this line

Code: Select all

encoder = new Phidgets.Encoder();
to another place further down in the same instance, the constructor would be called again, the label "encoder" would refer to this new object, and the old object would be lost.

dondiggy
Fresh meat
Posts: 4
Joined: Thu Nov 05, 2015 4:11 pm
Contact:

Re: Syntax question

Postby dondiggy » Mon Nov 09, 2015 11:22 am

What's the point of declaring these variables without initializing them? Why not declare them where you intend to initialize them? Just curious.

Thank you for all the help! Your answer definitely helped!

User avatar
mparadis
Site Admin
Posts: 643
Joined: Fri Oct 28, 2011 12:17 pm
Contact:

Re: Syntax question

Postby mparadis » Tue Nov 10, 2015 8:29 am

Whether you do it in two steps or one is largely a matter of preference for how your code is organized. You can certainly do both in the same line to save space in the program, for example:

Code: Select all

Phidgets.Encoder encoder = new Phidgets.Encoder();


But the reason it requires the 'new' keyword when a primitive data type like an int or char does not, is because objects are like customized data types. The compiler already knows what an int is, but it doesn't know what a Phidgets.Encoder is. The 'new' keyword tells it to start up the class constructor, which defines all other variables and functions contained in the class.

Additionally, since objects can take up a variable amount of space, memory needs to be allocated when a new object is created, and recouped when the object is destroyed.

dondiggy
Fresh meat
Posts: 4
Joined: Thu Nov 05, 2015 4:11 pm
Contact:

Re: Syntax question

Postby dondiggy » Sat Nov 21, 2015 11:51 pm

I really appreciate the help thus far. It's really helped me progress. I've got another example below I was hoping someone might be able to help me with.

void encoder_Attach(object sender, AttachEventArgs e)
{
Phidgets.Encoder attached = (Phidgets.Encoder)sender;
}

What exactly is the statement above highlighted in blue doing? Is it instantiating the variable "attached" with the encoder object that had triggered the event of being attached?

Ultimately I'm really curious what the function of "(Phidgets.Encoder)sender" is? The parenthesis on the left of the the keyword "sender" is throwing me for a bit of a loop.

frodegill
Phidget Mastermind
Posts: 114
Joined: Thu Mar 04, 2010 2:51 am
Contact:

Re: Syntax question

Postby frodegill » Mon Nov 23, 2015 3:11 am

dondiggy wrote:void encoder_Attach(object sender, AttachEventArgs e)
{
Phidgets.Encoder attached = (Phidgets.Encoder)sender;
}

What exactly is the statement above highlighted in blue doing?

It is simply a type cast (sender is cast from the very generic class object to the more spesific class Phidgets.Encoder. Which is ok if sender was created as a Phidgets.Encoder (or a subclass of it))


Return to “C#.NET”

Who is online

Users browsing this forum: No registered users and 1 guest