Home All Groups Group Topic Archive Search About

question about sub New() in a class

Author
30 Dec 2006 5:28 PM
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

Author
30 Dec 2006 6:08 PM
Tom Shelton
Bob wrote:
Show quoteHide quote
> 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.

--
Tom Shelton
Author
30 Dec 2006 6:23 PM
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
..






Show quoteHide quote
"Bob" <.> schreef in bericht news:%23c1F9fDLHHA.2236@TK2MSFTNGP02.phx.gbl...
> 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
>
>
>
Author
30 Dec 2006 6:41 PM
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.


Show quoteHide quote
"Bob" <.> wrote in message news:%23c1F9fDLHHA.2236@TK2MSFTNGP02.phx.gbl...
> 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
>
>
>
Author
30 Dec 2006 7:02 PM
Bob
Ok, thanks to both of you.
Happy newyear


Show quoteHide quote
"Tom Leylan" <tleylan@nospam.net> schreef in bericht
news:ODulTIELHHA.2236@TK2MSFTNGP02.phx.gbl...
> 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.
>
>
> "Bob" <.> wrote in message news:%23c1F9fDLHHA.2236@TK2MSFTNGP02.phx.gbl...
>> 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
>>
>>
>>
>
>
Author
30 Dec 2006 8:22 PM
Cor Ligthert [MVP]
Three,

Show quoteHide quote
:-)


"Bob" <.> schreef in bericht news:%237y2WUELHHA.4376@TK2MSFTNGP03.phx.gbl...
> Ok, thanks to both of you.
> Happy newyear
>
>
> "Tom Leylan" <tleylan@nospam.net> schreef in bericht
> news:ODulTIELHHA.2236@TK2MSFTNGP02.phx.gbl...
>> 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.
>>
>>
>> "Bob" <.> wrote in message
>> news:%23c1F9fDLHHA.2236@TK2MSFTNGP02.phx.gbl...
>>> 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
>>>
>>>
>>>
>>
>>
>
>
Author
30 Dec 2006 11:11 PM
Bob
sorry ... three

Show quoteHide quote
"Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> schreef in bericht
news:OtRMe$ELHHA.780@TK2MSFTNGP03.phx.gbl...
> Three,
>
> :-)
>
>
> "Bob" <.> schreef in bericht
> news:%237y2WUELHHA.4376@TK2MSFTNGP03.phx.gbl...
>> Ok, thanks to both of you.
>> Happy newyear
>>
>>
>> "Tom Leylan" <tleylan@nospam.net> schreef in bericht
>> news:ODulTIELHHA.2236@TK2MSFTNGP02.phx.gbl...
>>> 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.
>>>
>>>
>>> "Bob" <.> wrote in message
>>> news:%23c1F9fDLHHA.2236@TK2MSFTNGP02.phx.gbl...
>>>> 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
>>>>
>>>>
>>>>
>>>
>>>
>>
>>
>
>