How are Dictionaries different from simple collections such as ArrayList, Stack and Queue?
Copyright © 2008 Department of Education - Introduction to Visual Basic – VB.Net Page 181
Public Sub New size = 0
capacity = 3 ReDim elementscapacity - 1
End Sub Public Function PushByVal element As Object As Object
If size = capacity Then Expand
End If elementssize = element
size += 1 Return element
End Function Public Function Pop As Object
size -= 1 Return elementssize
End Function Public Function Peek As Object
Return elementssize - 1 End Function
Private Sub Expand Dim tempcapacity 2 As Object
Dim i As Integer For i = 0 To elements.Length - 1
tempi = elementsi Next
capacity = capacity 2 elements = temp
End Sub End Class
5. How are Dictionaries different from simple collections such as ArrayList, Stack and Queue?
In dictionaries, elements are stored in a key-value pair fashion and each key maps to only one value. On the other hand, An ArrayList and other collections allow access to their
elements in a predefined order like First In First Out FIFO, Last In First Out LIFO. Both the dictionary and simple collections keep track of the size of the collection on behalf of the
programmer. 6. In this text we did not describe which algorithm the Hashtable uses to maintain
the collection internally. Search for the text on rgw internet or in data structures books and write a short description of this algorithm.
A hashtable consists of sub-groups called buckets of elements belonging to the collection. Elements are stored in the hashtable as key-value pairs. An object which is used as a key
should be able to produce a hash-code; an integer, through its GetHashCode method. Elements with the same hash-code are placed in the same bucket. When a value is being
searched for within the Hashtable, the hash code is generated for that value and the bucket associated with that hash code is searched using the Equals method.
Copyright © 2008 Department of Education - Introduction to Visual Basic – VB.Net Page 182
We can see from the short description above that an object which is used as a key should override the System.Object class GetHashCode and Equals methods properly. The
GetHashCode should use a quick algorithm to generate the hash-codes. It is also important that the GetHashCode method produces the same hash-code every time it is
called for the same object, which is the reason why immutable objects are more often used as keys in hashtables. The hashing algorithm may generate the same hash-code for two
different keys but the hashing function that produces a unique hash-code for each key result has better performance. Finally, the Equals method should check for the object
equality rather than the default reference equality as it is used to search for elements in buckets.
We still recommend that the reader search for more text on this very interesting topic in MSDN and on the Internet
7. Why is it advised to properly override the Equals and the GetHashCode methods of a class that is to be used as a key in Hashtable?
The Hashtable class uses the GetHashCode method to find the hashcode for a particular object. This hashcode is used to select the storing bucket for an element. The Equals
method is used when searching for an element in a bucket selected on the basis of its hashcode.
8. What is the basic difference between the String and the StringBuilder class? What is the difference between the Intern and Copy method of the String
class? The String objects are immutable, that is the internal state of the object can not be changed
by any of its operations. The StringBuilder object, on the other hand, are mutable which means that the operations in this class may change the internal state of the object. The
Copy method creates and returns a copy of the supplied string while the Intern method returns the reference to the supplied string.
Whats Next… Next time, we will be discussing delegates and events in VB.Net. We will explore
• Thorough Introduction coverage of concept behind delegates • Declaring and using delegates in your program
• Multi-cast delegates • Events basics
• Implementing events through delegates • Multicasting events
Copyright © 2008 Department of Education - Introduction to Visual Basic – VB.Net Page 183
Copyright © 2008 Department of Education - Introduction to Visual Basic – VB.Net Page 184
Delegates and Events
Lesson Plan In this lesson we will explore the concept of delegates and events. We will start out by
looking at the idea behind delegates and will see how delegates are used in VB.Net. Later we will explore multicast delegates and their significance. Finally we will learn about events
and the event handling mechanism through delegates. Delegates Basics
Delegates are referenced to methods. So far we have used references for objects such as
Dim st As New Stack
In this senario st is a reference to an object of the Stack class type. Each reference has two properties 1. The type of object class, the reference can point to
2. The actual object referenced or pointed by the reference. Delegates are similar to object references but are used to reference methods instead of
objects. The type of a delegates is the type or signature of a method rather than the class. Hence a delegate has three properties 1. The type or signature of the method the delegate
can point to 2. A delegate reference which can be used to reference a method
3. The actual method referenced by the delegate Authors Note: The concept of delegates is similar to the function pointers
used in C++.