C# 如何在vb.net中使用对象初始值设定项设置属性来创建数组
我正在查看上的代码示例 作为练习,我试图将其从C#翻译成vb.net,但这篇文章没有成功C# 如何在vb.net中使用对象初始值设定项设置属性来创建数组,c#,vb.net,C#,Vb.net,我正在查看上的代码示例 作为练习,我试图将其从C#翻译成vb.net,但这篇文章没有成功 public class Product { public int Id { get; set; } public string Name { get; set; } public string Category { get; set; } public decimal Price { g
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public string Category { get; set; }
public decimal Price { get; set; }
}
Product[] products = new Product[]
{ new Product { Id = 1, Name = "Tomato Soup", Category = "Groceries", Price = 1 },
new Product { Id = 2, Name = "Yo-yo", Category = "Toys", Price = 3.75M },
new Product { Id = 3, Name = "Hammer", Category = "Hardware", Price = 16.99M }
};
我试过了
Public class Product
Public Property Id As Integer
Public Property Name As String
Public Property Category As String
Public Property price As Decimal
End Class
Dim products() As Product = { _
new Product (Id = 1, Name = "Tomato Soup", Category = "Groceries", Price = 1 ), _
new Product ( Id = 2, Name = "Yo-yo", Category = "Toys", Price = 3.75M ), _
new Product (Id = 3, Name = "Hammer", Category = "Hardware", Price = 16.99M ) }
我看到过使用列表而不是数组的建议,因此我将尝试使用列表,但想知道我在这里缺少了什么。看看对象初始值设定项:
Dim namedCust = New Customer With {.Name = "Terry Adams".....
请注意要设置的每个属性的和
以及“.”
Dim products() As Product = { _
new Product With {.Id = 1, .Name = "Tomato Soup", .Category = "Groceries",
.Price = 1 }, _.....
可能会有帮助,尤其是Jon Skeet的答案与您这里的答案有点不同。我可以解决阵列公式或创建新的
产品的问题吗?不用担心。如果那是你想要的,那就做个记号。