Search This Blog

Thursday 27 October 2011

Dictionary Objects with Examples


Dictionary objects:-

          Vbscript can support dictionary concept to store your required data as pairs. In every pair first value called as “KEY” and second value called as “ITEM”.

            In a dictionary keys must be unique,because items will be accessable by using  keys.


                                
           
            To create a dictionary ,
                                                            Option explicit
                                                            Dim d
                                                            Set d=createobject(“scripting.dictionary”)

*.To store pairs of data to dictionary, we can use below code.

            Static                                                                           Dynamic

Option explicit                                                            option explicit
Dim d                                                                          dim d,i,j,k
Set d=createobject(“scripting.dictionary”)     set d=createobject(“scripting.dictionary)
d.add “English”,98                                                     for i=1 to 3
d.add “telugu”,78                                                                   j=inputbox(“enter key”)
d.add “maths”,99                                                                    k=inputbox(“enter item”)
                                                                                                d.add j,k
                                                                                    next


           




Ex:-1
                        To create a dictionary ,to store subject names and marks and then calculate total marks.

                       
Option explicit
                        Dim marklist, i, j, k, total, nos, x, y
                        Set marklist=createobject(“scripting.dictionary”)
                        Nos=inputbox(“no:of subjects”)
                        For i=1 to nos
                                    J=inputbox(“enter the subject name”)
                                    K=inputbox(“enter the subject markx”)
                                    Marklist.add j,k
                        Next
                        Total=0
                        X=marklist.items
                        For each y in x
                                    Total=total+y
                        Next
                        Print(“total marks” &total)
                        Set marklist=nothing

            Ex:-2
                        To create dictionary , to store specify no:of subjects and marks and then display given dictionary.

                                    Option explicit
                                    Dim d, i, j, k, nos, x, y
                                    Set d=createobject(“scripting.dictionary”)
                                    Nos=inputbox(“enter no:of subjects”)
                                    For i=1 to nos
                                                J=inputbox(“enter the subject names”)
                                                K=inputbox(“enter the subject marks”)
                                                d.add j,k
                                    next
                                    x=d.keys
                                    for each y in x
                                                print(y&” “&d.item(y))
                                    next
                                    set d=nothing

1 comment: