2015-06-08 43 views
0
Private Sub UpdateListBox(message As String) 
    Dim beeGroups As IOrderedEnumerable(Of IGrouping(Of BeeState, Bee)) = From beeGroup In From bee In Me.world.BeesGroup bee By bee.CurrentStateOrder By beeGroup.KeybeeGroup 
    Me.listBox1.Items.Clear() 

    For Each beeGroup As IGrouping(Of BeeState, Bee) In beeGroups 
     Dim s As String 
     s = If(beeGroup.Count() = 1, String.Empty, "s") 

     Me.listBox1.Items.Add(Convert.ToString(beeGroup.Key) & ": " & beeGroup.Count() & " bee" & s) 

     If beeGroup.Key <> BeeState.Idle OrElse beeGroup.Count() <> Me.world.Bees.Count() OrElse Me.framesRun <= 0 Then 
      Continue For 
     End If 

     Me.listBox1.Items.Add("Simulation ended: all bees are idle") 
     Me.toolStripButtonStartSimulation.Text = "Simulation ended." 
     Me.statusStrip1.Items(0).Text = "Simulation ended" 
     Me.timer1.Enabled = False 
    Next 
End Sub  

我絕對堅持這個錯誤在c#項目對vb.net轉換。IOrderedEnumerable到vb.net IOrderedEnumerable轉換

我知道c#它是這樣的

IOrderedEnumerable<IGrouping<BeeState, Bee>> beeGroups = from bee in this.world.Bees 
                   group bee by bee.CurrentState 
                   into beeGroup orderby beeGroup.Key select beeGroup;  

但一旦轉換成vb.net它試圖將狀態與最少數量的聲明在同一行的一切。

雖然也c#你可以逃脫 c#

foreach (IGrouping<BeeState, Bee> beeGroup in beeGroups)  

vb.net試圖申報的語句中本身:

For Each group As var In beeGroups  
+0

'我絕對堅持這個錯誤在C#項目轉化爲vb.net'什麼錯誤到底是什麼? – mclark1129

回答

0
Private Sub UpdateListBox(message As String) 
    Dim beeGroups = From bee In Me.world.Bees 
        Group By bee.CurrentState Into beeGroup = Group 
        Order By beeGroup.Key 

    Me.listBox1.Items.Clear() 

    For Each beeGroup In beeGroups 
     Dim beeCount = beeGroup.Count ' to avoid reevaluation 
     Dim s = If(beeCount >= 1, String.Empty, "s") 

     Me.listBox1.Items.Add(Convert.ToString(beeGroup.Key) & ": " & beeCount & " bee" & s) 

     If beeGroup.Key <> BeeState.Idle OrElse beeCount <> Me.world.Bees.Count OrElse Me.framesRun <= 0 Then 
      Continue For 
     End If 

     Me.listBox1.Items.Add("Simulation ended: all bees are idle") 
     Me.toolStripButtonStartSimulation.Text = "Simulation ended." 
     Me.statusStrip1.Items(0).Text = "Simulation ended" 
     Me.timer1.Enabled = False 
    Next 
End Sub 
0

你 '對於每個'語句是正確的(C#和VB版本都在循環頭文件中聲明迭代器變量),但是VB LINQ查詢應該是:

Dim beeGroups As IOrderedEnumerable(Of IGrouping(Of BeeState, Bee)) = From bee In Me.world.Bees 
                     Group bee By bee.CurrentState Into beeGroup = Group 
                     Order By CurrentState 
                     Select beeGroup 

或用戶選項推斷在:

Dim beeGroups = From bee In Me.world.Bees 
       Group bee By bee.CurrentState Into beeGroup = Group 
       Order By CurrentState 
       Select beeGroup 
+0

在VB.Net中'Select'不需要,默認情況下,查詢返回查詢範圍中的當前內容(在這種情況下,只有'beeGroup')。你也忘了在'Order By'子句中用'bee.'來限定'CurrentState'; – Sehnsucht

+0

'從Dim beeGroups中選擇蜜蜂羣'後。它的行爲就像'select beeGroup',當我事後聲明'For Each beeGroup In beeGroup'時甚至不存在。我可以看到beeGroup在beeGroup中聲明並且錯誤是混淆的。我是否會錯誤地使用IEnumerable並且聲明?謝謝你的幫助:) – user4986601

+0

好吧,錯誤是它沒有被聲明,我的困惑是大到適合一個變量:D。但是他們是在蜂羣之前宣佈蜂羣的一種方式,或者如果你能想到蜂箱,那麼是一種更好的方法。 – user4986601

相關問題