VB to .NET

VB.Collection Special Maps

In VB6 the Collection class has a complex behavior which could be compared to a combination of the following .NET classes: HashTable, ArrayList, StringCollection and StringDictionary. Microsoft decided to implement an equivalent class in the Microsoft.VisualBasic library. The default behavior of VBUC is to convert Collection references to that support class.

In some cases, though, the behavior of a particular collection is very consistent and could be identified as a special case of one of the .NET classes mentioned above.

The “VB.Collection Special Maps” feature includes some heuristic transformations intended to recognize this particular patterns and generate .NET native classes to avoid the usage of Microsoft.VisualBasic.Collection as much as possible.

The usage of this particular functionality will cause the output code to be more .NET like yet the resulting code behavior might not be completely functionally equivalent.

The resulting .NET structure will be determined by the contents of the collection and how these contents are accessed. The following chart describes how the resulting structure will be selected.

Element Type Accessed By Resulting Structure
String Index StringCollection
String Key value StringDictionary
Other Index ArrayList
Other Key Value HashTable

Since this technique implicates a lot of small details and it might produce some different behavior, this feature is disabled by default, which means the resulting code would use the .NET collection (Microsoft.VisualBasic) compatibility class. This is a brief usage example:

Original VB6 Code:

Public Sub ArrayListDemo()
'This method will generate an ArrayList class
Dim collection1 As New Collection
collection1.Add 10
collection1.Add 9
...
collection1.Add 2
collection1.Add 1
End Sub

Public Sub HashTableDemo()
'This method will generate a HashTable class
Dim collection1 As New Collection
collection1.Add 1, "first"
collection1.Add 2, "second"
...
collection1.Add 9, "ninth"
collection1.Add 10, "tenth"
End Sub

Public Sub StringDictionaryDemo()
'This method will generate a StringDictionary class
Dim collection1 As New Collection
collection1.Add "first", "one"
collection1.Add "second", "two"
...
collection1.Add "ninth", "nine"
collection1.Add "tenth", "ten"
End Sub

Public Sub StringCollectionDemo()
'This method will generate a StringCollection class
Dim collection1 As New Collection
collection1.Add "first"
collection1.Add "second"
...
collection1.Add "ninth"
collection1.Add "tenth"
End Sub

Resulting VB.NET Code:

Public Sub ArrayListDemo()
'This method will generate an ArrayList class
Dim collection1 As New ArrayList
collection1.Add(10)
collection1.Add(9)
...
End Sub

Public Sub HashTableDemo()
'This method will generate a HashTable class
Dim collection1 As New Hashtable
collection1.Add("first", 1)
collection1.Add("second", 2)
...
End Sub

Public Sub StringDictionaryDemo()
'This method will generate a StringDictionary class
Dim collection1 As New StringDictionary
collection1.Add("one", "first")
collection1.Add("two", "second")
...
End Sub

Public Sub StringCollectionDemo()
'This method will generate a StringCollection class
Dim collection1 As New StringCollection
collection1.Add("first")
collection1.Add("second")
...
End Sub

Resulting C#.NET Code:

public void  ArrayListDemo(){
//This method will generate an ArrayList class
ArrayList collection1 = new ArrayList();
collection1.Add(10)
collection1.Add(9)
...
}
public void HashTableDemo(){
//This method will generate a HashTable class
Hashtable collection1 = new Hashtable();
collection1.Add("first", 1)
collection1.Add("second", 2)
...
}
public void StringDictionaryDemo(){
//This method will generate a StringDictionary class
StringDictionary collection1 = new StringDictionary();
collection1.Add("one", "first")
collection1.Add("two", "second")
...
}
public void StringCollectionDemo(){
//This method will generate a StringCollection class
StringCollection collection1 = new StringCollection();
collection1.Add("one", "first")
collection1.Add("two", "second")
...
}
Talk To An Engineer