What is the equivalent or workaround for typedef in VB.NET?

I am coding a VB.NET application that deals with a collection type. See code below:

Dim sub_Collection As System.Collections.Generic.Dictionary(Of String, System.Collections.ObjectModel.Collection) 

I have to type the above line so many times. If I changed the type of collection, I have to make changes for all instances. Therefore, if there is a way to use the "equivalent of typedef", I can get rid of all these problems. I tried with import, but it is intended only for namespaces and cannot be used for classes. Any help would be greatly appreciated.

Note. I am using VB 2008, Windows XP. An application type is a window form (VB).

EDIT: I made several attempts based on code_gray below the answer.

This is the first attempt.

 Imports dictionary_WaterBill = System.Collections.Generic.Dictionary(Of String, System.Collections.ObjectModel.Collection(Of WaterBill)) Structure WaterBill ... ... End Structure 

I got an error like

 Error:Type 'WaterBill' is not defined. 

This is an attempt 2.

 Structure WaterBill ... ... End Structure Imports dictionary_WaterBill = System.Collections.Generic.Dictionary(Of String, System.Collections.ObjectModel.Collection(Of WaterBill)) 

I got an error like

 Error:'Imports' statements must precede any declarations. 

No one can shed light on this problem.

+7
source share
2 answers

A simple solution is to simply add an Imports statement for the two namespaces that you use. From the start, this excludes half of your long type identifiers.

At the top of the code file, put the lines:

 Imports System.Collections Imports System.Collections.Generic Imports System.Collections.ObjectModel 

And then your ad can be changed to:

 Dim sub_Collection As Dictionary(Of String, Collection) 

I would recommend this approach because it still uses standard names, which makes it easy to read code for other programmers.


An alternative is to use the Imports operator to declare an alias. Something like:

 Imports GenericDict = System.Collections.Generic.Dictionary(Of String, System.Collections.ObjectModel.Collection) 

And then you can change your ad to:

 Dim sub_Collection As GenericDict 


*** By the way, to compile any of these samples, you need to specify a type for Collection , for example Collection(Of String) .

+2
source

Create an inherited class with no content:

 Class WaterBillDictionary Inherits System.Collections.Generic.Dictionary(Of String, System.Collections.ObjectModel.Collection(Of WaterBill)) End Class Structure WaterBill ... End Structure 

If you need a specific constructor, you will have to write a shell that takes parameters and passes them to the base constructor. For example:

 Public Sub New() MyBase.New() End Sub Public Sub New(capacity As Integer) MyBase.New(capacity) End Sub 

If you ever need to change the name of WaterBillDictionary , you can use the Visual Studio rename function to automatically change the name in the whole solution.

+2
source

All Articles