如何用VB.NET安全地验证整数类型?

2026-05-08 11:353阅读0评论SEO资讯
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计442个文字,预计阅读时间需要2分钟。

如何用VB.NET安全地验证整数类型?

这可能是一个问题,而不是功能。我正在寻找一种从字符串和对象检查整数绝对值最安全的方法,在.NET中使用大量内置函数可能会产生第一个异常,这会在立即窗口中显示,并随时间随机发生。

这可能是一个优雅的问题,而不是功能.我正在寻找从字符串和对象检查整数的绝对最安全的方法,

在.net中使用大多数内置函数似乎会生成第一个机会异常,显示在立即窗口中,并且随着时间的推移它们只是构建起来.这些异常的含义是什么,因为它们似乎不会影响系统的运行.

这是我的两次尝试,都觉得笨重,我知道必须有一个比使用VB.IsDBNull和Integer.TryParse更好的方法…或者我只是肛门.

(从对象的整数)

如何用VB.NET安全地验证整数类型?

Dim nInteger As Integer = 0 If oData Is Nothing OrElse VB.IsDBNull(oData) Then Else If bThrowErrorIfInvalid Then Else On Error Resume Next End If nInteger = CType(oData, Integer) End If Return nInteger

(从字符串整数)

Dim nInteger As Integer = 0 If sText Is Nothing Then Else If bThrowErrorIfInvalid Then Else On Error Resume Next End If Integer.TryParse(sText, nInteger) End If Return nInteger 使用Integer.TryParse有什么问题?多数民众赞成在… …

int i = 0; string toTest = "not number"; if(int.TryParse(toTest, out i)) { // it worked }

那笨重怎么样? (C#不是VB我知道,但相同的差异)

编辑:添加,如果你想从一个对象检查(因为TryParse依赖于一个字符串),我不太确定你如何实际计划使用它.这是否可以解决您的问题,因为这种方法会检查您的两种情况?

static bool TryParseInt(object o, out int i) { i = 0; if (o.GetType() == typeof(int)) { i = (int)o; return true; } else if (o.GetType() == typeof(string)) { return int.TryParse(o as string, out i); } return false; }

本文共计442个文字,预计阅读时间需要2分钟。

如何用VB.NET安全地验证整数类型?

这可能是一个问题,而不是功能。我正在寻找一种从字符串和对象检查整数绝对值最安全的方法,在.NET中使用大量内置函数可能会产生第一个异常,这会在立即窗口中显示,并随时间随机发生。

这可能是一个优雅的问题,而不是功能.我正在寻找从字符串和对象检查整数的绝对最安全的方法,

在.net中使用大多数内置函数似乎会生成第一个机会异常,显示在立即窗口中,并且随着时间的推移它们只是构建起来.这些异常的含义是什么,因为它们似乎不会影响系统的运行.

这是我的两次尝试,都觉得笨重,我知道必须有一个比使用VB.IsDBNull和Integer.TryParse更好的方法…或者我只是肛门.

(从对象的整数)

如何用VB.NET安全地验证整数类型?

Dim nInteger As Integer = 0 If oData Is Nothing OrElse VB.IsDBNull(oData) Then Else If bThrowErrorIfInvalid Then Else On Error Resume Next End If nInteger = CType(oData, Integer) End If Return nInteger

(从字符串整数)

Dim nInteger As Integer = 0 If sText Is Nothing Then Else If bThrowErrorIfInvalid Then Else On Error Resume Next End If Integer.TryParse(sText, nInteger) End If Return nInteger 使用Integer.TryParse有什么问题?多数民众赞成在… …

int i = 0; string toTest = "not number"; if(int.TryParse(toTest, out i)) { // it worked }

那笨重怎么样? (C#不是VB我知道,但相同的差异)

编辑:添加,如果你想从一个对象检查(因为TryParse依赖于一个字符串),我不太确定你如何实际计划使用它.这是否可以解决您的问题,因为这种方法会检查您的两种情况?

static bool TryParseInt(object o, out int i) { i = 0; if (o.GetType() == typeof(int)) { i = (int)o; return true; } else if (o.GetType() == typeof(string)) { return int.TryParse(o as string, out i); } return false; }