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