为 Option Strict 转换数据类型 [英] Converting DataTypes for Option Strict

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

问题描述

我只是想知道如何正确

  1. 将字符串转换为日期
  2. 将整数转换为短整数
  3. 将字符串转换为整数
  4. 将字符串转换为双精度

正确地不使用任何类型的任何 Ctypes.我打开了 Option Strict,现在所有这些错误都出现了,所以我正在尝试修复它们:)

correctly without using any Ctypes of any sort. I turned on Option Strict and now all these errors popped up so I'm trying to fix them :)

推荐答案

NET 提供了多种方法来检查、测试和转换数据.

NET provides several ways to check, test and convert data.

TryParse

这可能是最安全的转换方式.它非常适合验证用户输入:

This is perhaps the most bulletproof way to convert. It is ideal for validating user input:

Dim n As Int32
If Int32.TryParse(TextBox.Text, n) Then
    ' work with n
Else
    ' error handling: TextBox.Text cannot convert to int
End If

所有核心数据类型都支持它(Int64DecimalDate 等).它与其他的不同:它不返回转换后的值,而是返回一个Boolean,指示源字符串是否可以转换为变量.

All the core datatypes support it (Int64, Decimal, Date etc). It is different from others: it does not return the converted value, but a Boolean indicating whether the source string could be converted to a variable.

解析

Dim d as Double = Double.Parse(strVar) 

当您的代码创建 strVar 并且您知道它包含数字而不是标点符号等时,可以使用它.ParseTryParse 都允许您可以通过提供 FormatProvider 和其他选项来解析来自其他文化的数据:

This is ok to use when your code created strVar and you know it contains numerals and not punctuation etc. Parse and TryParse both allow you to parse data from other cultures by providing a FormatProvider and other options:

Dim strDec = "$ 123.45"
Dim decVal As Decimal = Decimal.Parse("$123.45", NumberStyles.Currency)

其他方法会阻塞$",样式参数允许.

Other methods will choke on the "$", the style argument allows it.

Convert.ToInt32/Convert.ToInteger

这个有很多很多重载:

Dim intV As Int32 = Convert.ToInt32(aString)
Dim dblV As Double = Convert.ToDouble(aString)
Dim lngV As Int64 = Convert.ToInt64(aString)

Convert.Toxxx 方法的专业性略低于上述方法.他们允许文化转换,但他们假设直接转换是可能的.也就是说,123"将转换为整数,但不会转换为123.45",它不会知道货币格式之类的东西.

The Convert.Toxxx methods are slightly less specialized than the ones above. They allow culture conversions, but they assume a direct conversion is possible. That is, "123" will convert to integer, but not "123.45" and it wont know anything thing like currency formats.

因此,当您知道值可以转换时使用它,也许是因为您将值转换为字符串.这些也适用于从数据库中拆箱对象 DataReader:

So, use this when you know the value can convert, perhaps because you converted the value to string. These also work to unbox objects from a database DataReader:

myDT = Convert.ToDateTime(rdr("BirthDate"))
myV = Convert.ToInt32(rdr("FooValue"))

如果数据实际上存储为这些类型并装箱"为 DBDataReader 的对象,Convert 将正常工作.(注意,DBDataReaders 可以直接返回类型化数据:n = rdr.GetInt32(1) 但不能使用列名,只能使用序号位置).

Provided the data is actually stored as those types and "boxed" as Object for the DBDataReader, Convert will work fine. (Note that DBDataReaders can return typed data directly: n = rdr.GetInt32(1) but you cannot use the column name, just the ordinal position).

CInt、CSng...等

CInt 类似于 Convert.ToInteger,但根据 MSDN 优化用于 VB.例如:

CInt is similar to Convert.ToInteger, but according to MSDN optimized for use in VB. For instance:

a = Convert.ToInt32("123.50")     ' exception due to decimal point
a = CInt("123.50")                ' a = 124

令人困惑?是的,但是一旦你掌握了它们,你就可以使用最适合这种情况的方法.无法为 CIntCDec 等指定区域性

Confusing? Yes, but once you get a handle on them, you can use the method which best fits the situation. There is no way to specify a culture for CInt, CDec etc.

不要使用Val.曾经.这是一个非常通用的函数,它返回一个 Double.使用它会导致各种不受欢迎的类型转换:

Don't use Val. Ever. It is a very generic function which returns a Double. Using it can result in all sorts of undesirable Type conversions:

Dim n = Val(tbSmallValue.Text)

n 被创建为 Double,所以如果文本框是5",那么 n = 5.0.当 n 用于需要整数的地方(例如插入数据库)时,可能会发生错误.结果差别很大:

n is created as a Double so if the textbox was "5", then n = 5.0. When n is used where an integer is expected (such as inserting into a database) errors can happen. The result varies greatly:

dd = Val("123.4")          ' = 123.4
dd = Val("$123.45")        ' = 0.0
dd = Val("123abc")         ' = 123.0

<小时>

NB CIntConvert.ToInt32 实现银行家的舍入",其中 .50 分数舍入为最近的偶数.123.50124.50 都舍入/转换为 124.


NB CInt and Convert.ToInt32 implement "Banker's Rounding" where .50 fractions are rounded to the nearest even number. 123.50 and 124.50 both round/convert to 124.

这篇关于为 Option Strict 转换数据类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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