Search This Blog

Thursday 27 October 2011

Dictionary objects methods "Add()","Item()","Keys()","Items()","key",Remove()","RemoveAll()","Exist()" & Examples


Add( ):-
                        We can use this method ,to “Add” a pair to dictionary.

Items( ):-
                   We can use this method , to get all “Items” in dictionary into array/list.

Keys( ):-
                   We can use this method, to get all “keys” in a dictionary into an array/list.

Item( ):-
                   We can use this property to get item of specified key.

                                                Syntax:-  dictionary.item(key)
EX:-3
            To create a dictionary, to store specify no:of subjects names and marks, then to display highest marks.

                                    Option explicit
                                    Dim marklist, i, j, high, nos, x,y
                                    Set marklist=createobject(“scripting.dictionary”)
                                    Nos=inputbox(“enter the no:of subjects”)
                                    For i=1 to nos
                                                J=inputbox(“enter the subject name”)
                                                K=inputbox(“enter the marks”)
                                                Marklist.add j,k
                                    Next
                                    X=marklist.items
                                    Max=marklist.item(“English”)
                                    For each y in x
                                                If (max<y) then
                                                            Max=y
                                                End if
                                    Next
                                    Print(max)

EX:-4
            To Display the employee name and his salary , who’s sal is highest in dictionary.
                                   
                                    Option explicit
                                    Dim noe, sal, i, j, k, x, y, emp
                                    Set emp=createobject(“scripting.dictionary”)
                                    Noe=inputbox(“enter the no:of emp’s)
                                    For i=1 to noe
                                                J=inputbox(“enter the emp name”)
                                                K=inputbox(“enter the emp sal”)
                                                Emp.add j, k
                                    Next
                                    X=emp.items
                                    Max=emp.item(“ram”)
                                    For each y in x
                                                If(clng(max)<y) then
                                                            Max=y
                                                End if
                                    Next
                                    X=emp.keys
                                    For each y in x
                                                If(clng(max)<emp.item(y))
                                                            Print(“emp name”&y”sal”&max)
                                                End if
                                    Next
                                    Set emp=nothing


Key:-
          We can use this property to change existing key to new key.

                        EX:-    set d=createobject(“scripting.dictionary”)
                                    d.add “math”,124
                                    --------------------
                                    --------------------
                                    --------------------
                                    d.key(“math”)=”mathes”
                                    --------------------
                                    --------------------

Remove( ):-
                        We can use this method to remove specified pair from dictionary.

                        EX:-    set d=createobject(“scripting.dictionary”)
                                    d.add “maths”,123
                                    d.add “physics”,232
                                    ------------------
                                    ------------------
                                    d.remove(“maths”)

Removeall( ):-
                   We can use this method to remove all pair in dictionary.

                        EX:-     set d=createobject(“scripting.dictionary”)
                                    d.add “maths”,123
                                    d.add “physics”,89
                                    d.add “social”,322
                                    -------------------
                                    -------------------
                                    d.removeall

Exist( ):-
                   We can use this method to verify the existence of specified pair in dictionary.

                        Ex:-    set d=createobject(“scripting.dictionary”)
                                    D.add “maths”,322
                                    d.add “physics”,89
                                    ------------------
                                    ------------------
                                    If d.exist(“maths”) then
                                                Print(“maths available”)
                                    End if

Note:-
            The data structures like variables ,arrays ,and dictionary are temporary ,because they are creating in ram. Due to this reason testers are using files, because files are saving in hard disk.

2 comments: