0

编辑

好的,所以在解决了一段时间之后,我想出了一个可行的解决方案,而且它比我最初想象的要干净。谢谢你们的帮助。这是代码。

Function Program_Search() As String()
    'An ArrayList of Objects containing strings for each row.
    'So the result is ArrayList(objRow1(strID, strPrograms), objRow2(strID, strPrograms).. etc)
    Dim program_results As ArrayList = Get_Results("SELECT ID, Programs FROM tbMetrics ORDER BY ID ASC", strConnectionString)

    'Initialize the list of programs. This will contian the tbMetrics Programs that match the selected Program name.
    Dim programs As ArrayList = New ArrayList

    'Loop through each row selected
    For Each row As Object In program_results
        'Not currently used.
        Dim strID As String = row(0).ToString

        'An integer representation of a binary number. Each digit represents a different program being checked.
        Dim intPrograms As Integer = CInt(row(1).ToString)

        'Convert number to binary string (reversed)
        Dim strReversed As String = StrReverse(Convert.ToString(intPrograms, 2))

        'Loop through each of the programs in the drop down box which should contain all of the possible choices in order.
        For index As Integer = 0 To ddlPrograms.Items.Count - 1
            'A bit of an ugly if state that checks for a 1 in the binary position for the selected program.
            'Then if it's selected, it checks that the program matches the one selected by the user.
            'Finally, it makes sure it doesn't add the same number to the array.
            If (strReversed.Length - 1) >= index _
                And strReversed(index) = "1" _
                And ddlPrograms.SelectedValue.ToString = ddlPrograms.Items(index).Value.ToString _
                And programs.Contains(intPrograms) = False Then

                'If it passes all of the above checks, then finally add the program integer to the arraylist.
                programs.Add(intPrograms)
            End If
        Next index
    Next row

    'Return the list of programs as an array, to be used with SQL IN().
    Return programs.ToArray
End Function

结束编辑

原文如下

好的,所以我是一名 PHP 程序员,正在尝试学习一些 VB.NET。数组在 VB.NET 中让我非常困惑,所以我以我知道的方式用 PHP 编写了一些示例代码。如果有人能告诉我它在 VB.NET 中的正确工作方式,我将不胜感激。

<?php

function get_result() {
    $result = query("SELECT id, value FROM test_table");
    /*Returned as:
    array(
      array("id1", "value1"), 
      array("id2", "value2")...etc.
    )*/

    $haystack_top = array();

    foreach ($result as $row) {
        $id = $row[0]; //Not used currently
        $value = $row[1];

        for($i=0; $i <= $value; $i++) {
            if (check_value($i)) {
                $haystack_value = get_new_value($i);
                $haystack_top[$value][] = $haystack_value;
            }
        }
    }

    $needle = get_needle();

    $result = array();

    foreach ($haystack_top as $value=>$haystack) {
        if (in_array($needle, $haystack)) {
            $result[] = $value;
        }
    }

    return array_unique($result);
}

?>

这是我在 Vb.NET 中工作的一个较旧的、未完成的副本。它不是我真正需要的形式,因为我在构建 PHP 示例时更改了逻辑的工作方式,但它显示了我在使用数组时遇到的困惑在 VB.NET 中。

Function Program_Search() As String()
    Dim program_results As Object = Get_Results("SELECT ID, Programs FROM tbMetrics ORDER BY ID ASC", strConnectionString)

    'Create an array of strings to fill with potential field results.
    Dim fields As List(Of String) = New List(Of String)()

    For Each row As Object In program_results
        Dim strID As String = row(0).ToString
        Dim strPrograms As String = row(1).ToString

        Dim intPrograms As Integer = CInt(strPrograms)

        'Convert number to binary string (reversed)
        Dim strReversed As String = StrReverse(Convert.ToString(intPrograms, 2))

        For index As Integer = 0 To ddlPrograms.Items.Count - 1
            If (strReversed.Length - 1) >= index Then
                If strReversed(index) = "1" Then
                    fields.Add(ddlPrograms.Items(index).Value.ToString)
                End If
            End If
        Next index
    Next row

    Dim programs As String() = fields.ToArray

    Dim results As String()

    If programs.Contains(ddlPrograms.SelectedValue.ToString) Then

    End If

    Return programs
End Function

因为有人对 Get_Results 函数感到好奇,所以这里是它的代码。

'Runs the passed query and returns each row as an object within an ArrayList
    Function Get_Results(ByVal query As String, ByVal ConnectionStringName As String) As ArrayList
        Dim sqlComm As Data.SqlClient.SqlCommand = Get_Connection(query, ConnectionStringName)

        'Open that connection
        sqlComm.Connection.Open()

        'Execute the query and store all of the results into the SqlDataReader.
        Dim sqlRead As Data.SqlClient.SqlDataReader = sqlComm.ExecuteReader()

        Dim result As ArrayList = New ArrayList

        'Read each row one by one.
        While sqlRead.Read()
            'Create an object of the size needed.
            Dim row(sqlRead.FieldCount - 1) As Object

            'Fill the row object with the values.
            sqlRead.GetValues(row)

            'Add the result object to the array.
            result.Add(row)
        End While

        'Close all open connections to the database.
        sqlRead.Close()
        sqlComm.Connection.Close()

        Return result
    End Function
4

1 回答 1

2

基于此评论“......老实说,我只是很困惑如何在 VB.NET 中正确创建动态多维数组,以及如何像在我的 PHP 示例中那样使用它们......”,我会提供我的答案。

您的线路:

 Dim fields As List(Of String) = New List(Of String)()

创建一个列表,而不是一个数组。列表更易于使用,因为您可以添加项目而无需调整大小。List(Of MyObject)如果你想这样做而不是二维数组,你可以做一个。只需创建一个包含两个字段的类并列出这些对象。你的课程大约有 4 行代码。

要创建一个 2D 数组,就像你正在做的那样,你应该在这里找到你需要的一切。

http://msdn.microsoft.com/en-us/library/d2de1t93(v=vs.90).aspx

不幸的是,要创建动态 2D 数组,没有简化的方法可以做到这一点。如果我没记错的话,您必须将元素复制到另一个数组。这很蹩脚,我知道。这就是为什么我喜欢这种List(Of MyObject)方法,因为您可以MyList.Add(myObject)在创建新项目时使用。

如果您需要稍后遍历项目以检索值,则可以使用 for 循环并按如下方式访问它们:

MyList(i).MyObjectPropertyName1
MyList(i).MyObjectPropertyName2

VB.NET 可能是可读性更强的语言之一,但对于任何一种语言,您都必须习惯它。我看着你的 PHP,我的头几乎要爆炸了。我敢肯定,如果我花几个小时学习语法,它会(更)有意义。

于 2012-05-16T14:19:25.427 回答