0

我在 222-225 下面的一个字段中有一个数据库。我尝试进行拆分以读取我的函数的值。只是简单的函数 a=225 b=222 然后 total=(ab)+1。这是我的代码

Dgv.CellClick
'Dim x As Boolean
Dim a As Double
Dim total As Double

a = CDbl(Dgv.Item(8, Dgv.CurrentRow.Index).Value)
Split(a, "-")
total = (a) - (a)
Dgv.Item(9, Dgv.CurrentRow.Index).Value = total

我的问题是这不起作用。我无法获得我拆分的价值。知道如何解决这个问题吗?

注意:我使用 VB.NET 2005

4

7 回答 7

2

如果你想total=(a-b)+1..那应该是

dim b = a.Split("-")

total = val(b(1)) - val(b(2)) + 1
于 2013-07-03T03:32:06.003 回答
1

就像其他人所说,Split()返回一个String数组,如下所示:

Dim SplitValue() As String = Split(a, "-")
total = (CType(SplitValue(1), Double) - CType(SplitValue(0), Double)) + 1
于 2013-07-03T03:39:58.893 回答
1

如果我正确阅读了您的问题,则您要查找的值是 222-225,并且该值位于 Dgv 的指定单元格中(我猜是 DataGridView)。如果我的理解是正确的,那么有几件事正在发生。

首先,我不确定您为什么要尝试使用以下代码行将该值转换为双精度:

a = CDbl(Dgv.Item(8, Dgv.CurrentRow.Index).Value)

DataGridView的Item属性包含一个DataGridViewCell,而返回的Value属性是一个。我相信,尝试将 222-225 转换为双精度会失败(尽管由于这是 VB.NET,因此它可能不会取决于您设置的选项 - 我对 VB.NET 并不熟悉使用 C#)。DataGridViewCellObject

即使它确实成功地工作(我不确定输出会是什么),也Split需要一个字符串。我会将该行代码更改为以下内容:

a = Dgv.Item(8, Dgv.CurrentRow.Index).Value.ToString()

现在你有了一个可以使用 Split 的字符串。您发布的代码中的 Split 似乎是 Visual Basic (pre-.NET) Split 方法Split Function (Visual Basic)。正如其他人所提到的,Split 根据分隔符返回一个字符串数组。在您的代码中,您没有将 Split 的结果分配给任何东西,因此您无法获取这些值。

我建议使用 .NET 版本的 Split ( String.Split Method ) - 您可以调用多种方法String.Split,但出于代码的目的,我会像这样使用它:

Dim splits As String() = a.Split(New Char() { "-" })

其中 a 是上面选定的 DataGridViewCell 中的字符串值。这将为您提供一个 2 元素数组:

splits(0) = "222"
splits(1) = "225"

最后一部分是你的公式。由于您有字符串,因此您需要将它们转换为数字数据类型:

total = (CDbl(splits(1)) - CDbl(splits(0))) + 1

变为 (225 - 222) + 1 = 4。

总而言之,它看起来像这样:

Dim a As String
Dim total As Double
Dim splits() As String

a = Dgv.Item(8, Dgv.CurrentRow.Index).Value.ToString()

splits = a.Split(New Char() { "-" })

total = (CDbl(splits(1)) - CDbl(splits(0))) + 1

Dgv.Item(9, Dgv.CurrentRow.Index).Value = total
于 2013-07-03T06:44:09.350 回答
1

可能这会有所帮助。试试这个...

Dim a As String
            a = ""
            Dim x As String
            Dim total As Double
            a = Dgv.Item(8, Dgv.CurrentRow.Index).Value.ToString
            Dim ary() As String
            x = a
            ary = x.Split("-")
            total = CInt(ary(1)) - CInt(ary(0))

            Dgv.Item(9, Dgv.CurrentRow.Index).Value = total
于 2013-07-03T07:06:35.087 回答
0

拆分返回一个数组。像这样的东西。VB.Net 不是我的主要语言,但这应该会有所帮助。

dim arr = a.Split(New Char (){"-"})
total = ctype(arr(0), double) - ctype(arr(1),double)
于 2013-07-03T03:34:17.187 回答
0

试试这个:

Dim aux() As String = a.Split("-"c)

total = CDbl(aux(0)) - CDbl(aux(1)) + 1
于 2013-07-03T05:20:41.500 回答
0
Dim a As string
Dim x As String
Dim total As Double

a = Dgv.Item(8, Dgv.CurrentRow.Index).Value

Dim ary() As String
x = a

ary() = x.Split("-")
total = CInt(ary(1)) - CInt(ary(0))

Dgv.Item(9, Dgv.CurrentRow.Index).Value = total
于 2013-07-03T05:21:11.513 回答