Handles Keyword VB.NET

Syntax
Handles name.event

name Use: Required Type: String literal

The name of the class or object whose event the subroutine is handling

event Use: Required Type: String literal

The name of the event that the subroutine is handling

Description
Defines a procedure as the event handler for a particular event
Rules at a Glance

  • The Handler keyword is used to define event handlers for events trapped by an object defined with the WithEvents keyword.
  • The Handles keyword can only be used with a procedure declaration, since an event handler must be a procedure rather than a function.
  • The Handles keyword must be placed on the same line as, and at the end of, a procedure declaration.

Example

In a Windows application, the following definition appears in the declarations section of the Form1class module:
Public WithEvents Button1 As Button

The Button1 object is then instantiated with a line of code like the following in the New subroutine or another initialization routine:
Me.Button1 = New Button

The Button1 object's Click event can then be handled with a event handler like the following:

Programming Tips and Gotchas

  • The WithEvents and Handles are designed to define event handlers at compile time. If you want to define event handlers dynamically at runtime, use the AddHandler and RemoveHandler statements.
  • By convention, event handlers take the form objectname_eventname. For example, the Click event of an object named Button1 could be trapped by an event handler named Button1_Click. Although this convention is highly recommended, it is not obligatory.

VB .NET/VB 6 Differences
The Handles keyword is new to VB .NET. In VB 6, the link between an object and its event handler
was handled automatically and transparently by Visual Basic.

Hashtable Class

Namespace
System.Collections

Createable
Yes

Description
A Hashtable object represents a collection of values (of type Object) that are indexed by objects called keys (also of type Object). We can also think of a hash table as containing key/value pairs.

Identification of the location of elements in a hash table is done using a hashing function. Simply put, a hashing function is a function that assigns a location in the hash table to each element, based on the element's value. This is not the place to go into any detail about hashing. It is worth mentioning that hash tables can be very efficient structures for storing and retrieving elements. However, there is no "best approach" to defining hashing functions, and so only experimentation can determine whether this particular implementation of a hash table is efficient in any given case.

Note that the Hashtable class is more flexible than the Collection class of the Microsoft.VisualBasic
namespace.

Hashtable class members marked with a plus sign (+) are discussed in detail in their own entries.

Public Shared Method
Synchronized

Public Instance Properties
Count +
IsFixedSize
IsReadOnly
IsSynchronized
Item +
Keys +
SyncRoot
Values +

Public Instance Methods
Add +
Clear +
Clone
Contains
ContainsKey +
ContainsValue +
CopyTo +
Equals
GetEnumerator
GetHashCode
GetObjectData
GetType
OnDeserialization
Remove +
ToString

Example
The following example illustrates most of the members that we will discuss:

VB .NET/VB 6 Differences
The Hashtable object is new to the .NET platform.

Hashtable.Add Method

Class
System.Collections.Hashtable

Syntax
hashtablevariable.Add(Key, Value)

Key Use: Required Data Type: Object

The hash table entry's key

Value Use: Required Data Type: Object

The hash table entry's value

Return Value
None

Description
Adds a key/value pair to the hash table

Rules at a Glance

  • Key must be unique or a runtime error occurs.
  • Keys are immutable. Once added, a particular key value cannot be changed during the lifetime of the hash table except by removing it through the Remove or Clear method and then adding it once again.
  • Value need not be unique.

Programming Tips and Gotchas

  • According to the documentation, it is better to build a key from a String object than the Base Class Library's StringBuilder object.
  • The Item property can also be used to add new members to the hash table.
  • To insure that key is unique when calling the Add method, you can call the ContainsKey method beforehand.

Hashtable.Clear Method

Class
System.Collections.Hashtable

Syntax
hashtablevariable.Clear( )

Return Value
None

Description
Removes all entries from the hash table

Rules at a Glance

  • The Clear method removes all items from the collection, leaving the Hashtable object uninitialized. It does not set the object to Nothing.
  • The Clear method sets the Hashtable object's Count property to 0.

Hashtable.ContainsKey Method
Class
System.Collections.Hashtable

Syntax
hashtablevariable.ContainsKey(Key)

Key Use: Required Data Type: Object

The key to search for among the hash table entries

Return Value
A Boolean indicating whether the key exists (True) or not (False)

Description
Indicates whether a given key is contained in the hash table

Hashtable.ContainsValue Method

Class
System.Collections.Hashtable

Syntax
hashtablevariable.ContainsValue(Value)

Value Use: Required Data Type: Object

The value to search for among the hash table entries

Return Value
A Boolean indicating whether the value exists (True) or not (False)

Description
Indicates whether a given value is contained in the hash table

Programming Tips and Gotchas
ContainsValue is intended to determine whether a value exists in the hash table; it is not designed to
indicate the key belonging to a particular value or to determine whether multiple occurrences of a
particular value exist.

Hashtable.CopyTo Method

Class
System.Collections.Hashtable

Syntax
hashtablevariable.CopyTo(array, arrayindex)

array
Use: Required Data Type: Array of DictionaryEntry structures

The destination of the items copied from the hash table

arrayindex Use: Required Data Type: Integer

The first index that is to receive an element of the hash table

Return Value
None

Description
Copies the hash table values into an array of DictionaryEntry structures. A DictionaryEntry structure is a key/value pair. Note that the array must be sized to accommodate the elements of the hash table prior to calling the CopyTo method.

Rules at a Glance

  • array must be a one-dimensional array.
  • Elements are copied from the hash table to array in the same order in which the hash table is iterated.
  • The CopyTo method copies each key/value pair in the hash table to a DictionaryEntry structure.
  • array, the array of DictionaryEntry structures, must be sized before calling the CopyTo method. This is illustrated in the example.

Example

Hashtable.Count Property

Class
System.Collections.Hashtable

Syntax
hashtablevariable.Count( )

hashtablevariable Use: Required Type: Hashtable object

A reference to a Hashtable object

Return Value
An Integer indicating the number of elements in the hash table

Description
This read-only property returns an Integer specifying the number of elements in the hash table.

Hashtable.Item Property

Class
System.Collections.Hashtable

Syntax
hashtablevariable.Item(key)

hashtablevariable Use: Required Type: Hashtable object

A reference to a Hashtable object

key Use: Required Data Type: Object

The key whose value is to be retrieved

Return Value
An Object representing the value associated with key

Description
Returns an Object that is the value associated with a particular key/value pair.

Rules at a Glance

  • Item is the default property of the Hashtable object, and since it is parameterized, we can write: hashtablevariable(key)
  • Item is a read/write property. In other words, you can use the Item property to retrieve the value belonging to a particular key, as well as to modify the value belonging to a particular key.
  • If key does not exist in the hash table when you attempt to retrieve a value, the Item property returns Nothing.
  • If key does not exist in the hash table when you attempt to modify a value, the key and its associated value are added to the hash table, as a sort of implicit add. For example, if the key "AK" does not exist in a hash table, the code fragment:

hshStates.Item("AK") = "Alaska"

adds the key "AK" and its associated value, "Alaska".

Programming Tips and Gotchas

  • To guard against inadvertently adding a member to the hash table when you intend to modify an existing value, call the ContainsKey method beforehand.
  • You can also retrieve individual members of the Hashtable object by iterating it using the For Each...Next statement. Each iteration of the loop returns a DictionaryEntry object containing a single key/value pair. For information on the DictionaryEntry object, see the entry for the Hashtable.CopyTo method.

Hashtable.Keys Property

Class
System.Collections.Hashtable

Syntax
hashtablevariable.Keys( )

hashtablevariable Use: Required Type: Hashtable object

A reference to a Hashtable object

Return Value
An ICollection interface containing the keys in the hash table

Description
Returns an ICollection interface that contains the keys in the hash table. There is not much we can do with an ICollection object except copy it to an array of Objects using its CopyTo method, as the following example illustrates.

Example

Programming Tips and Gotchas
You can work around the inconvenience of calling the ICollection object's CopyTo method to convert the interface to another object by defining a class that inherits from or implements ICollection.

Hashtable.Remove Method

Class
System.Collections.Hashtable

Syntax
hashtablevariable.Remove(key)

hashtablevariable Use: Required Data Type: Hashtable object

A reference to a Hashtable object

key Use: Required Data Type: Object

The key whose key/value pair is to be removed

Return Value
None

Description
Removes an element from a hash table

Rules at a Glance

  • If key is found in the hash table, the member is removed, and the Count property is decreased by one.
  • If key is not found in the hash table, the hash table remains unchanged, and no exception is thrown.

Programming Tips and Gotchas
For cases in which you need to know whether the call to the Remove method has actually removed a key, you can call the ContainsKey method beforehand to make sure that the key you want to remove actually exists.

Hashtable.Values Property

Class
System.Collections.Hashtable

Syntax
hashtablevariable.Values( )

hashtablevariable Use: Required Data Type: Object

A reference to a Hashtable object

Return Value
An ICollection object containing the values in the hash table

Description
Returns an ICollection object that contains the values in the hash table. There is not much we can do with an ICollection object except copy it to an array of objects.

Hex Function

Class
Microsoft.VisualBasic.Conversion

Syntax
Hex(number)

number Use: Required Data Type: Numeric or String

A valid numeric or string expression

Return Value
String representing the hexadecimal value of number

Description
Returns a string that represents the hexadecimal value of number

Rules at a Glance

  • If number contains a fractional part, it will be automatically rounded to the nearest whole number before the Hex function is evaluated.
  • number must evaluate to a numeric expression that ranges from -2,147,483,648 to 2,147,483,647. If the argument is outside of this range, runtime error 6, "Overflow," results.
  • The return value of Hex is dependent upon the value and type of number:

TABLE

TABLETABLE

Programming Tips and Gotchas
If the value of number is known beforehand and is not the result of an expression, you can represent the number as a hexadecimal by simply affixing &H to number. Each of the following two statements assigns a hexadecimal value to a variable, for instance:

lngHexValue1 = &HFF ' Assigns 255
lngHexValue2 = "&H" & Len(dblNumber) ' Assigns 8

Hour Function

Class
Microsoft.VisualBasic.DateAndTime

Syntax
Hour(timevalue)

timevalue
Use: Required Data Type: Datee

Date variable or literal date

Return Value
An Integer from 0 to 23, specifying the hour of the day

Description
Extracts the hour element from a time expression

Example
The line:

MsgBox(Hour(#1:33:00 PM#)) displays the number 13.

Rules at a Glance

  • Regardless of the time format passed to Hour, the return value will be a whole number between 0 and 23, representing the hour of a 24-hour clock.
  • If time contains Nothing, 0 is returned, so be careful here to check for Nothing.
  • You can also use the DatePart function.


Face Book Twitter Google Plus Instagram Youtube Linkedin Myspace Pinterest Soundcloud Wikipedia

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

VB.NET Topics