473,513 Members | 2,359 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

question about sub New() in a class

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

Dec 30 '06 #1
6 1690

Bob wrote:
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

Dec 30 '06 #2
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
..


"Bob" <.schreef in bericht news:%2****************@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

Dec 30 '06 #3
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:%2****************@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

Dec 30 '06 #4
Bob
Ok, thanks to both of you.
Happy newyear
"Tom Leylan" <tl*****@nospam.netschreef in bericht
news:OD**************@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:%2****************@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


Dec 30 '06 #5
Three,

:-)
"Bob" <.schreef in bericht news:%2****************@TK2MSFTNGP03.phx.gbl...
Ok, thanks to both of you.
Happy newyear
"Tom Leylan" <tl*****@nospam.netschreef in bericht
news:OD**************@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:%2****************@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



Dec 30 '06 #6
Bob
sorry ... three

"Cor Ligthert [MVP]" <no************@planet.nlschreef in bericht
news:Ot*************@TK2MSFTNGP03.phx.gbl...
Three,

:-)
"Bob" <.schreef in bericht
news:%2****************@TK2MSFTNGP03.phx.gbl...
>Ok, thanks to both of you.
Happy newyear
"Tom Leylan" <tl*****@nospam.netschreef in bericht
news:OD**************@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:%2****************@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





Dec 30 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
1672
by: Kurt | last post by:
i just can't figure out why something im doing is not working correctly.... public interface IInterface { int someProperty { get; set; }
18
1634
by: Kamen Yotov | last post by:
hi all, i first posted this on http://msdn.microsoft.com/vcsharp/team/language/ask/default.aspx (ask a c# language designer) a couple of days ago, but no response so far... therefore, i am...
18
2026
by: Nick Z. | last post by:
I am writing a reusable class for logging. My goal is to make it as fast and as robust as possible, while keeping memory usage to the lowest. All members are static. For some reason I'm stuck on...
9
1241
by: Hasani \(remove nospam from address\) | last post by:
I was reading a ppt ( http://www.gotdotnet.com/team/pdc/4064/tls310.ppt ) and came aross this statement. "Users can leverage a destructor. The C++ compiler generates all the Dispose code...
6
2107
by: rodchar | last post by:
Hey all, I'm trying to understand Master/Detail concepts in VB.NET. If I do a data adapter fill for both customer and orders from Northwind where should that dataset live? What client is...
2
1595
by: Chris Puncher | last post by:
Hi. I have a RCW class that was generated by the VS.NET2003 IDE when I added a COM dll reference to my project. This all works fine when I call methods on it. My initial problem is that in...
3
1519
by: Ken H | last post by:
Hi I have a question about architecting solutions.. I have a part of a project which requires me to track person details (name, addresses, etc... Should I be creating Person objects, Address...
6
1748
by: Peter Oliphant | last post by:
I just discovered that the ImageList class can't be inherited. Why? What could go wrong? I can invision a case where someone would like to add, say, an ID field to an ImageList, possible so that...
7
4454
by: jason | last post by:
In the microsoft starter kit Time Tracker application, the data access layer code consist of three cs files. DataAccessHelper.cs DataAcess.cs SQLDataAccessLayer.cs DataAcccessHelper appears...
6
1695
by: RSH | last post by:
I am still trying to grasp the use of real world Objects and how to conceptualize them using a business scenerio. What I have below is an outline that I am wrestling with trying to figure out a...
0
7265
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7545
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7539
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5692
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5095
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3240
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1605
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
807
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
461
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.