編集
わかりました、これでしばらく作業した後、私は機能する解決策を思いつきました。それは私が最初に考えていたよりもきれいです。助けてくれてありがとう。これがコードです。
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
編集終了
以下のオリジナル
OK、私は 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