使用 DirectCast、CType、TryCast 转换数据类型 [英] Casting DataTypes with DirectCast, CType, TryCast

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

问题描述

自从我在 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 对转换的支持的转换运算符.它们可以快速将基类型的对象转换为派生类型或取消装箱值类型的值.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)

如果 Option Strict On 生效,您必须使用它.如果它是关闭的,那么你可以直接进行:

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

当然非常方便,也是 VB.NET 作为动态类型语言的遗产的一部分.但并非没有问题,该日期是 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、CType 和显式转换是个人选择.如果您现在使用 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 转换数据类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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