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.
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)
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With