vb.net中的十进制值问题 [英] Problem with a decimal Value In vb.net

查看:100
本文介绍了vb.net中的十进制值问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个十进制值,并且必须减去它们...

当我从数据集中读取第一个数据集时(数据集返回正确的值ex.0.012)然后当我给出变量的值,它将它转换为12D)我怎样才能避免这个问题,我希望变量得到相同的值0.012 ...这是我的代码

I have two decimal values, and a have to subtract them...
When I read the first one form a dataset (the dataset returns the correct value ex.0.012) then when I gave the value to a variable it converts it to a 12D) How can I avoid this problem, I want the variable to get the same value 0.012... Here is my code

Private _Value As Decimal 
    Public Property Value As Decimal
        Get
            Return _Value
        End Get
        Set(ByVal value As Decimal)
            _Value = value
        End Set
    End Property
'-------------- The method---------------
 Value = EX.GetValue(a, b, c).Tables(0).Rows(0)(0)







方法GetValue它返回一个值为的数据集:






The method GetValue It returns a dataset with the value:

Public Function GetValue(ByVal a As String, ByVal bAs String, ByVal s As String) As DataSet

        Dim ds As DataSet
        ds = ExecDataSet(Source.sp, "MyProcedure", "Expertiza", a, b, c)
        Return ds

    End Function



AND ExecDataset如下所示:


AND the ExecDataset looks like:

Public Function ExecDataSet _
       (ByVal source As Source, ByVal strStoredProc As String, _
       ByVal tableName As String, _
       ByVal ParamArray objValues() As Object) _
       As DataSet

       Dim objCommand As SqlCommand
       Dim objDataSet As DataSet
       Dim objAdapter As New SqlClient.SqlDataAdapter

       objCommand = New SqlCommand

       objCommand.CommandText = strStoredProc
       objCommand.CommandTimeout = 1800 ' 30 min.

       objCommand.CommandType = source 'cmdType
       If dbConnection.State <> ConnectionState.Closed Then dbConnection.Close()
       objCommand.Connection = dbConnection

       Try
           If objCommand.Connection.State = ConnectionState.Closed Then
               objCommand.Connection.Open()
           End If
           If source = source.sp Then
               If (objValues.Length = 0) Then
                   'AddFieldParameters(objCommand)
               Else
                   AddParameters(objCommand, objValues)
               End If
           End If
           objAdapter = New SqlDataAdapter(objCommand)
           objDataSet = New DataSet

           objAdapter.Fill(objDataSet, tableName)
       Catch ex As Exception
           If ConnectionState.Open Then
               objCommand.Connection.Close()
           End If
           Throw ex
       Finally
           objAdapter.Dispose()
           objCommand.Dispose()
           If ConnectionState.Open Then
               objCommand.Connection.Close()
           End If

       End Try

       Return objDataSet
   End Function

推荐答案

所以 EX.GetValue(a, b,c)返回数据集对象。您正在查看第一个表,查看第一行,并返回第一列中的值。



问题是,这是对象,你的属性需要 Decimal 。框架可以通过做出最佳猜测来处理这个问题,但有时它会猜错。这就是为什么我发现最好使用 Option Explicit = True 编写和编译我的应用程序,它告诉编译器不做任何假设并将其标记为错误。



在分配结果之前尝试显式转换结果,看看它是否有任何区别:
So EX.GetValue(a, b, c) returns a Dataset object. You are taking the first table, looking at the first row, and returning the value in the first column.

Problem is, that is an Object, and your property expects a Decimal. The Framework can handle this by making a best guess, but sometimes it guesses wrong. This is why I find it best to write and compile my apps with Option Explicit = True, which tells the compiler not to make ANY assumptions and just flag it as an error.

Try explicitly converting the result before assigning it, and see if it makes any difference:
Value = CDbl(EX.GetValue(a, b, c).Tables(0).Rows(0)(0))

要检查的另一件事是验证数据库中的值是否为您期望的价值:实际上可能是12而不是0.012。

The other thing to check is to verify that the value in the database is the value you are expecting: it may actually be 12 rather than 0.012.


这篇关于vb.net中的十进制值问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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