vb.net 对象初始值设定项列表(Of T)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3254169/
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
vb.net Object Initialiser List(Of T)
提问by Tim B James
I have been looking at some C# code:
我一直在看一些 C# 代码:
List<Employee> Employees = new List<Employee>{
new Employee{firstname="Aamir",lastname="Hasan",age=20},
new Employee{firstname="awais",lastname="Hasan",age=50},
new Employee{firstname="Bill",lastname="Hasan",age=70},
new Employee{firstname="sobia",lastname="khan",age=80},
};
Now when I convert this to vb.net
现在当我将其转换为 vb.net
Dim Employees as List(Of Employee) = New List(Of Employee)() With { New Employee() With { _
.firstname = "Aamir", _
.lastname = "Hasan", _
.age = 20 _
}, _
New Employee() With { _
.firstname = "awais", _
.lastname = "Hasan", _
.age = 50 _
}, _
New Employee() With { _
.firstname = "Bill", _
.lastname = "Hasan", _
.age = 70 _
}, _
New Employee() With { _
.firstname = "sobia", _
.lastname = "khan", _
.age = 80 _
} _
}
I get the error "Name of field or property being initialized in an object initializer must start with'.'."
我收到错误“在对象初始值设定项中初始化的字段或属性的名称必须以'.'开头。”
Now I can get an array of employee using the code:
现在我可以使用代码获取一组员工:
Dim Employees = { New Employee() With { _
.FirstName = "Aamir", _
.LastName = "Hasan", _
.Age = 20}, _
New Employee() With { _
.FirstName = "Awais", _
.LastName = "Hasan", _
.Age = 50}, _
New Employee() With { _
.FirstName = "Bill", _
.LastName = "Hasan", _
.Age = 70 _
} _
}
But I would like a List(Of Employee)
as it is bugging me as to why this doesnt work in vb.net?
但我想要一个,List(Of Employee)
因为它在困扰我为什么这在 vb.net 中不起作用?
回答by MarkJ
Collection initialisers were added in VB.NET 2010. This is air code, but here goes:
在 VB.NET 2010中添加了集合初始值设定项。这是航空代码,但这里是:
Dim Employees as List(Of Employee) = New List(Of Employee)() From
{
New Employee() With { _
.firstname = "Aamir", _
.lastname = "Hasan", _
.age = 20 _
}, _
New Employee() With { _
.firstname = "awais", _
.lastname = "Hasan", _
.age = 50 _
}, _
New Employee() With { _
.firstname = "Bill", _
.lastname = "Hasan", _
.age = 70 _
}, _
New Employee() With { _
.firstname = "sobia", _
.lastname = "khan", _
.age = 80 _
} _
}
回答by dxh
EDIT(2)
As pointed out in comments, VB.NET collection initializershave now been introduced, and a lot of the following post should be considered obsolete.
编辑(2)
正如评论中所指出的,现在已经引入了VB.NET 集合初始值设定项,下面的很多帖子都应该被认为是过时的。
EDIT
Don't always blindly trust the C# to VB.NET converterHere's a handy tool for online conversion
编辑
不要总是盲目地相信 C# 到 VB.NET 转换器这里有一个方便的在线转换工具
Turns out VB.NET doesn't have collection initializers. Which means there is no equivalence of
原来VB.NET 没有集合初始值设定项。这意味着没有等价的
var myList = new List<string>()
{
"abc",
"def"
};
... but it doeshave object initializers. So you can create an instance of a class and assign values to its properties all in one go, but you cannot create an instance of a list and add items to it all in one go.
...但它确实有对象初始值设定项。因此,您可以一次性创建一个类的实例并为其属性赋值,但是您不能一次性创建一个列表的实例并向其中添加项目。
There closest you can get is in the link above. You can create an Arrayand add items to it in a single operation, and then you have to ToList
that array.
您可以在上面的链接中找到最接近的位置。您可以在单个操作中创建一个数组并向其中添加项目,然后您必须添加到ToList
该数组。
So this time I've actually compiled the code myself, and it works. Sorry for the hassle
所以这次我实际上是自己编译了代码,并且它有效。抱歉给您带来麻烦
Dim EmployeesTemp As Employee() = { _
New Employee() With { _
.firstname = "Aamir", _
.lastname = "Hasan", _
.age = 20 _
}, _
New Employee() With { _
.firstname = "awais", _
.lastname = "Hasan", _
.age = 50 _
}, _
New Employee() With { _
.firstname = "Bill", _
.lastname = "Hasan", _
.age = 70 _
}, _
New Employee() With { _
.firstname = "sobia", _
.lastname = "khan", _
.age = 80 _
} _
}
Dim Employees as List(Of Employee) = EmployeesTemp.ToList()
回答by Eric
How about this?
这个怎么样?
Dim Employees As List(Of Employee) = { _
New Employee() With { .firstname = "Aamir", .lastname = "Hasan", .age = 20 }, _
New Employee() With { .firstname = "awais", .lastname = "Hasan", .age = 50 }, _
New Employee() With { .firstname = "Bill", .lastname = "Hasan", .age = 70 }, _
New Employee() With { .firstname = "sobia", .lastname = "khan", .age = 80 } _
}.ToList()