Wrox Programmer Forums
|
BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3
This is the forum to discuss the Wrox book Beginning ASP.NET 3.5: In C# and VB by Imar Spaanjaars; ISBN: 9780470187593
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 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 October 2nd, 2009, 01:51 PM
epc epc is offline
Registered User
 
Join Date: Jul 2008
Posts: 7
Thanks: 4
Thanked 0 Times in 0 Posts
Default Chapter 5 Pages 182 to 183 Constructors

Two part question:

1. When I created a C# class file in the App_Code folder I was able to see this code within the file which includes the default constructor.

Public class ClassName
{
Public ClassName()
{

}
}

When I created a similar class file for VB I did not see anything that looked like
the code on page 182 listed below.

Public Class Class Name
Public Sub New()

End Sub
End Class

Book states the VB constructor will be created by the compliler. Can I see this somewhere in code?

2. Why does the VB code use Sub before the constructor

Public Sub New()

but void is not used before the c# constructor code? Seems like the vb code is creating a subroutine.

Any thoughts would be helpful. Thanks!
 
Old October 2nd, 2009, 02:39 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

Quote from the book:
Quote:
By default, when you create a new class in Visual Web Developer, you get a default constructor for C# but not for VB.NET.
This is purely a template issue. That is, in C#, the constructor is added, in VB.NET it isn't. The code on page 182 is to show how a default constructor looks but doesn't reflect the New Class template.

This means that in VB.NET, the following are functionally the same:

Code:
' Without constructor Public Class Person
End Class
' With constructor Public Class Person Public Sub New() End Sub
End Class
In C#, this is the same:

Code:
 
// Without constructor public class Person {
} // With constructor
public class Person { public Person() { }
}
So, the difference between the two is purely the default code that is added by the Class template.



Quote:
Book states the VB constructor will be created by the compliler. Can I see this somewhere in code?
Not directly, But you can see it when instantiating a class without an explicit constucor works. E.g.:

Code:
' Without constructor Public Class Person
End Class
Public Class SomeOtherClass Public Sub SomeMethod Dim myPerson As New Person() ' works because of the default constructor. End Sub End Class
Quote:
Seems like the vb code is creating a subroutine.

Yes, but because it uses the reserved New keyword, the compiler knows you're defining a constructor. It's probably a better question for the C# and VB.NET compiler teams, but my guess is that the grammar for VB.NET somehow requires the Sub word, while C# doesn't....



Hope this clarifies things.

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
The Following User Says Thank You to Imar For This Useful Post:
epc (October 11th, 2009)





Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with starting on master pages chapter 6 italianjob BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 5 September 29th, 2009 06:45 PM
On page 182 tedr BOOK: ASP.NET 3.5 Enterprise Application Development with Visual Studio 2008: Problem Design Solutio 0 March 28th, 2009 07:19 PM
Errata page 182 tpangborn BOOK: Ivor Horton's Beginning Visual C++ 2005 0 May 21st, 2006 09:19 PM
Chapter 9 Protecting Your Pages malhyp Dreamweaver (all versions) 1 August 20th, 2005 04:14 AM





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