使用DirectCast,CType,TryCast投射DataTypes [英] Casting DataTypes with DirectCast, CType, TryCast

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

问题描述

自从2005年我从VB6迁移到VB.NET以来,我一直在使用CType从一种数据类型转换到另一种数据类型。我这样做,因为它只是更快的类型,用于存在于VB6,我不知道为什么我必须使用DirectCast,如果它们之间显然没有区别。

Ever since I moved from VB6 to VB.NET somewhere in 2005, I've been using CType to do casting from one data type to another. I do this because it is simply faster to type, used to exist in VB6 and I do not know why I have to be using DirectCast if there is apparently no difference between them.

我使用TryCast一段时间,因为我明白,有时铸造可能失败。然而,我不能得到CType和DirectCast之间的区别。

I use TryCast once in a while because I understand that sometimes casting can fail. I however cannot get the difference between CType and DirectCast.

任何人都可以告诉我的区别简单的英语什么区别两个(CType和DirectCast)?

Can anyone tell me the difference in plain simple English what the difference the two (CType and DirectCast)? Adding examples of where to use what as well would be helpful.

感谢。

推荐答案

TryCast和DirectCast是直接映射到CLR支持的转换操作符。它们可以快速将基本类型的对象转换为派生类型或取消对值类型的值。当Cast不可能时,DirectCast抛出异常,如果失败,TryCast返回Nothing。你通常希望DirectCast捕获编程错误。

TryCast and DirectCast are casting operators that directly map to the CLR's support for casting. They can quickly cast an object of a base type to a derived type or unbox a value of a value type. DirectCast throws an exception when the cast isn't possible, TryCast returns Nothing if it failed. You typically want to favor DirectCast to catch programming mistakes.

CType允许一个超集的转换,CLR皱眉的。我可以想到的最好的例子是将字符串转换为数字或日期。例如:

CType allows a superset of conversions, ones that the CLR frowns on. The best example I can think of is converting a string to a number or date. For example:

Dim obj As Object
obj = "4/1/2010"
Dim dt As DateTime = CType(obj, DateTime)

有效。如果是关闭,那么您可以直接执行:

Which you'll have to use if Option Strict On is in effect. If it is Off then you can do it directly:

Option Strict Off
...
    Dim dt As DateTime = obj

非常方便,而且作为动态类型语言。但不是没有问题,那个日期是独角兽日在stackoverflow.com但将是一天在一月英国人进入字符串。意外的转换是CLR不允许这些直接的原因。显式的,从来没有惊喜的转换看起来像这样:

Very convenient of course and part of VB.NET's legacy as a dynamically typed language. But not without problems, that date is Unicorn day at stackoverflow.com but will be a day in January when a Briton enters the string. Unexpected conversions is the reason the CLR doesn't permit these directly. The explicit, never a surprise conversion looks like this:

Dim dt As DateTime = DateTime.Parse(obj.ToString(), _
    System.Globalization.CultureInfo.GetCultureInfo("en-US").DateTimeFormat)

是否应该购买Try / DirectCast vs CType vs显式转换是一个个人选择。如果你现在使用Option Strict On编程,那么你应该肯定开始使用Try / DirectCast。如果你喜欢VB.NET语言,因为你喜欢动态类型的方便,那么不要犹豫留在CType。

Whether you should buy into Try/DirectCast vs CType vs explicit conversions is rather a personal choice. If you now program with Option Strict On then you should definitely start using Try/DirectCast. If you favor the VB.NET language because you like the convenience of dynamic typing then don't hesitate to stay on CType.

这篇关于使用DirectCast,CType,TryCast投射DataTypes的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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