VB.NET 中 typedef 的等效项或解决方法是什么? [英] What is the equivalent or workaround for a typedef in VB.NET?

查看:27
本文介绍了VB.NET 中 typedef 的等效项或解决方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个 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)

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

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.

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

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

我根据 code_gray 的以下答案做了一些尝试.

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

这是第一次尝试.

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

我得到的错误是

Error:Type 'WaterBill' is not defined.

这是尝试 2.

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

我得到的错误是

Error:'Imports' statements must precede any declarations.

欢迎任何人阐明这个问题.

Anybody is welcome to shed a light on this issue.

推荐答案

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

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.

另一种方法是使用 Imports 语句来声明别名.类似的东西:

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


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


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

这篇关于VB.NET 中 typedef 的等效项或解决方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆