question about sub New() in a class

B

Bob

Hi,

I found this code here below (about cartitems and shoppingcart) and I have
two questions about sub New().

In the first class CartItem, there is two times sub New():
Public Sub New()
End Sub

and

Public Sub New(ByVal ProductID As Integer, ByVal ProductName As String, ...)
_productID = ProductID
_productName = ProductName
...
End Sub


In the second class WroxShoppingCart, there is one time sub New():
Public Sub New()
_items = New List(Of CartItem)
_dateCreated = DateTime.Now
End Sub

My questions are:
1) why an empty sub New() in class CartItem
2) why are there parameters defined in sub New() of class CartItem and not
in sub New() of class WroxShoppingCart?

Thanks
Bob

Part of code: (hopely this is enough)
---------------
Public Class CartItem
Private _productID As Integer
Private _productName As String
...

Public Sub New()
End Sub

Public Sub New(ByVal ProductID As Integer, ByVal ProductName As
String, ...)
_productID = ProductID
_productName = ProductName
...
End Sub

Public Property ProductID() As Integer
Get
Return _productID
End Get
Set(ByVal value As Integer)
_productID = value
End Set
End Property
....
End Class


Public Class WroxShoppingCart

Private _dateCreated As DateTime
Private _lastUpdate As DateTime
Private _items As List(Of CartItem)

Public Sub New()
_items = New List(Of CartItem)
_dateCreated = DateTime.Now
End Sub
End Class
 
T

Tom Shelton

Bob said:
Hi,

I found this code here below (about cartitems and shoppingcart) and I have
two questions about sub New().

In the first class CartItem, there is two times sub New():
Public Sub New()
End Sub

and

Public Sub New(ByVal ProductID As Integer, ByVal ProductName As String, ...)
_productID = ProductID
_productName = ProductName
...
End Sub


In the second class WroxShoppingCart, there is one time sub New():
Public Sub New()
_items = New List(Of CartItem)
_dateCreated = DateTime.Now
End Sub

My questions are:
1) why an empty sub New() in class CartItem

If you did not include the default constructor you would no longer be
able to create objects of type cartitem without passing parameters. In
other words:

Dim c As CartItem = New CartItem()

would no longer be valid. The compiler will only provide a default
constructor as long as you don't add your own. As soon as you add a
constructor to your class, then it's up to you to provide the default.
2) why are there parameters defined in sub New() of class CartItem and not
in sub New() of class WroxShoppingCart?

Because they are not needed in WroxShoppingCart... That may not sound
like much of an answer, but it is true. WroxShoppingCart is simply a
container for CartItem's. It provides methods to manipulate and store
cartitems. But, it doesn't need to know details of the individual
cartitems - it just needs to know that they are cartitems.
 
C

Cor Ligthert [MVP]

Bob,

I tell it in another way than Tom, he is assuming something more than you
maybe know.

The "Sub New" is the Constructor in VB.Net.
Every Class that has to be instanced (that is a class that has no Shared
Members) needs a constructor.

If you build a Form, than that is standard there and it is a good habbit to
leave a standard constructor in every class: Public Sub New() as it is done
in your sample.

However sometimes you want to give the class some information at startup,
that you can overload that constructer
Public Sub New(Byval one as integer, byval two as integer).

The compiler will see what the method looks like and takes the one that fits
the best (or shows an error as there is no fitted).

For the rest I leave it to the answer from Tom, because I would not write it
in another way.

Cor
..
 
T

Tom Leylan

Bob... I think Cor explained it well in his reply. I will add that you
would do well to read a book about OOP in general. It is also my
recommendation that you select a language agnostic book. There is nothing
wrong with reading about VB.Net but it is important to recognize that
object-orientation transcends any particular language and overloading (and
constructor overloading in this case) is something almost every OOP language
supports.

Software development isn't about a particular set of keywords, it's the
concepts that matter most.
 
B

Bob

Ok, thanks to both of you.
Happy newyear


Tom Leylan said:
Bob... I think Cor explained it well in his reply. I will add that you
would do well to read a book about OOP in general. It is also my
recommendation that you select a language agnostic book. There is nothing
wrong with reading about VB.Net but it is important to recognize that
object-orientation transcends any particular language and overloading (and
constructor overloading in this case) is something almost every OOP
language supports.

Software development isn't about a particular set of keywords, it's the
concepts that matter most.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top