铸件? [英] Casting?
问题描述
您好:我对投射感到困惑...
选项严格= ON
示例1:
---------------
当我说出这样的话......
Dim lo_Foo = New Foo
Dim lo_Obj as Object = lo_Foo
工作正常!
例2:
--- ------------
当我说出这样的话......
Dim lo_Foo = New Foo
OutPutObj(lo_Foo)
Public Sub OutPutObj(作为对象的ao_Obj)
...
结束子
编译错误:Option Strict ON禁止隐式转换从
System.Object到类型Foo
当第一个例子中
类型对象的变量被设置为Foo的实例时,是否存在隐式转换???
谢谢,
Shannon Richards
BBA,AIT,MCP
" ; Shannon Richards& QUOT; < SR ********** @ hotmail.com> schrieb:你好:我对铸造感到困惑......
选项严格= ON
例1:
---- -----------
当我说出这样的话......
Dim lo_Foo = New Foo
Dim lo_Obj as Object = lo_Foo
工作正常!
例2:
---------------
当我说出这样的话......
Dim lo_Foo = New Foo
OutPutObj(lo_Foo)
Public Sub OutPutObj(ao_Obj as Object)
...
编译错误:Option Strict ON禁止从
System.Object到类型Foo的隐式转换
它在哪里说?对于您发布的代码,它没有说明。
从基类型转换为其某个
派生类型时,将显示该消息,但不是在铸造时从派生类型到基本类型。
这就是为什么演员''对象''将始终有效。
-
Herfried K. Wagner [MVP]
< URL:http://dotnet.mvps.org/>
< br>
所有类型的轿车都是汽车,但并非所有轿车都是轿车
汽车。
你可以把轿车当作轿车但是你不能把汽车当成轿车。
得到它?
Shannon Richards < SR ********** @ hotmail.com>在消息中写道
新闻:uU ************* @ TK2MSFTNGP10.phx.gbl ...你好:我很困惑铸造...
选项严格= ON
例1:
---------------
我说的是这样的......
Dim lo_Foo = New Foo
Dim lo_Obj as Object = lo_Foo
工作正常!
例2:
---------------
当我说出这样的话......
Dim lo_Foo = New Foo >
OutPutObj(lo_Foo)
Public Sub OutPutObj(ao_Obj as Object)
...
End Sub
编译错误:" ; Option Strict ON禁止从
System.Object到类型Foo的隐式转换
类型对象的变量
在第一个示例中没有隐式转换设置为Foo的实例???
谢谢你,
Shannon Richards
BBA,AIT,MCP
>
Hi Shannon,
Option Strict On不允许后期绑定。出于某种原因,我相信
编译器认为你的第一个例子是早期绑定或允许
后期绑定和你的第二个例子非法后期绑定与Option Strict
开。我认为这是正在发生的事情但我不是完全正面的。我的
点可能这不是一个演员问题,而是一个绑定问题。
一些关于绑定的研究可以给你一个明确的答案。祝你好运!
肯。
-
Ken Dopierala Jr.
对于优秀的ASP .Net虚拟主机尝试:
http:// www。 webhost4life.com/default.asp?refid=Spinlight
如果您在我这里注册并需要帮助,请给我发电子邮件。
" Shannon Richards < SR ********** @ hotmail.com>在消息中写道
新闻:uU ************* @ TK2MSFTNGP10.phx.gbl ...你好:我很困惑铸造...
选项严格= ON
例1:
---------------
我说的是这样的......
Dim lo_Foo = New Foo
Dim lo_Obj as Object = lo_Foo
工作正常!
例2:
---------------
当我说出这样的话......
Dim lo_Foo = New Foo >
OutPutObj(lo_Foo)
Public Sub OutPutObj(ao_Obj as Object)
...
End Sub
编译错误:" ; Option Strict ON禁止从
System.Object到类型Foo的隐式转换
当对象类型的变量
是第一个示例时,是否存在隐式转换设置为Foo的实例???
谢谢你,
Shannon Richards
BBA,AIT,MCP
Hello: I am confused about casting...
Option Strict = ON
Example1:
---------------
When I say something like this...
Dim lo_Foo = New Foo
Dim lo_Obj as Object = lo_Foo
Works Fine!
Example2:
---------------
When I say something like this...
Dim lo_Foo = New Foo
OutPutObj(lo_Foo)
Public Sub OutPutObj(ao_Obj as Object)
...
End Sub
Compile Error: "Option Strict ON disallows implicit conversion from
System.Object to type Foo"
Isn''t there an implicit conversion in the first example when the variable of
type object is set to the instance of Foo???
Thank you,
Shannon Richards
BBA, AIT, MCP
"Shannon Richards" <sr**********@hotmail.com> schrieb:Hello: I am confused about casting...
Option Strict = ON
Example1:
---------------
When I say something like this...
Dim lo_Foo = New Foo
Dim lo_Obj as Object = lo_Foo
Works Fine!
Example2:
---------------
When I say something like this...
Dim lo_Foo = New Foo
OutPutObj(lo_Foo)
Public Sub OutPutObj(ao_Obj as Object)
...
End Sub
Compile Error: "Option Strict ON disallows implicit conversion from
System.Object to type Foo"
Where does it say that? It doesn''t say that for the code you posted.
The message will be shown when casting from a base type to one of its
derived type, but not when "casting" from the derived type to the base type.
That''s why the cast to ''Object'' will always work.
--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
All types of sedan automobiles are cars, but not all cars are sedan
automobiles.
You can cast a sedan as a car, but you can''t cast a car as a sedan.
Get it?
"Shannon Richards" <sr**********@hotmail.com> wrote in message
news:uU*************@TK2MSFTNGP10.phx.gbl...Hello: I am confused about casting...
Option Strict = ON
Example1:
---------------
When I say something like this...
Dim lo_Foo = New Foo
Dim lo_Obj as Object = lo_Foo
Works Fine!
Example2:
---------------
When I say something like this...
Dim lo_Foo = New Foo
OutPutObj(lo_Foo)
Public Sub OutPutObj(ao_Obj as Object)
...
End Sub
Compile Error: "Option Strict ON disallows implicit conversion from
System.Object to type Foo"
Isn''t there an implicit conversion in the first example when the variable
of type object is set to the instance of Foo???
Thank you,
Shannon Richards
BBA, AIT, MCP
Hi Shannon,
Option Strict On doesn''t allow late-binding. For some reason I believe the
compiler considers your first example either early-binding or allowed
late-binding and your second example illegal late-binding with Option Strict
On. I think that is what is going on but I''m not totally positive. My
point is that maybe this isn''t a casting problem but a binding problem.
Some research on binding could give you a definitive answer. Good luck!
Ken.
--
Ken Dopierala Jr.
For great ASP.Net web hosting try:
http://www.webhost4life.com/default.asp?refid=Spinlight
If you sign up under me and need help, email me.
"Shannon Richards" <sr**********@hotmail.com> wrote in message
news:uU*************@TK2MSFTNGP10.phx.gbl...Hello: I am confused about casting...
Option Strict = ON
Example1:
---------------
When I say something like this...
Dim lo_Foo = New Foo
Dim lo_Obj as Object = lo_Foo
Works Fine!
Example2:
---------------
When I say something like this...
Dim lo_Foo = New Foo
OutPutObj(lo_Foo)
Public Sub OutPutObj(ao_Obj as Object)
...
End Sub
Compile Error: "Option Strict ON disallows implicit conversion from
System.Object to type Foo"
Isn''t there an implicit conversion in the first example when the variable of type object is set to the instance of Foo???
Thank you,
Shannon Richards
BBA, AIT, MCP
这篇关于铸件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!