Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

vb.net

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

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

I have to type the above line for so many times. If I changed the collection type, then I have to make the change for all the instances. So if there is a way to use "a typedef equivalent", then I can get rid of all these problems. I tried with imports, but it is only for namespaces and it can't be used for classes. Any helping hand will be greatly appreciated.

Note: I am using VB 2008, windows XP. The application type is windows forms(VB).

EDIT: I made some tries based on code_gray's below answer.

This is the first try.

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

I got the error as

Error:Type 'WaterBill' is not defined.

This is try 2.

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

I got the error as

Error:'Imports' statements must precede any declarations.

Anybody is welcome to shed a light on this issue.

like image 897
prabhakaran Avatar asked Dec 22 '11 06:12

prabhakaran


1 Answers

The simple solution is simply to add an Imports statement for the two namespaces that you're using. Right off the bat, that eliminates half of your long type identifiers.

At the top of your code file, place the lines:

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

And then your declaration can be changed to:

Dim sub_Collection As Dictionary(Of String, Collection)

I'd recommend this approach because it still uses the standard names, which keeps your code easy to read for other programmers.


The alternative is to use an Imports statement to declare an alias. Something like:

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

And then you could change your declaration to:

Dim sub_Collection As GenericDict


*** By the way, in order to get either of these samples to compile, you'll have to specify a type for the Collection, such as Collection(Of String).

like image 175
Cody Gray Avatar answered Oct 21 '22 23:10

Cody Gray