Re: VB 2008: Option Strict On + Infer On at class level



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;
>

.



Relevant Pages

  • Re: VB 2008: Option Strict On + Infer On at class level
    ... What does the compiler prevent from declaring x As String? ... That's true unless Option Infer is On ... Public Sub New ...
    (microsoft.public.dotnet.languages.vb)
  • Re: problem with function
    ... Hence any attempt to write into memory past the end of oldName would ... you are declaring newName inside the ... after oldName so the compiler is not warning you when you overwrite ... If you declare a dummy string just after oldName in your second ...
    (alt.comp.lang.learn.c-cpp)
  • Re: wchar_t as a string parameter
    ... The bottom line is that there is no setting for this, and there is no known compiler bug that causes this to happen. ... I agree totally on my bad form of declaring my string variable wchar_t ... MyFunction(szMyString); ...
    (microsoft.public.vc.mfc)
  • Threading and AddressOf in VB.Net
    ... AddressOf reflectionBegin) ... Public Sub reflectionBegin(ByVal aString As String) ... When i don't include paramaters compiler says signature not the same. ...
    (microsoft.public.dotnet.general)
  • Re: Overloading 101 quesiton
    ... What about the ByRef keyword? ... Public Sub SetText(ByRef x As String) ... That is used when declaring a method, but you can't use ByRef as a ...
    (microsoft.public.dotnet.languages.csharp)

Loading