vb.net 对象初始值设定项列表(Of T) [英] vb.net Object Initialiser List(Of T)

查看:42
本文介绍了vb.net 对象初始值设定项列表(Of T)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在看一些 C# 代码:

I have been looking at some C# code:

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},  
    };

现在当我将其转换为 vb.net

Now when I convert this to 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 _
} _  
}    

但我想要一个 List(Of Employee) 因为它让我烦恼,为什么这在 vb.net 中不起作用?

But I would like a List(Of Employee) as it is bugging me as to why this doesnt work in vb.net?

推荐答案

EDIT (2)
正如评论中指出的,VB.NET 集合初始值设定项现已引入,并且以下很多帖子都应该被视为过时了.

EDIT (2)
As pointed out in comments, VB.NET collection initializers have now been introduced, and a lot of the following post should be considered obsolete.

编辑
不要总是盲目相信 C# 到 VB.NET 转换器
这是一个方便的在线转换工具

结果 VB.NET 没有集合初始化器.这意味着没有

Turns out VB.NET doesn't have collection initializers. Which means there is no equivalence of

var myList = new List<string>()
{
   "abc",
   "def"
};

...但它确实有对象初始值设定项.因此,您可以一次性创建一个类的实例并为其属性赋值,但您不能一次性创建一个列表的实例并向其中添加项目.

... but it does have 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.

您可以在上面的链接中找到最接近的位置.您可以创建一个 Array 并在单个操作中向其中添加项目,然后您必须ToList 该数组.

There closest you can get is in the link above. You can create an Array and add items to it in a single operation, and then you have to ToList that array.

所以这次我实际上自己编译了代码,并且可以正常工作.抱歉打扰了

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

这篇关于vb.net 对象初始值设定项列表(Of T)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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