[英]How to apply php array non index in vb.net
我想在 vb.net 中創建數組,就像在我的 .php 中一樣。 php中的代碼是這樣的。
$query = ".......";
$query = mssql_query($query );
$data_array = array();
while ($res4 = mssql_fetch_array($query ))
{
$ms_part_cust = $res4['ms_part_cust'];
$jmlRows = $res4['jmlRows'];
$data_array[$ms_part_cust]= $jmlRows;
}
所以如果我需要,我可以在其他地方調用 $data_array[$ms_part_cust] 。 如果在 vb.net 中呢?
您似乎指的是關聯數組。 VB.NET 對應的是一個Dictionary(Of TKey, TValue)
在您的情況下,您似乎需要一個Dictionary(Of String, String)
,其中鍵和值都是字符串。
Dim myDict As New Dictionary(Of String, String)
Dictionary(Of String, String)
為了向Dictionary
添加新項目,您必須調用其Add()
方法,指定鍵及其關聯值。
myDict.Add(<key>, <value>)
'Example:
myDict.Add("User", "Visual Vincent")
如果指定的鍵已存在於Dictionary
,則拋出ArgumentException
,說明指定的鍵已存在。
Dictionary(Of String, String)
刪除項目Dictionary(Of String, String)
同樣,為了從Dictionary
刪除項目,您可以調用其Remove()
方法。 但是,這次您只需指定要刪除的條目的鍵。
myDict.Remove(<key>)
'Example:
myDict.Remove("User")
'Removes anything with the key "User" (if present).
'For example: {"User", "Visual Vincent"}.
Dictionary(Of String, String)
獲取項目Dictionary(Of String, String)
從Dictionary
獲取一個項目就像在 PHP 中一樣,除了使用常規括號()
而不是方括號[]
。
這樣做會調用Dictionary
的底層Item
屬性。
myDict(<key>)
'Example:
Dim myUser As String = myDict("User")
'Returns, if present, for example "Visual Vincent".
如果在Dictionary
找不到您要查找的鍵,它將拋出KeyNotFoundException
。
Dictionary(Of String, String)
的現有項目Dictionary(Of String, String)
覆蓋(又名設置) Dictionary
已經存在的項目也像在 PHP 中一樣完成,但再次使用括號而不是方括號。
myDict(<key>) = <value>
'Example:
myDict("User") = "John Doe"
像上面一樣,這也調用底層Item
屬性,如果未找到指定的鍵,則拋出KeyNotFoundException
。
Dictionary(Of String, String)
有兩種 [推薦] 方法來迭代Dictionary
。 您可以:
遍歷每個鍵,然后使用它來獲取值。
For Each Key As String In myDict.Keys Dim Value As String = myDict(Key) 'Print to console in the format of: ' [key] => [value] Console.WriteLine(Key & " => " & Value) Next
遍歷每個KeyValuePair
,包含鍵和值。
For Each Entry As KeyValuePair(Of String, String) In myDict 'Print to console in the format of: ' [key] => [value] Console.WriteLine(Entry.Key & " => " & Entry.Value) Next
模仿 PHP 行為的唯一方法:
$myArray["myKey"] = "myValue"
...添加或覆蓋項目,是創建一個單獨的方法(例如擴展方法)。
該方法必須檢查指定的鍵是否已經存在。 如果是,那么它應該覆蓋與指定鍵關聯的值。 如果鍵不存在,則該方法應向Dictionary
添加一個新項目。
下面是一個擴展方法的例子(需要放在單獨的代碼文件中,去Add > New item... > Module
):
Imports System.Runtime.CompilerServices
Public Module Extensions
''' <summary>
''' Adds a new key/value-pair to the dictionary if the specified key is not present, otherwise overwrites the existing value.
''' </summary>
''' <param name="Key">The key of the item to add or overwrite in the dictionary.</param>
''' <param name="Value">The value to add or overwrite in the dictionary.</param>
''' <remarks></remarks>
<Extension()> _
Public Sub AddOrSet(Of TKey, TValue)(ByVal TargetDictionary As Dictionary(Of TKey, TValue), ByVal Key As TKey, ByVal Value As TValue)
If TargetDictionary.ContainsKey(Key) = True Then
TargetDictionary(Key) = Value
Else
TargetDictionary.Add(Key, Value)
End If
End Sub
End Module
用法:
myDict.AddOrSet(<key>, <value>)
'Example:
myDict.AddOrSet("User", "John Doe")
'Overwrites "User"'s value if the key exists, otherwise adds it as a new item (key/value-pair) to the dictionary.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.