Solved

dictionary object example needed

Posted on 2001-08-03
13
448 Views
Last Modified: 2008-03-10
Hi,

Who can show me an example code that uses dictionary object? I hope it includes all properties and methods of the object.

I have check the help file and found that not help:-(

Thank you in advance.
0
Comment
Question by:johnwood
  • 6
  • 4
  • 2
  • +1
13 Comments
 
LVL 22

Accepted Solution

by:
CJ_S earned 63 total points
ID: 6348404
here's a start

Dim d as Dictionary
Set d = New Dictionary
d.Add "keyname", "itemvalue"
d.Add "keyname2", "itemvalue2"

b = d.items
For i = 0 To b.Count -1
    MsgBox b(i)
Next

If(d.Exists("keyname2")) Then MsgBox "keyname2 exists!!"


0
 
LVL 15

Expert Comment

by:mohan_sekar
ID: 6348423
Hi,

     some more methods and properties

---------------- Keys--------------------------
Dim a, d, i             'Create some variables
Set d = CreateObject("Scripting.Dictionary")
d.Add "a", "Athens"     'Add some keys and items.
d.Add "b", "Belgrade"
d.Add "c", "Cairo"
a = d.keys              'Get the keys
For i = 0 To d.Count -1 'Iterate the array
    Print a(i)          'Print key
Next

------------------- RemoveAll ---------------------
Dim a, d, i             'Create some variables
Set d = CreateObject("Scripting.Dictionary")
d.Add "a", "Athens"     'Add some keys and items
d.Add "b", "Belgrade"
d.Add "c", "Cairo"
a = d.RemoveAll         'Clear the dictionary

------------------ Items-------------------------------
Dim a, d, i             'Create some variables
Set d = CreateObject("Scripting.Dictionary")
d.Add "a", "Athens"     'Add some keys and items
d.Add "b", "Belgrade"
d.Add "c", "Cairo"
a = d.Items             'Get the items
For i = 0 To d.Count -1 'Iterate the array
    Print a(i)          'Print item
Next

-------------------- Remove -------------------------
Dim a, d, i             'Create some variables
Set d = CreateObject("Scripting.Dictionary")
d.Add "a", "Athens"     'Add some keys and items
d.Add "b", "Belgrade"
d.Add "c", "Cairo"
a = d.Remove()          'Remove second pair


------------------ Count------------------------------
Dim a, d, i             'Create some variables
Set d = CreateObject("Scripting.Dictionary")
d.Add "a", "Athens"     'Add some keys and items.
d.Add "b", "Belgrade"
d.Add "c", "Cairo"
a = d.Keys              'Get the keys
For i = 0 To d.Count -1 'Iterate the array
    Print a(i)          'Print key
Next


I hope this will help you

bye


0
 
LVL 1

Expert Comment

by:mcbeth
ID: 6348448
try this

Option Explicit
Dim isShortKey As Boolean
Dim xDic As New Dictionary

Private Sub Form_Load()

xDic.Add "a", "today "

End Sub

Private Sub Text1_KeyPress(KeyAscii As Integer)
Static pzTest As String
If isShortKey Then
    pzTest = Chr(KeyAscii)
    If xDic.Exists(pzTest) Then
        Text1 = Replace(Text1, "\", xDic.Item(pzTest), 1, , vbTextCompare)
        KeyAscii = 0
        Text1.SelStart = Len(Text1)
        Text1.SelLength = 0
    End If
    isShortKey = False
End If
If KeyAscii = 92 Then
    isShortKey = True
    pzTest = "\"
End If
End Sub

you have to reference the MS scripting runtime to get the
dictionary object. then place your table data in the dictornary object only character after \ will be used
if item is not found nothing happens (e.g. 1\2)
0
 
LVL 1

Expert Comment

by:mcbeth
ID: 6348452
sorry..
0
 

Author Comment

by:johnwood
ID: 6349571
Wow....
Then what will be printed by:
For i = 0 To d.Count - 1
Print d(i)
Next i
0
 

Author Comment

by:johnwood
ID: 6349624
Hi,

What difference between the following two ways to create dictionary object:

Dim d as Dictionary
Set d = New Dictionary
and
Set d = CreateObject("Scripting.Dictionary")


0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 15

Expert Comment

by:mohan_sekar
ID: 6351183
Hi john,

         There r 2 types of bindings available. Compile time and Run time bindings.

         set d=new Dictionary - Compile time binding.

         set d=CreateObject("scripting.Dictionary") - Run time binding.

bye

Mohan
0
 

Author Comment

by:johnwood
ID: 6351520
Hi mohan_sekar,

Thank you.
Do you mean earlier bound and later bound?
0
 
LVL 15

Expert Comment

by:mohan_sekar
ID: 6351575
Hi john,

    It's not bound. It's binding.

    Early binding - The compiler will identify the type of the object during the compile time itself.

    Late binding - The compiler will identify the type of the object during the run time only.

That's it. I am giving more elaborate answers for your question below. I hope

-----------------------------------------------------------
                       Example 1

                   Early vs. late binding
by Mike Parry
Visual Basic includes two kinds of binding: early and late. Which one you use depends on whether the object you're working with has a type library. If it doesn't, you'll use late binding, and your code will include a generic reference to the object, such as


Dim X as Object


When the client is compiled, the compiler knows nothing about the object. It must figure out any reference to the object's methods and properties later, when the object is actually contacted. If you call a method using late binding, as we do with the Turn method in the article "Come Fly the Friendly Skies of Visual Basic 5.0" on page XX, the client first calls the GetIDsOfNames function in the default interface of the object and passes the Turn method name. The server returns the DispatchID or DispID of the Turn method. The client must now pass the DispID to the server's Invoke function. This need to make two calls to accomplish anything accounts for the slowness of late binding, but it also allows the X object variable to be a Word document at one point in an application and an Excel application in another.
Early binding, on the other hand, comes in two flavors: DispID and VTable binding. VTable binding is faster, but it requires that an object support dual interfaces. Early binding is accomplished when a reference to an object's type library is set and an explicit object reference is created with code, such as


Dim X as Airplane.Cockpit


Then, when you use code like X.Turn, the compiler retrieves either the DispID of the Turn method (as in DispID binding) or a pointer to the location of the Turn method in the dual interface (as in VTable binding). Both forms of early binding require you to make only one call to the object. While the Invoke function is still called in DispID binding, a faster, more direct call is made to the method in VTable binding.
==========================================================
                     Example 2

                   Early vs. Late Binding

Article contributed by Dave Rado
 

There are two ways to use Automation (or OLE Automation) to programmatically control another application.

Late binding uses CreateObject to create and instance of the application object, which you can then control.  For example, to create a new instance of Excel using late binding:

Dim  oXL As Object
Set oXL = CreateObject("Excel.Application")

On the other hand, to manipulate an existing instance of Excel (if Excel is already open) you would use GetObject (regardless whether you're using early or late binding):

Dim  oXL As Object
Set oXL = GetObject(, "Excel.Application")

To use early binding, you first need to set a reference in your project to the application you want to manipulate.  In the VB Editor of any Office application, or in VB itself, you do this by selecting Tools + References, and selecting the application you want from the list (e.g. ?Microsoft Excel 8.0 Object Library?).

To create a new instance of Excel using early binding:

Dim  oXL As Excel.Application
Set oXL = New Excel.Application

In either case, incidentally, you can first try to get an existing instance of Excel, and if that returns an error, you can create a new instance in your  error handler.
 

Advantages of Early Binding
1.
 Your code will run considerably faster, because it can all be compiled up front.  With late binding, the code relating to an application you declared as an object has to, in effect, be compiled as it runs.
 
 
2.
 Because your code can all be compiled up front, debugging is far easier ? select Debug + Compile, and the compiler will be able to spot syntax errors which would have been missed had you used late binding.
 
 
3.
 You have full access in your project to intellisense (type a keyword and a dot to get a popup list of properties and methods supported by that keyword, select one to insert it;  type a keyword and press F1 to launch the Help topic on that keyword).
 
 
4.
 You have full access to the application's object model via the Object Browser and VBA Help.
 
 
5.
 You have access to the application's built-in constants.  For instance, if you are automating Word from Excel, you can use:

Dim objWord As Word.Application
Set objWord = New Word.Application

With objWord
    .Visible = True
    .Activate
    .WindowState = wdWindowStateMaximize
    .Documents.Open ("c:\temp\temp.doc")
End With

Furthermore, when you type

.WindowState =

you'll get a pop-up list of the supported constants, and can simply pick ?wdWindowStateMaximize? from the list.

If you used late binding, you would need to use:

    .WindowState = 1

.. and you would need to know (by looking it up in Word's Object Browser) that the value of the constant ?wdWindowStateMaximize? happens to be 1.
 

All this makes programming using early binding immeasurably easier than using late binding.
 

Advantages of Late Binding
1.
 The main advantage is that code which uses late binding is more certain to be version-independent

If you set a reference in a Word 97 project to ?Microsoft Excel 8.0 Object Library?, then the project willrun OK on a machine which has Office 2000 installed.  Word 2000 changes the reference on the fly to the  ?Microsoft Excel 9.0 Object Library?.

But as they famously say, YMMV.  Problems have been found in certain circumstances.  For instance, if you run a Word 97 project containing a reference to the Excel 8.0 object library on a machine with Office 2000 installed, it will run OK, but you may get the occasional ?cannot open macro storage? error unless you save the project in  Word 2000.  If you do save it in Word 2000, the reference will change to the Excel 9.0 object library.  So if you use early binding and support a mixed environment, it may be safest to create separate Word 97 and Word 2000 versions of your addins, despite the maintenance overhead.
 
 
2.
 The more references your project contains, the larger the file size and the longer it takes to compile.
 
 
3.
 Some programming environments don't allow you to create references to another application.
 
-----------------------------------------------------------

:-)

bye

Mohan
0
 

Author Comment

by:johnwood
ID: 6353198
Hi Mohan and all experts here,

I don't think the difference is early and late binding for
Dim d as Dictionary
Set d = New Dictionary
and
Set d = CreateObject("Scripting.Dictionary")

To be more clearly, I guess that both:
Dim d as Dictionary
Set d = New Dictionary
and
Dim d as Dictionary
Set d = CreateObject("Scripting.Dictionary")
are early binding, and both
Dim d as Object
Set d = New Dictionary
and
Dim d as Object
Set d = CreateObject("Scripting.Dictionary")
are late binding.

My question: what is the difference between
Set d = New Dictionary
and
Set d = CreateObject("Scripting.Dictionary")







So, what is this:

Dim d As Dictionary
Set d = CreateObject("Scripting.Dictionary")

early or late binding?
Dim d as Dictionary
Set d = New Dictionary
0
 

Author Comment

by:johnwood
ID: 6353204
Ooops, please ignore the last stuff beging with
'So, what is this:'

0
 
LVL 15

Expert Comment

by:mohan_sekar
ID: 6355098
Hi,

   Sorry. Dim d as new Dictionary comes under compile time binding.  erratum regretted.

bye
:-(

Mohan
0
 

Author Comment

by:johnwood
ID: 6359310
Hi,

I have asked CS to split the points for CJ_S and mohan_sekar. Now CS has reduce to half of original points.
 
I am going to give it to CJ_S who posted the first comment.

I will post a new question for the rest points for mohan_sekar.

Thanks for all experts helped me:-)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now