directCast [英] directCast

查看:82
本文介绍了directCast的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用DirectCast将对象转换为它的基础,还是应该使用CType?

我有一个类的实例作为Object。

运行时tipe是一个派生类,但我需要引用那个

实例作为基类


Ex:Class BaseCl

类DerivCl

继承BaseCl


''运行时间

将thisCl调暗为Object = new DerivCl

我可以使用DirectCast(thisCl,BaseCl).baseClMethod ...?

-

干杯,

Crirus


------------------------------

如果工作是件好事,老板会把这一切都从你那里拿走


--------------------- ---------

解决方案

AFAIK,是的。


CType做*转换*从一种类型转换为另一种类型(例如整数转换为

字符串)


DirectCast只接受一个对象,并将其转换为派生或基础或

interfac e型。对于直播工作,类型之间必须有直接关系

。在您的示例中,因为thisCl包含DerivC1和

" DeriveC1"继承自BaseC1,有直接的关系。


HTH,


Trev。


Crirus <铬**** @ datagroup.ro>在消息中写道

新闻:%2 **************** @ TK2MSFTNGP12.phx.gbl ...

我可以使用DirectCast将对象转换为它的基础,还是应该使用
CType?我有一个类的实例作为Object。
运行时tipe是一个类的派生,但我需要引用那个
实例作为基类
Ex:Class BaseCl
类DerivCl
继承BaseCl

''运行时
将thisCl变为Object = new DerivCl
我可以使用DirectCast( thisCl,BaseCl).baseClMethod ...?

-
干杯,
Crirus

----------- -------------------
如果工作是件好事,老板会把这一切都从你身上拿走

---- --------------------------



< blockquote class =post_quotes>我可以使用DirectCast将对象转换为它的基础,还是应该使用CType?




无关紧要在这种情况下使用的一个。


Mattias


-

Mattias Sj?gren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com

请回复到新闻组。


我猜它是为了速度的缘故


-

干杯,

Crirus


----------------------------- -

如果工作是件好事,老板就会把这一切都从你身上拿走


-------------- ----------------


" Mattias Sj?gren" <毫安******************** @ mvps.org>在消息中写道

新闻:%2 **************** @ TK2MSFTNGP10.phx.gbl ...

我可以使用DirectCast将对象转换为它的基础,还是应该使用


CType?
你在这个中使用哪一个没关系上下文。

Mattias

- Mattias Sj?gren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
请仅回复新闻组。



Can I use DirectCast to convert a object to it''s base or should I use CType?
I have a instance of a class as Object.
The run-time tipe is a derived of a class, but I need to refer to that
instance as to the base class

Ex: Class BaseCl
Class DerivCl
Inherits BaseCl

''Run-time
Dim thisCl as Object= new DerivCl
Can I use DirectCast(thisCl, BaseCl).baseClMethod ... ?
--
Cheers,
Crirus

------------------------------
If work were a good thing, the boss would take it all from you

------------------------------

解决方案

AFAIK, yes.

CType does a *conversion* from one type to another (e.g. Integer to a
string)

DirectCast simply takes an object, and casts it to a derived or base or
interface type. For directcast to work, there must be a direct relationship
between the types. In your example, because "thisCl" contains "DerivC1" and
"DeriveC1" inherits from "BaseC1", there is a direct relationship.

HTH,

Trev.

"Crirus" <Cr****@datagroup.ro> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...

Can I use DirectCast to convert a object to it''s base or should I use CType? I have a instance of a class as Object.
The run-time tipe is a derived of a class, but I need to refer to that
instance as to the base class

Ex: Class BaseCl
Class DerivCl
Inherits BaseCl

''Run-time
Dim thisCl as Object= new DerivCl
Can I use DirectCast(thisCl, BaseCl).baseClMethod ... ?
--
Cheers,
Crirus

------------------------------
If work were a good thing, the boss would take it all from you

------------------------------



Can I use DirectCast to convert a object to it''s base or should I use CType?



It doesn''t matter which one you use in this context.

Mattias

--
Mattias Sj?gren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.


I gues it does for speed sake

--
Cheers,
Crirus

------------------------------
If work were a good thing, the boss would take it all from you

------------------------------

"Mattias Sj?gren" <ma********************@mvps.org> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...

Can I use DirectCast to convert a object to it''s base or should I use

CType?
It doesn''t matter which one you use in this context.

Mattias

--
Mattias Sj?gren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.



这篇关于directCast的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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