《ASP中字典的用法.txt》由会员分享,可在线阅读,更多相关《ASP中字典的用法.txt(6页珍藏版)》请在得力文库 - 分享文档赚钱的网站上搜索。
1、 VBDictionary Dictionary飬黹1 Dictionary /DictionaryCompareMode CompareMode VBScript趨 Count Dictionary/ -10Item(key) 趨 Key(key) 趨 Add(key,item) /Dictionary Exists(key) TrueFalse Items() Dictionary Keys() Dictionary Remove(key) / RemoveAll() / 2 Dictionary Dictionary
2、 In VBScript: objMyData.Add MyKey, MyItemobjMyData.Add YourKey, YourItemblnIsThere = objMyData.Exists(MyKey)strItem = objMyData.Item(YourKey)strItem = objMyData.Remove(MyKey) objMyData.RemoveAll3 洢DictionaryMyKey ObjMyData.Item(MyKey) = NewValue In VBScript ObjMyData.Item(MyKey) = NewValue; / In JSc
3、ript DictionaryDictionaryMyKeyNew Value/Dictionary/KeyMyKeyMyNewKey objMyData.Key(MyKey) = MyNewValue In VBScriptobjMyData.Item(MyKey) = MyNewValue; / In JScript 4 DictionaryCompareModeVBScriptJScriptBinaryCompare(0)TextCompare(1)BinaryCompare(0)TextCompare(1)5 Dictionary Dictionary磬objMyDataDiction
4、aryCountDictionary/VBScriptJScript00Count-1In VBScript: arrKeys = objMyData.Keys Get all the keys into an arrayarrItems = objMyData.Items Get all the items into an array For intLoop = 0 To objMyData.Count C1 Iterate through the array StrThisKey = arrKeys(intLoop) This is the key value StrThisItem =
5、arrItems(intLoop) This is the item (data) value Next / In JScript / Get VB-style arrays using the Keys() and Items() methods var arrKeys = new VBArray(objMyData.Keys().toArray(); var arrItems = new VBArray(objMyData.Items().toArray(); for (intLoop = 0; intLoop objMyData.Count; intLoop+) / Iterate th
6、rough the arrays strThisKey = arrKeysintLoop; / This is the key value strThisItem = arrItemsintLoop; / This is the item (data) value VBScriptFor Each Next arrKeys = objMyData.Keys arrItems = objMyData.ItemsFor Each objItem in arrItems Response.Write objItem & = & arrItems(objItem) & Next 5.3.2 Dicti
7、onary VBScriptJScriptChapter055-2Dictionaryshow_dictionary.asp档Dictionary5-31 Dictionaryglobal.asa Dictionaryglobal.asaDictionaryDictionaryglobal.asaglobal.asa3Chapter05dictionaryPropertiesHome Direcglobal.asaScripting.DictionarySession_onStartAddDictionaryDictionaryASPMyD
8、ictionary Sub Session_onStart() objBookList.Add 2610, Professional Active Server Pages 3.0 objBookList.Add 1274, Instant JavaScript objBookList.Add 2882, Beginning ASP Components objBookList.Add 1797, Professional ASP Techniques objBookList.Add 1835, AD0 2.0 Programmers Reference Set Session(MyDicti
9、onary) = objBookList End Sub 2 Dictionary Scripting.Dictionary ObjectDictionaryVBScriptSetglobal.asaASP Iterating the Dictionary with Arrays % arrKeysArray = objMyData.Keys get all the keys into an array arrItemsArray = objMyData.Items get all the items into an array For intLoop =
10、0 To objMyData.Count - 1 iterate through the array Response.Write Key: & arrKeysArray(intLoop) & Value: _ & arrItemsArray(intLoop)& Next % Other code and controls go here DictionaryDictionaryKeyItems飬 3 Dictionary DictionaryHTML趨DictionaryACTION棬档 FORM ACTION= METHOD=POST The Dictionary Properties D
11、ictionary.Key ( % For Each objItem in objMyData Response.Write & objItem Next % ) = Other controls go here 4 Dictionary Scription.Dictionary Object棬鲢Keyİ5-5 档鱻İResquest.FormcmdChangKeyKey look for a command sent from the FORM section buttons If Len(Request.Form(cmdChangeKey) Then strKeyName = Reques
12、t.Form(lstChangeKey) Existing key from list box strNewKey = Request.Form(txtChangeKey) New key value from text box objMyData.Key(strKeyName) = strNewKey Set key property of this item End If Dictionary5-6 趨ItemDictionaryKeyDictionary If Len(Request.Form(cmdChangeItem) Then strKeyName = Request.Form(l
13、stChangeItem) Existing key from list box strNewValue = Request.Form(txtChangeItem) New item value from text box objMyData.Item(strKeyName) = strNewValue Set the Item property End If If Len(Request.Form(cmdAdd) Then strKeyName = Request.Form(txtAddKey) New key value from text box strItemValue = Reque
14、st.Form(txtAddItem) New item value from text box objMyData.Add strKeyName, strItemValue Execute the Add method End If If Len(Request.Form(cmdRemove) Then strKeyName = Request.Form(lstRemove) Existion key from list box objMyData.Remove strKeyName Execute the Remove method End If If Len(Request.Form(c
15、mdRemoveAll) Then objMyData.RemoveAll Execute the RemoveAll method End If 磬AddİDictionary5-7 5-8 CompareMode Dictionary object.CompareMode = compareCompareMode object Dictionary compare compare StrComp compare :Constant Value Description vbBinaryCompare 0 . vbTextCompare 1 . 2 Ids (LCID) Dictionary CompareMode StrComp compare CompareMode : Dim d Set d = CreateObject(Scripting.Dictionary)d.CompareMode = vbTextCompared.Add a, Athens d.Add b, Belgraded.Add c, Cairod.Add B, Baltimore b Dictionary
限制150内