VB.NET中typedef的等价或解决方法是什么?

pra*_*ran 8 vb.net

我正在编写一个VB.NET应用程序,它大量处理集合类型.请参阅以下代码:

Dim sub_Collection As System.Collections.Generic.Dictionary(Of String, 
                             System.Collections.ObjectModel.Collection)
Run Code Online (Sandbox Code Playgroud)

我必须多次键入上面的行.如果我更改了集合类型,那么我必须对所有实例进行更改.因此,如果有一种方法可以使用"typedef equivalent",那么我可以摆脱所有这些问题.我尝试使用导入,但它仅用于命名空间,不能用于类.任何帮助之手将不胜感激.

注意:我使用的是VB 2008,Windows XP.应用程序类型是Windows窗体(VB).

编辑: 我根据code_gray的下面的答案做了一些尝试.

这是第一次尝试.

Imports dictionary_WaterBill = System.Collections.Generic.Dictionary(Of String, System.Collections.ObjectModel.Collection(Of WaterBill))
Structure WaterBill
...
...
End Structure
Run Code Online (Sandbox Code Playgroud)

我得到了错误

Error:Type 'WaterBill' is not defined.
Run Code Online (Sandbox Code Playgroud)

这是尝试2.

Structure WaterBill
...
...
End Structure
Imports dictionary_WaterBill = System.Collections.Generic.Dictionary(Of String,     
System.Collections.ObjectModel.Collection(Of WaterBill))
Run Code Online (Sandbox Code Playgroud)

我得到了错误

Error:'Imports' statements must precede any declarations.
Run Code Online (Sandbox Code Playgroud)

欢迎任何人对此问题进行阐述.

Cod*_*ray 3

简单的解决方案就是为您正在使用的两个命名空间添加一条Imports语句。立刻,这消除了一半的长类型标识符。

在代码文件的顶部,放置以下行:

Imports System.Collections
Imports System.Collections.Generic
Imports System.Collections.ObjectModel
Run Code Online (Sandbox Code Playgroud)

然后你的声明可以更改为:

Dim sub_Collection As Dictionary(Of String, Collection)
Run Code Online (Sandbox Code Playgroud)

我推荐这种方法,因为它仍然使用标准名称,这使您的代码易于其他程序员阅读。


另一种方法是使用Imports语句来声明别名。就像是:

Imports GenericDict = System.Collections.Generic.Dictionary(Of String,
                            System.Collections.ObjectModel.Collection)
Run Code Online (Sandbox Code Playgroud)

然后你可以将你的声明更改为:

Dim sub_Collection As GenericDict
Run Code Online (Sandbox Code Playgroud)


*** 顺便说一句,为了编译这些示例中的任何一个,您必须指定 的类型Collection,例如Collection(Of String).