2010-09-15 59 views
3

問:如何縮短這個數組的創建?
我需要創建一個具有一個屬性的ReportingService2005_W​​ebService.Property類型的數組。縮短VB.NET數組聲明?

喜歡的東西:

Dim PropertyArray() as new ReportingService2005_WebService.Property(1) 

我必須這樣做:

 Dim PropertyArray As ReportingService2005_WebService.Property() = New ReportingService2005_WebService.Property(0) {} 
     PropertyArray(0) = New ReportingService2005_WebService.Property 
     PropertyArray(0).Name = "Description" 
     PropertyArray(0).Value = "Automatically added DataSource" 

回答

4

http://blogs.msdn.com/b/wriju/archive/2008/02/05/vb-net-9-0-object-and-array-initializers.aspx

Dim PropertyArray() As ReportingService2005_WebService.Property = { _ 
    new ReportingService2005_WebService.Property() With {.Name = "Description", .Value="Automatically added DataSource" } _ 
} 

確保你的 「陣列支架」 是在正確的位置在初始Dim語句。應該是: Dim PropertyArray()...

+0

需要什麼版本的VB.NET?例如。它會與VB.NET的版本對應於Visual Studio 2008嗎? – 2010-09-15 12:26:43

+0

我使用2008年,這種語法工作正常。 – 2010-09-15 13:35:55

+0

根據我的帖子中的鏈接,它是VB.NET v9,這是VS2008。 – Zippit 2010-09-15 13:41:28