数组的反向交集操作 vb.net [英] Reverse Intersection operation with arrays vb.net

查看:22
本文介绍了数组的反向交集操作 vb.net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

    Dim count as Short = 10
    Dim roll(count), absent(count), present(count) As Short
    Dim i As Short = 1
    query = "SELECT * FROM primary_student_table WHERE admityear=" & year & " AND batch= " & batch & ""
    con.Open()
    cmd = New SqlCommand(query, con)
    re = cmd.ExecuteReader
    While re.Read
            roll(i) = re("roll")
            i += 1
    End While
    con.Close()

    absent = txtlist.Text.Split(","c).Select(Function(s) Short.Parse(s)).ToArray()
    present = roll.Except(absent).ToArray()
        MsgBox(absent(0))
        MsgBox(present(0))

在上面的代码中,数组中的值如下

In the above code, the values in the arrays are as follows

  • roll=21,22,23,24,25,26,27,28,29,30(所有学生的rollNos在课堂上)

  • roll=21,22,23,24,25,26,27,28,29,30 (rollNos of all the students in the class)

txtlist.text 值 = 21,22(这意味着这两个课程缺席)

txtlist.text value = 21,22 (meaning those two were absent for the class)

现在我需要将缺席者的卷保存在不存在的数组中,而将其余的卷保存在数组中

缺席者列表已正确保存,但第二个 MsgBox 显示的是 0 而不是 23

The absentees list gets saved correctly but the second MsgBox is displaying 0 instead of 23

代码有什么问题

推荐答案

VB.NET 中的数组是从零开始的.您正在从索引 1 开始设置滚动值:

Arrays in VB.NET are zero-based. You are setting values in roll starting from index 1 :

Dim i As Short = 1

然后,当您读取数据读取器时,您将从索引 1 开始加载到滚动数组中.

Then when you're reading the datareader, you're loading into roll array starting from index 1.

'i is set to 1, so you're setting values from the second array item onwards
While re.Read
        roll(i) = re("roll")
        i += 1
End While

所以 roll(0) 的值始终为 0,这将被发送到您当前的数组.

So the value of roll(0) is always 0, and this will be sent to your present array.

从数据读取器读取时您可能没有得到索引越界异常的原因是因为您的数据读取器返回 10 行,而您的 roll 数组可以容纳 11(roll(10) 实际上是一个 11 的数组).

The reason that you're probably not getting an index out of bounds exception when reading from your datareader is because your datareader returns 10 rows, and your roll array can hold 11 (roll(10) is actually an array of 11).

将i的值设为0,应该就可以了:

Set the value of i to 0, and it should be fine:

Dim i As Short = 0

根据 Tim Schmelter 的建议,您确实应该考虑使用 List(Of T) 而不是数组.另外,请考虑使用参数化 sql 查询.

As per Tim Schmelter's advice, you really should consider using List(Of T) instead of arrays. Also, consider using parameterized sql queries.

这篇关于数组的反向交集操作 vb.net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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