Re: VB 2008: Option Strict On + Infer On at class level
- From: "Armin Zingler" <az.nospam@xxxxxxxxxx>
- Date: Wed, 12 Dec 2007 05:10:49 +0100
I don't understand. In your case the assignment is missing, so it can't
work. My case is unambiguous:
Public Class Stuff
dim x= "string"
End Class
What does the compiler prevent from declaring x As String? It complains that
Option Strict requires an As clause. That's true unless Option Infer is On
and also the declaration line contains an assignment. Both preconditions are
met, so I don't see a reason for the error.
In other words, everyone who reads the source code knows how x must be declared in this case. Are there any doubts? If the compiler does not recognize the same, it's a flaw.
"Kelly Ethridge" <kelly@xxxxxxxxxxxxxxxxx> schrieb
I don't understand your response. The var keyword is not valid at
the class level.
As for the OP question, there is no way to determine at compile time
what Dim x at the class level should be when it could be initialized
from multiple location within the class.
Public Class Stuff
Dim x
Public Sub New()
x = 17
End Sub
Public Sub New(ByVal s As String)
x = s
End Sub
Public Sub Append(ByVal s As String)
x &= s
End Sub
End Class
Which datatype should the anonymous type be when used in Append? The
compiler can't infer it.
Tom Shelton wrote:
> On 2007-12-11, Armin Zingler <az.nospam@xxxxxxxxxx> wrote:
> > Hi,
> >
> > after dealing with the new possiblities of the current VB
> > version, I again and still wonder why Option Infer can not be
> > used with Option Strict On at class level, i.e. when declaring a
> > field:
> >
> > Dim x = 17
> >
> > Now I found out that the same line does work within a procedure.
> > Actually I wanted to ask why, but now I see that it is probably
> > because the line is realy split into declaration and assignment:
> >
> > private x
> >
> > sub new
> > x = 17
> > end sub
> >
> > This partially explains the error message, but shouldn't the
> > compiler still be clever enough to implicitly declare variable x
> > As Integer? It should be able to infer the type from the
> > expression just like within a procedure. That's what Option
> > Infer is all about. The compiler does have all the information.
> > I don't see the problem.
> >
> > The only explanation I have is that it internally first splits
> > the source code line into these two parts
> > (declaration+assigment) while loosing the type information.
> > Avoidable, IMO.
> >
> >
> > Armin
> >
>
> Probably why they added the var keword in C#...
>
> var x = 17;
>
.
- Follow-Ups:
- Re: VB 2008: Option Strict On + Infer On at class level
- From: Kelly Ethridge
- Re: VB 2008: Option Strict On + Infer On at class level
- References:
- VB 2008: Option Strict On + Infer On at class level
- From: Armin Zingler
- Re: VB 2008: Option Strict On + Infer On at class level
- From: Tom Shelton
- Re: VB 2008: Option Strict On + Infer On at class level
- From: Kelly Ethridge
- VB 2008: Option Strict On + Infer On at class level
- Prev by Date: Re: VB 2008: Option Strict On + Infer On at class level
- Next by Date: Re: VB 2008: Option Strict On + Infer On at class level
- Previous by thread: Re: VB 2008: Option Strict On + Infer On at class level
- Next by thread: Re: VB 2008: Option Strict On + Infer On at class level
- Index(es):
Relevant Pages
|
Loading