1. Computing & Technology

Discuss in my forum

Dan Mabbutt

Object Initializers

By , About.com GuideSeptember 6, 2009

Follow me on:

While reviewing a new book, I noticed that I haven't written anything about the "object initializer" feature in VB.NET 2008. This Quick Tip makes up for that ommission. Click Here to read the tip.
Comments
September 8, 2009 at 2:05 am
(1) John McIlhinney :

The tip says:

“The idea is that you can initialize a complex data object without calling a constructor.”

This is not so because the ONLY way to create a class instance is to invoke a constructor. The fact that it may be the default constructor doesn’t make it any less a constructor.

The tip says:

“Object initializers also work when the object takes a parameter.”

Objects don’t take parameters though. Methods, including constructors, do.

You know what you meant and I know what you meant but the use of imprecise terminology is a good way to confuse those with less experience, either now or down the track. For instance, a beginner should know that every time they use the New keyword they are invoking a constructor. If we tell them that they’re not then that’s only going to lead to confusion.

September 8, 2009 at 2:32 pm
(2) Dan Mabbutt :

Point taken. I often get confused by the language in technical articles myself and I do try to make mine “precise”.

Sometimes it’s hard. For example …

Although in strict technical fact, you’re correct that instantiation of a class to create an object must call a constructor … somewhere … in this case, it’s not in any code that you can find in your project. (It’s not in one of the hidden files, for example.) So while it may be there in the IL code – and it’s a great theoretical construct to keep in mind – the actual experience of the programmer is that the object is initialized without calling a constructor.

Your second point is a better example of sloppy language, however. I’ll try to do better in the future.

September 13, 2009 at 12:03 pm
(3) Jim :

Dan,
I forgive you for your imprecise words, but the comment was correct. As an old guy trying to learn all these new fangled (well, they were new years ago…) programming constructs, I think it is important to use the right words and point out that constructors are “implicit” if need be to avoid confusing my already befuddled mind….

September 13, 2009 at 6:25 pm
(4) Atere-Roberts D. B. :

The link I clicked was supposed to take me to this spotlight topic: Formatting Strings, Numbers, and Other Objects. I seem to have got lost. please redirect me. Thank you.

September 14, 2009 at 8:51 am
(5) Scott Simpson :

I also got lost on a link that was supposed to show me string formatting etc and ended up here on an article for Object Initializers?

September 14, 2009 at 11:56 am
(6) visualbasic :

A googolplex of apologies! I put the newsletter links in myself so I don’t even have anyone to blame for this error.

The correct link is:

http://visualbasic.about.com/od/usingvbnet/a/formatting.htm

September 16, 2009 at 12:52 pm
(7) Aster :

Dear visualbasic;
now that you realize your mistake, how ABOUT initializing a constructive fix on that
Thank you very much, ABOUT and all your subcategories.

September 16, 2009 at 4:27 pm
(8) Dan Mabbutt :

Dear Aster,

Actually, I hadn’t actually planned on changing anything before now. While it is true that an object doesn’t take parameters and methods do, ninety-nine out of a hundred programmers will understand what is meant and that wasn’t the real message in the Quick Tip anyway.

But never let it be said that I refused to listen to readers. Just for you, I tweaked the language.

Leave a Comment

Line and paragraph breaks are automatic. Some HTML allowed: <a href="" title="">, <b>, <i>, <strike>

©2012 About.com. All rights reserved.

A part of The New York Times Company.