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

匿名 (未验证) 提交于 2019-12-03 00:47:02

问题:

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.

回答1:

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 want a particular constructor, you'll have to write a wrapper that accepts the 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 Visual Studio's rename feature to automatically change the name in the entire solution.



回答2:

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).



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!