Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Basics
|
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 30th, 2006, 09:45 AM
Friend of Wrox
 
Join Date: Apr 2006
Posts: 160
Thanks: 0
Thanked 0 Times in 0 Posts
Default question about sub New() in class

Hi,

i bought the book Beginining asp.net 2.0 and i downloaded the code. I have two questions about some code in chapter 13.

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

Part of code:
    <Serializable()> _
    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


    <Serializable()> _
    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



 
Old January 2nd, 2007, 04:30 AM
Registered User
 
Join Date: Dec 2006
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi ,

I can explain to u the Sub New of the CartItem Class.

The first New() is the default Constructor. This Constructor is invoked whenever you create an instance of the CartItem class without passing any parameters.

Example: Dim objCartItem as new CartItem

Sometimes a user of your class may not supply valid values to all the members of your class. In such cases the need arises that whenever an instance of a class is created , it be in a valid form ie. all its member variables have valid values . In such cases you define a constructor that accepts parameters like in the second case.

This way you ensure that your class members have valid values.

In such cases you create an object of the class as follows

Dim objCartItem as new CartItem(12,"Beginning ASP.NET",..)

If u don't supply any values the default constructor is invoked. However you don't have to create any default constructor as ASP.NET already has a default constructor. Hence the code:

Public Sub New()
End Sub

is not really required.
 
Old January 2nd, 2007, 04:41 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Quote:
quote:If u don't supply any values the default constructor is invoked. However you don't have to create any default constructor as ASP.NET already has a default constructor. Hence the code:

Public Sub New()
End Sub
This is only true when you have no constructors defined in your code. However, as soon as you add an overloaded constructor with parameters, no default constructor is created. So, if you want to have a default constructor with no parameters and one with one or more parameters, you have to explicitly specify both in your code...

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
 
Old January 2nd, 2007, 05:12 AM
Registered User
 
Join Date: Dec 2006
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Imar,

Can you explain to me the line:

Quote:
quote:_items = New List(Of CartItem)
What does it mean??
 
Old January 2nd, 2007, 05:18 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

It assigns the variable _Items a new generics list of CartItem objects.

Generics are new in .NET 2 and allow you to create strongly typed objects with generic code. In the example above, a strongly typed list is created that allows you to add, remove and edit CartItem objects. For example:

_List.Add(New CartItem())

(assuming that CartItem has a parameterless constructor)

Cheers,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
 
Old January 2nd, 2007, 01:38 PM
Friend of Wrox
 
Join Date: Apr 2006
Posts: 160
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi, thanks for replying.
I have still one question.
Why is that second constructor with parameters needed in the class CartItem?
(Public Sub New(ByVal ProductID As Integer, ByVal ProductName As String, ...)
            _productID = ProductID
            _productName = ProductName
           ...
       End Sub)

I can't see in the code of chapter 13 any instance creation with paramaters, only without paramaters (Dim NewItem As New CartItem()).

Here the whole code:
Imports Microsoft.VisualBasic
Imports System.Data
Imports System.Data.SqlClient
Imports System.Collections.Generic

Namespace Wrox.Commerce

    <Serializable()> _
    Public Class CartItem
        Private _productID As Integer
        Private _productName As String
        Private _productImageUrl As String
        Private _quantity As Integer
        Private _price As Double
        Private _lineTotal As Double

        Public Sub New()
        End Sub

        Public Sub New(ByVal ProductID As Integer, ByVal ProductName As String, ByVal ProductImageUrl As String, ByVal Quantity As Integer, ByVal Price As Double)

            _productID = ProductID
            _productName = ProductName
            _productImageUrl = ProductImageUrl
            _quantity = Quantity
            _price = Price
            _lineTotal = Quantity * Price

        End Sub

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

        Public Property ProductName() As String
            Get
                Return _productName
            End Get
            Set(ByVal value As String)
                _productName = value
            End Set
        End Property

        Public Property ProductImageUrl() As String
            Get
                Return _productImageUrl
            End Get
            Set(ByVal value As String)
                _productImageUrl = value
            End Set
        End Property

        Public Property Quantity() As Integer
            Get
                Return _quantity
            End Get
            Set(ByVal value As Integer)
                _quantity = value
            End Set
        End Property

        Public Property Price() As Double
            Get
                Return _price
            End Get
            Set(ByVal value As Double)
                _price = value
            End Set
        End Property

        Public ReadOnly Property LineTotal() As Double
            Get
                Return _quantity * _price
            End Get
        End Property

    End Class


    <Serializable()> _
    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


        Public Property Items() As List(Of CartItem)
            Get
                Return _items
            End Get
            Set(ByVal value As List(Of CartItem))
                _items = value
            End Set
        End Property

        Public Sub Insert(ByVal ProductID As Integer, ByVal Price As Double, ByVal Quantity As Integer, ByVal ProductName As String, ByVal ProductImageUrl As String)

            Dim ItemIndex As Integer = ItemIndexOfID(ProductID)

            If ItemIndex = -1 Then
                Dim NewItem As New CartItem()

                NewItem.ProductID = ProductID
                NewItem.Quantity = Quantity
                NewItem.Price = Price
                NewItem.ProductName = ProductName
                NewItem.ProductImageUrl = ProductImageUrl

                _items.Add(NewItem)
            Else
                _items(ItemIndex).Quantity += 1
            End If

            _lastUpdate = DateTime.Now()

        End Sub

        Public Sub Update(ByVal RowID As Integer, ByVal ProductID As Integer, ByVal Quantity As Integer, ByVal Price As Double)

            Dim Item As CartItem = _items(RowID)

            Item.ProductID = ProductID
            Item.Quantity = Quantity
            Item.Price = Price
            _lastUpdate = DateTime.Now()

        End Sub

        Private Function ItemIndexOfID(ByVal ProductID As Integer) As Integer

            Dim index As Integer

            For Each item As CartItem In _items
                If item.ProductID = ProductID Then
                    Return index
                End If
                index += 1
            Next

            Return -1

        End Function

        Public Sub DeleteItem(ByVal rowID As Integer)

            _items.RemoveAt(rowID)
            _lastUpdate = DateTime.Now()

        End Sub

        Public ReadOnly Property Total() As Double
            Get
                Dim t As Double

                If _items Is Nothing Then
                    Return 0
                End If

                For Each Item As CartItem In _items
                    t += Item.LineTotal
                Next

                Return t
            End Get
        End Property

    End Class

End Namespace

Thanks



 
Old January 2nd, 2007, 06:34 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

I can't answer that since I didn't write the book.

My take is that it's probably not needed so you can remove it. You can do a full compile (or Search the project) to see if it's used in one of the ASPX pages or their code behind files.

Maybe they added that for future additions to the site? It can be convenient to have a constructor that accepts arguments for all the (relevant) properties a class has....

Cheers,

Imar
---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Want to be my colleague? Then check out this post.
 
Old January 3rd, 2007, 02:10 PM
Friend of Wrox
 
Join Date: Apr 2006
Posts: 160
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Ok, thanks






Similar Threads
Thread Thread Starter Forum Replies Last Post
A question about abstract class nf0001384 C# 2005 5 July 22nd, 2013 06:58 AM
Question about value class type saadli BOOK: Ivor Horton's Beginning Visual C++ 2008 ISBN: 978-0-470-22590-5 4 December 3rd, 2008 10:15 PM
Question about the Name Value Class used BugBase mikener BOOK: ASP.NET 2.0 Instant Results ISBN: 978-0-471-74951-6 5 June 26th, 2008 07:04 AM
a question of class bitmap and graphic china911 ASP.NET 2.0 Basics 3 January 25th, 2008 05:44 PM
Question about Class Inheritance forkhead Pro VB.NET 2002/2003 0 March 21st, 2005 03:37 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.