English 中文(简体)
设法将一些PHP阵列代码改为VB。 NET
原标题:Trying to convert some PHP array code to VB.NET

EDIT

因此,在经过一段时间的工作之后,我就提出了一个行之有效的解决办法,而且比我原先认为更清洁。 感谢这些帮助。 这部法典。

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

<>ENDIT

ORIGINAL BELOW

Ok, so I m a PHP programmer,力图学习一些VB。 NET。 Arrays在VB对我大火。 NET,因此,我以我知道如何做到这一点的方式,在住房和财产管理系统中写了一些例子。 如果一些发言者能够向我表明它将在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的Arrays混为一谈。 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
最佳回答

Based on this comment "...to be honest, I m just confused how to properly create dynamic multi-dimensional arrays in VB.NET, and how to use them like I do in my PHP example...", I ll provide my answer.

阁下:

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

编制一份清单,而不是一个阵列。 清单比较容易使用,因为你可以增加物品而不必再进行消化。 如果你想这样做,而不是2D阵列的话,你可以提出<代码>List(Of MyObject)

To create a 2D array, which is what it looks like you re doing, you should find everything you need here.

不幸的是,为了建立一个充满活力的2D阵列,没有这样做的简化方法。 如果我不错的话,你必须把内容复制到另一个阵列。 我知道,这只是空话。 因此,我喜欢<代码>List(Of MyObject)的做法,即:在你创建新项目时,你只能读到<代码>MyList.Add(myObject)。

If you need to iterate through the items later to retrieve the values, you can just use a for-loop and access them as such:

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

VB. 该网络可能是更可读语言之一,但如有的话,你必须加以使用。 我看着你的购买力平价和我的头部几乎爆炸。 我确信,如果我花了几个小时,并汲取了辛迪加的话,那就更有意义。

问题回答

暂无回答




相关问题
Is Shared ReadOnly lazyloaded?

I was wondering when I write Shared ReadOnly Variable As DataType = New DataType() Or alternatively Shared ReadOnly Variable As New DataType() Is it lazy loaded or as the instance initializes? ...

Entertaining a baby with VB.NET

I would like to write a little application in VB.NET that will detect a baby s cry. How would I get started with such an application?

Choose Enter Rather than Pressing Ok button

I have many fields in the page and the last field is a dropdown with list of values. When I select an item in a dropdown and press Enter, it doesn t do the "Ok". Instead I have to manually click on Ok ...

ALT Key Shortcuts Hidden

I am using VS2008 and creating forms. By default, the underscore of the character in a textbox when using an ampersand is not shown when I run the application. ex. "&Goto Here" is not ...

Set Select command in code

On button Click I want to Set the Select command of a Gridview. I do this and then databind the grid but it doesn t work. What am i doing wrong? protected void bttnView_Click(object sender, ...

Hover tooltip on specific words in rich text box?

I m trying to create something like a tooltip suddenly hoovering over the mouse pointer when specific words in the richt text box is hovered over. How can this be done?

热门标签