vb.net对象初始化列表(Of T)

时间:2010-07-15 09:22:09

标签: vb.net syntax

我一直在看一些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},  
    };

现在我将其转换为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 _  
} _  
}  

我收到错误“在对象初始化程序中初始化的字段或属性的名称必须以'。'开头。”

现在我可以使用代码获得一组员工:

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中不起作用而烦恼我?

3 个答案:

答案 0 :(得分:19)

收集初始化者为added in 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 _ 
    } _ 
}   

答案 1 :(得分:18)

编辑 (2)
正如评论中指出的那样,VB.NET collection initializers现在已经被引入,以下很多帖子应该被认为是过时的。

修改
Don't always blindly trust the C# to VB.NET converter
Here's a handy tool for online conversion

结果VB.NET doesn't have collection initializers。这意味着没有等价的

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

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

你可以得到的最接近的是上面的链接。您可以创建数组并在一次操作中向其中添加项目,然后您必须ToList该数组。

所以这次我实际上自己编译了代码,并且它有效。对不起,麻烦

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

答案 2 :(得分:1)

这个怎么样?

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