VB 2008:Option Strict On + Infer On在课堂上 [英] VB 2008: Option Strict On + Infer On at class level

查看:83
本文介绍了VB 2008:Option Strict On + Infer On在课堂上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




在处理了当前VB版本的新可能性之后,我再次

并且仍然想知道为什么Option Infer不能用于Option Strict On

在课堂上,即在声明一个字段时:


Dim x = 17


现在我发现同一行在一个过程中起作用。其实我想要b $ b想问为什么,但现在我看到这可能是因为这行是

真的分成了声明和作业:


私人x


sub new

x = 17

end sub


这部分解释了错误信息,但编译器是否仍然不能足够聪明地隐式声明变量x As Integer?它应该是

能够像在一个过程中一样从表达式中推断出类型。

那就是Option Infer的全部内容。编译器确实拥有所有

信息。我没有看到问题。


我唯一的解释就是它内部首先将源代码分成这两部分(声明)同时丢失了

类型的信息。可以避免,IMO。

Armin

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

推荐答案

2007-12-11,Armin Zingler< az *******@freenet.dewrote:
On 2007-12-11, Armin Zingler <az*******@freenet.dewrote:




处理了新的可能性之后当前的VB版本,我再次

并且仍然想知道为什么Option Infer不能与Class Strict一起使用

在类级别,即在声明字段时:


Dim x = 17

现在我发现同一行在一个程序中起作用。其实我想要b $ b想问为什么,但现在我看到这可能是因为这行是

真的分成了声明和作业:


私人x


sub new

x = 17

end sub


这部分解释了错误信息,但编译器是否仍然不能足够聪明地隐式声明变量x As Integer?它应该是

能够像在一个过程中一样从表达式中推断出类型。

那就是Option Infer的全部内容。编译器确实拥有所有

信息。我没有看到问题。


我唯一的解释就是它内部首先将源代码分成这两部分(声明)同时丢失了

类型的信息。可以避免,IMO。


Armin
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



可能为什么他们在C#中添加了变量密钥...


var x = 17;


-

Tom Shelton

Probably why they added the var keword in C#...

var x = 17;

--
Tom Shelton


我不明白你的回答。 var关键字在

类级别无效。


至于OP问题,在编译时无法确定

类级别的Dim x应该是什么时候可以从类中的多个位置初始化

.


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追加(ByVal s As String)

x& = s

End Sub

结束班级


在Append中使用匿名类型时应该使用哪种数据类型?

编译器无法推断它。

Tom Shelton写道:
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:

2007-12- 11,Armin Zingler< az ******* @ freenet.dewrote:
On 2007-12-11, Armin Zingler <az*******@freenet.dewrote:

>

处理完毕后当前VB版本的新可能性,我再次
并且仍然想知道为什么Option Infer不能与Class Strict在类级别上使用,即在声明字段时:
Dim x = 17
现在我发现同一行在一个程序中起作用。实际上我想问为什么,但现在我看到这可能是因为这条线真的被分成了声明和作业:

私人x

sub new
x = 17
结束sub

这部分解释了错误信息,但编译器仍然不应该聪明到隐含声明变量x作为整数?它应该能够像在一个过程中一样从表达式中推断出类型。
这就是Option Infer的全部内容。编译器确实拥有所有的信息。我没有看到问题。

我唯一的解释是它内部首先将源代码行拆分为这两部分(声明+分配),同时放弃
类型信息。可以避免,IMO。

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



可能为什么他们在C#中添加了变量密钥...


var x = 17;


Probably why they added the var keword in C#...

var x = 17;


我不明白。在你的情况下,分配失踪,所以它不能
工作。我的情况是明确的:


公共类资料

dim x =" string"

结束班


编译器阻止声明x As String是什么?它抱怨

Option Strict需要一个As子句。这是正确的,除非Option Infer开启

并且声明行包含一个赋值。这两个先决条件都符合
,所以我没有看到错误的原因。

换句话说,阅读源代码的每个人都知道如何在这种情况下,x必须是

。有疑问吗?如果编译器没有b $ b识别相同,那就是一个缺陷。


" Kelly Ethridge" < ke *** @ kellyethridge.comschrieb
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" <ke***@kellyethridge.comschrieb

我不明白你的回答。 var关键字在

类级别无效。


至于OP问题,在编译时无法确定

类级别的Dim x应该是什么时候可以从类中的多个位置初始化

.


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追加(ByVal s As String)

x& = s

End Sub

结束班级


在Append中使用匿名类型时应该使用哪种数据类型?

编译器无法推断它。


Tom Shelton写道:
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 ******* @ freenet.dewrote:
On 2007-12-11, Armin Zingler <az*******@freenet.dewrote:



> ;

在处理了当前VB

版本的新可能性之后,我再次仍然想知道为什么Option Infer不能与
一起使用选项严格在课程级别,即在声明一个

字段时:

>

Dim x = 17

>

现在我发现同一行在一个程序中起作用。

实际上我想问为什么,但现在我看到它可能是

因为该行真的被拆分为声明和赋值:

>

private x

>

sub new

x = 17

end sub

>

这部分解释了错误信息, 但是不应该是

编译器仍然足够聪明,可以隐式声明变量x

As Integer?它应该能够像在一个过程中一样从

表达式推断出类型。这就是选项

推断的全部内容。编译器确实拥有所有信息。

我没有看到问题。

>

我唯一的解释是它在内部首先拆分

将源代码行分成这两部分

(声明+分配)同时丢失类型信息。

可避免,IMO 。

>

>

Armin

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



可能为什么他们在C#中添加了变量密钥...


var x = 17;

Probably why they added the var keword in C#...

var x = 17;


这篇关于VB 2008:Option Strict On + Infer On在课堂上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆