Wrox Programmer Forums

Need to download code?

View our list of code downloads.

| FAQ | Members List | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old October 2nd, 2009, 01:51 PM
epc epc is offline
Registered User
 
Join Date: Jul 2008
Location: Milwaukee, Wisconsin, USA
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!
Reply With Quote
  #2 (permalink)  
Old October 2nd, 2009, 02:39 PM
Imar's Avatar
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 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!
Reply With Quote
The Following User Says Thank You to Imar For This Useful Post:
epc (October 11th, 2009)
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 12:08 AM.


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