473,785 Members | 2,412 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 WroxShoppingCar t, 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 WroxShoppingCar t?

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 WroxShoppingCar t

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 1708

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 WroxShoppingCar t, 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 WroxShoppingCar t?
Because they are not needed in WroxShoppingCar t... That may not sound
like much of an answer, but it is true. WroxShoppingCar t 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******** ********@TK2MSF TNGP02.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 WroxShoppingCar t, 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 WroxShoppingCar t?

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 WroxShoppingCar t

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******** ********@TK2MSF TNGP02.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 WroxShoppingCar t, 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 WroxShoppingCar t?

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 WroxShoppingCar t

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******** ******@TK2MSFTN GP02.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******** ********@TK2MSF TNGP02.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 WroxShoppingCar t, 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 WroxShoppingCar t?

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 WroxShoppingCar t

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******** ********@TK2MSF TNGP03.phx.gbl. ..
Ok, thanks to both of you.
Happy newyear
"Tom Leylan" <tl*****@nospam .netschreef in bericht
news:OD******** ******@TK2MSFTN GP02.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
recommendati on 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******* *********@TK2MS FTNGP02.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 WroxShoppingCar t, 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 WroxShoppingCar t?

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 WroxShoppingCar t

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.nlschre ef in bericht
news:Ot******** *****@TK2MSFTNG P03.phx.gbl...
Three,

:-)
"Bob" <.schreef in bericht
news:%2******** ********@TK2MSF TNGP03.phx.gbl. ..
>Ok, thanks to both of you.
Happy newyear
"Tom Leylan" <tl*****@nospam .netschreef in bericht
news:OD******* *******@TK2MSFT NGP02.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
recommendatio n 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****** **********@TK2M SFTNGP02.phx.gb l...
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 WroxShoppingCar t, 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 WroxShoppingCar t?

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 WroxShoppingCar t

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
1684
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
1669
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 pasting it here as well... enjoy! (you can skip to the source at the end of the message if you like...) Consider:
18
2083
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 the following design question. Obviously you need a stream to write the log file. Should this stream be created every time the log needs to be written, or should the stream be a member variable of the logging class and only be opened and closed...
9
1254
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 automatically, including chaining calls to Dispose. (There is no Dispose pattern)" but Dispose can thrown an exception. Is the exception supressed?
6
2119
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 responsible for instantiating the orders class? Would it be the ui layer or the master class in the business layer? thanks,
2
1609
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 ASP.NET I would like to add this to the Session object whilst using SQLServer as the session storage. Unfortunately, as this required the RCW class to be serializable, this won't work, and throws an exception saying you can't Serialize the Session...
3
1538
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 objects etc as Business layer classes and then have an data access layer which actually save / updates / delete to and from the DB.. If so, and bearing in mind the 'easiest' way to pass data between the layers would be with a dataset - should my BL...
6
1771
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 the individual elements in an array of ImageList's could be identified by the ID, thereby allowing re-ordering the array without harm. A person could identify by index into the array, but that would not be preserved by re-ordering (and re-ordering...
7
4474
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 to be checking that the correct data type is used DataAcess sets an abstract class and methods
6
1708
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 class structure:\ Top level Objects: Companies Employees
0
9645
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10341
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10155
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8979
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7502
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6741
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5383
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3656
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2881
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.