Why does VB .net have a constructor?
I'matightbastard
Thread Starter
Join Date: Jul 2001
Location: Texas
Posts: 1,747
Likes: 0
Received 0 Likes
on
0 Posts
Why does VB .net have a constructor?
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?
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?
Join Date: Sep 2002
Location: London, UK
Posts: 778
Likes: 0
Received 0 Likes
on
0 Posts
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.
"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.
I'matightbastard
Thread Starter
Join Date: Jul 2001
Location: Texas
Posts: 1,747
Likes: 0
Received 0 Likes
on
0 Posts
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).
01 WS-VARIABLE PIC X(8).
Fixed+Rotary (aircraft, not washing lines)
Join Date: Jan 2005
Location: Peak District, Yorkshire, UK
Age: 56
Posts: 357
Likes: 0
Received 0 Likes
on
0 Posts
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.
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.