PDA

View Full Version : Why does VB .net have a constructor?


Onan the Clumsy
12th Apr 2006, 00:02
I'm really having trouble coming to terms with this... Why do I have to type:

Dim custDS As DataSet = New DataSet("CustomerOrders")

why isn't it just

Dim custDS As DataSet
Set custDS = ("CustomerOrders")

Why does it need a New in there?

I mean Dim is a declaration, why doesn't that declare a new thingie for me?

:confused:

drauk
12th Apr 2006, 09:34
The Dim is a declaration of the fact that a variable ('custDS') is a reference to a dataset object. It could be that one dataset that you're just about to create, it could be another dataset that you create later on, or it could be one you've already created elsewhere. Building a load of wooden pigeon holes doesn't create the letters that you put in them, rather just gives you space to put the letters when they arrive.

"New" creates an INSTANCE of an object. Hence you need the declaration and the constructor if what you want is a new instance of an object and a reference to it.

As to:

Dim custDS As DataSet
Set custDS = ("CustomerOrders")

The first line is fine; you're making a variable which will be a reference to a dataset. But in the second line, how does the interpreter know exactly what you want to create? You might suggest that it could infer it from the type of custDS (which it knows is "DataSet"), but it might be another type of object which descends from a DataSet; how can it know unless you tell it? That's polymorphism at work and is a powerful feature of object oriented languages.

MyData
12th Apr 2006, 16:29
drauk. That's a bloody good way of explaining this, I shall keep it up my sleeve for future reuse...

Onan the Clumsy
13th Apr 2006, 14:00
Thanks. I'm mulling that over. I think I might have to forget a lot of mainframe development experience before I become fully comfortable in this new environment. I'm used to a declaration being all you need:

01 WS-VARIABLE PIC X(8).

:O

MyData
14th Apr 2006, 13:25
Onan

I managed to avoid mainframes in my previous life as a code monkey (but did dabble on VAXs for a while).

My upbringing was on C and then C++. Although I program in VB.NET these days for ease of use to knock up prototypes I do miss the rigour and structure that C/C++ enforced in that if you did something implicitly then it might compile, it might execute, but later down the line it would come back to bite you.

VB tends to be a happy medium of easy to understand syntax, but sometimes it allows the programmer to do things the easy way and makes assumptions on your behalf.