gpt4 book ai didi

vb.net - Visual Basic 动态类型转换和比较

转载 作者:行者123 更新时间:2023-12-03 21:19:46 30 4
gpt4 key购买 nike

我正在尝试创建一个函数来测试 DataRow 对象的两个成员是否相等,所有这些成员最初都是字符串,但需要在比较之前转换为它们的实际数据类型。如果无法将数据转换为实际数据类型,则该函数应返回 false。这是我要争取的一般框架。

Private Function compareValues(ByVal row as DataRow, ByVal field as String, ByVal dType as Type) as Boolean
' Get the raw values from the DataRow
Dim raw1 As String = row("t1_" + field).ToString()
Dim raw2 As String = row("t2_" + field).ToString()
Dim val1, val2 As dtype

' Try to convert the raw strings to the target type
Try
val1 = CType(raw1, dType) '<--Error here
val2 = CType(raw2, dType) '<--Error here
Catch ex As Exception
Return False ' If either conversion fails, return false
End Try

' Compare the values in their actual types and return false is they don't match
If (Not val1 = val2) Then
Return False
End If
Return True
End Function

我得到的错误是:Type 'dType' is not defined.

我试图通过使用 Of 子句来通用化该函数:

Private Function compareValues(Of dtype)(ByVal row As DataRow, ByVal field As String) As Boolean
Dim raw1 As String = row("t1_" + field).ToString()
Dim raw2 As String = row("t2_" + field).ToString()
Dim val1, val2 As dtype
Try
val1 = CTypeDynamic(Of dtype)(raw1)
val2 = CTypeDynamic(Of dtype)(raw2)
Catch ex As Exception
Return False
End Try
If (Not val1 = val2) Then '<--Error here
Return False
End If
Return True
End Function

然而,尝试这样做会导致错误:Operator '=' is not defined for types 'dtype' and 'dtype'. 一般来说,我认为我没有使用of 子句正确。

给定一个数据行 dr,它有两个字段 t1_sizet2_size,它们的值分别为“01.92”和“1.92”,我打算这样调用函数:

Dim match as Boolean = compareValues(dr, "size", Double) 'evaluates to True

给定具有各自值“01.90”和“1.92”的相同数据行和字段,应以相同方式调用该函数,但返回 False。

考虑到 t1_sizet2_size 分别为“apple”和“01.92”,该函数应该以与前面示例相同的方式调用,并且仍然返回 False .

决议/实现:

Steven Doggart 和 Styxxy 用他们的每一份贡献把它带回家。请在下面找到工作代码:

Private Function compareValues(Of dtype)(row As DataRow, field As String) As Boolean
Dim val1 As dtype
Dim val2 As dtype
Try
val1 = CType(row("t1_" + field), dtype)
val2 = CType(row("t2_" + field), dtype)
Catch ex As Exception
Return False
End Try
If (Not val1.Equals(val2)) Then
Return False
End If
Return True
End Function

调用如下,假设您有一个 DataRow,其中有两列您希望比较是否相等,“t1_size”和“t2_size”,每列都包含 float 的字符串表示形式:

Dim match as Boolean = compareValues(Of Double)(dr, "size")

最佳答案

你把它倒过来了。当dtype 是一个Type 变量时,您应该使用CTypeDynamic 方法,例如:

Private Function compareValues(row as DataRow, field as String, dType as Type) as Boolean
' ...
val1 = CTypeDynamic(raw1, dtype)
' ...
End Function

而当dtype是泛型类型时,应该使用CType,例如:

Private Function compareValues(Of dtype)(row As DataRow, field As String) As Boolean
' ...
val1 = CType(raw1, dtype)
' ...
End Function

但是,您可能希望将 raw1raw2 定义为 Object 并将它们设置为实际的字段值。我怀疑您是否真的想将它们转换为字符串只是为了转身并将它们再次转换回正确的数据类型。例如:

Dim raw1 As Object = row("t1_" + field)
Dim raw2 As Object = row("t2_" + field)

正如 Styxxy 指出的那样,通用 Field 扩展方法是在 .NET Framework 3.5 版中添加的。它会为您进行转换,因此您可以跳过“原始”部分并执行如下操作:

Private Function compareValues(Of dtype)(row As DataRow, field As String) As Boolean
Dim val1 As dtype = row.Field(Of dtype)("t1_" + field)
' ...
End Function

关于vb.net - Visual Basic 动态类型转换和比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19254415/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com