2

我正在查看http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web-api上的代码示例

作为一个练习,我正在尝试将它从 C# 翻译成 vb.net,但是这件作品没有运气,

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

我已经看到使用列表而不是数组的建议,所以我将尝试这样做,但想知道我在这里缺少什么。

4

1 回答 1

9

看看对象初始化器:

Dim namedCust = New Customer With {.Name = "Terry Adams".....

注意With以及“。” 对于您要设置的每个属性。

 Dim products() As Product = { _
         new Product With {.Id = 1, .Name = "Tomato Soup", .Category = "Groceries", 
                           .Price = 1 }, _.....

MSDN 链接

进一步阅读。

于 2013-04-30T17:21:37.967 回答