不能在 VB.NET 中声明列表?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13826593/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 11:32:00  来源:igfitidea点击:

Can't declare lists in VB.NET?

vb.net

提问by redhotspike

Dim lstNum As New List(Of Integer)(New Integer() { 3, 6, 7, 9 })

When I type the above line of code, Visual Studio informs me of an error

当我输入上面的代码行时,Visual Studio 通知我一个错误

'Microsoft.Office.Interop.Word.List' has no type parameters and so cannot have type arguments.

“Microsoft.Office.Interop.Word.List”没有类型参数,因此不能有类型参数。

What on earth does that mean and how do I fix it? I can't seem to create lists of any kind. I'm assuming I'm missing some sort of import but I'm not fluent with VB.Net enough to know what to try.

这到底是什么意思,我该如何解决?我似乎无法创建任何类型的列表。我假设我缺少某种导入,但我对 VB.Net 不够流利,不知道该尝试什么。

回答by David Brunow

Use Generic.List instead of just List.

使用 Generic.List 而不是 List。

Dim lstNum As New Generic.List(Of Integer)(New Integer() { 3, 6, 7, 9 })

Since you have the Word interop imported, it is trying to find Word.List. Specifying Generic.List will tell it to go outside of that import.

由于您已导入 Word 互操作,因此它正在尝试查找 Word.List。指定 Generic.List 将告诉它在导入之外。

回答by Emmanuel N

Try adding System.Collections.Generic

尝试添加 System.Collections.Generic

 Dim lstNum As New System.Collections.Generic.List(Of Integer)(New Integer() { 3, 6, 7, 9 })

回答by Hassan

Either You use Generic.list instead of List Dim lstNum As New Generic.List(Of Integer)(New Integer() { 3, 6, 7, 9 }) Or you just import System.Collections.Generic Namespace, Both approaches are fine, but I'd go for the later one if I've to use list again and again. .

要么你使用 Generic.list 而不是 List Dim lstNum As New Generic.List(Of Integer)(New Integer() { 3, 6, 7, 9 }) 或者你只是导入 System.Collections.Generic Namespace,两种方法都很好,但如果我必须一次又一次地使用 list ,我会选择后者。.