Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Is this a limitation of VB.NET?
Message
General information
Forum:
ASP.NET
Category:
Other
Miscellaneous
Thread ID:
00713932
Message ID:
00713992
Views:
18
Hi David,

Yes, you are correct that VB .NET does not allow you to call the base constructor anywhere but the first line of code.

Here is the reason why it doesn't work:

The VB.NET Lang. Spec. section 7.2 says:

"Note that variable initializers are transformed into assignment
statements, and that these assignment statements are executed after
the invocation of the base class constructor. Such ordering ensures
that all base instance variables are initialized by their variable
initializers before any statements that have access to the instance
are executed"


>I can only put the call to the base constructor as the first line. Anywhere else, I get a compile error message: "constructor call is valid only as the first statement in an instance constructor". In C# I can put it anywhere.
>
>
>Public Class BaseClass
>    Public Sub New()
>
>    End Sub
>End Class
>
>Public Class MyDerivedClass
>    Inherits BaseClass
>
>    Public Sub New()
>        Dim x As Int32 = 0
>        x = x + 1
>        MyBase.New() ' This wont work, have to put it on firt line of the method
>    End Sub
>
>End Class
>
>
>Does anyone know the design decision behind this one?
-----------------------------------------

Cathi Gero, CPA
Prenia Software & Consulting Services
Microsoft C# / .NET MVP
Mere Mortals for .NET MVP
cgero@prenia.com
www.prenia.com
Weblog: blogs.prenia.com/cathi
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform