For Each 引用对象本身或其值吗?
Does For Each reference objects itself or their values?
这是我的问题:我正在我的 WPF 应用程序中使用一些 ComboBox
,我想用 ComboBoxItemPlus
填充它们,基本上 ComboBoxItem
有一些更多的属性,以便 link 它们更容易地包含数据。我的问题是我想自动填充这些 ComboBox
。为此,我声明了一个 ComboBoxItemPlus
的列表,一个一个地实例化它们,然后给它们赋予一个值,如下所示:(在这种情况下,我可以一个一个地做,因为只有七个,但我会之后必须用更多的元素来做)
Private Sub FillCBB_Jour()
Dim item(7) As ComboBoxItemPlus
For Each it As ComboBoxItemPlus In item
it = New ComboBoxItemPlus
Next
With item(0)
.Content = New String("Lundi")
.Value = DayOfWeek.Monday
End With
With item(1)
.Content = New String("Mardi")
.Value = DayOfWeek.Tuesday
End With
With item(2)
.Content = New String("Mercredi")
.Value = DayOfWeek.Wednesday
End With
With item(3)
.Content = New String("Jeudi")
.Value = DayOfWeek.Thursday
End With
With item(4)
.Content = New String("Vendredi")
.Value = DayOfWeek.Friday
End With
With item(5)
.Content = New String("Samedi")
.Value = DayOfWeek.Saturday
End With
With item(6)
.Content = New String("Dimanche")
.Value = DayOfWeek.Sunday
End With
For Each it As ComboBoxItemPlus In item
CBB_Jour.Items.Add(it)
Next
它 returns 我出现以下错误 :
System.ArgumentOutOfRangeException
HResult=0x80131502
Message=L'index était hors limites. Il ne doit pas être négatif et doit être inférieur à la taille de la collection.
Nom du paramètre : index
Source=mscorlib
Arborescence des appels de procédure :
à System.ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument argument, ExceptionResource resource)
à System.Collections.Generic.List`1.get_Item(Int32 index)
à AutoFHT.DocumentFHTEditor.FillCBB_Jour() dans C:\Users\BOUCKB\Source\Repos\AutoFHT\AutoFHT\DocumentFHTEditor.xaml.vb :ligne 35
à AutoFHT.DocumentFHTEditor.DocumentFHTEditon_Initialized(Object sender, EventArgs e) dans C:\Users\BOUCKB\Source\Repos\AutoFHT\AutoFHT\DocumentFHTEditor.xaml.vb :ligne 21
à System.Windows.FrameworkElement.RaiseInitialized(EventPrivateKey key, EventArgs e)
à System.Windows.FrameworkElement.OnInitialized(EventArgs e)
à System.Windows.FrameworkElement.TryFireInitialized()
à System.Windows.FrameworkElement.EndInit()
à MS.Internal.Xaml.Runtime.ClrObjectRuntime.InitializationGuard(XamlType xamlType, Object obj, Boolean begin)
告诉我 item(0) 没有实例化。
我尝试将它们从 For Each
语句中实例化出来,并且效果很好。但是,同样,我将不得不对我不知道长度的列表执行此操作,这就是我想要这样做的原因。
谁能告诉我为什么这不起作用?
好的,我找到了问题的根源。
For Each
语句引用实例化对象。为了让它工作,我使用了经典的For
。
这是我的问题:我正在我的 WPF 应用程序中使用一些 ComboBox
,我想用 ComboBoxItemPlus
填充它们,基本上 ComboBoxItem
有一些更多的属性,以便 link 它们更容易地包含数据。我的问题是我想自动填充这些 ComboBox
。为此,我声明了一个 ComboBoxItemPlus
的列表,一个一个地实例化它们,然后给它们赋予一个值,如下所示:(在这种情况下,我可以一个一个地做,因为只有七个,但我会之后必须用更多的元素来做)
Private Sub FillCBB_Jour()
Dim item(7) As ComboBoxItemPlus
For Each it As ComboBoxItemPlus In item
it = New ComboBoxItemPlus
Next
With item(0)
.Content = New String("Lundi")
.Value = DayOfWeek.Monday
End With
With item(1)
.Content = New String("Mardi")
.Value = DayOfWeek.Tuesday
End With
With item(2)
.Content = New String("Mercredi")
.Value = DayOfWeek.Wednesday
End With
With item(3)
.Content = New String("Jeudi")
.Value = DayOfWeek.Thursday
End With
With item(4)
.Content = New String("Vendredi")
.Value = DayOfWeek.Friday
End With
With item(5)
.Content = New String("Samedi")
.Value = DayOfWeek.Saturday
End With
With item(6)
.Content = New String("Dimanche")
.Value = DayOfWeek.Sunday
End With
For Each it As ComboBoxItemPlus In item
CBB_Jour.Items.Add(it)
Next
它 returns 我出现以下错误 :
System.ArgumentOutOfRangeException
HResult=0x80131502
Message=L'index était hors limites. Il ne doit pas être négatif et doit être inférieur à la taille de la collection.
Nom du paramètre : index
Source=mscorlib
Arborescence des appels de procédure :
à System.ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument argument, ExceptionResource resource)
à System.Collections.Generic.List`1.get_Item(Int32 index)
à AutoFHT.DocumentFHTEditor.FillCBB_Jour() dans C:\Users\BOUCKB\Source\Repos\AutoFHT\AutoFHT\DocumentFHTEditor.xaml.vb :ligne 35
à AutoFHT.DocumentFHTEditor.DocumentFHTEditon_Initialized(Object sender, EventArgs e) dans C:\Users\BOUCKB\Source\Repos\AutoFHT\AutoFHT\DocumentFHTEditor.xaml.vb :ligne 21
à System.Windows.FrameworkElement.RaiseInitialized(EventPrivateKey key, EventArgs e)
à System.Windows.FrameworkElement.OnInitialized(EventArgs e)
à System.Windows.FrameworkElement.TryFireInitialized()
à System.Windows.FrameworkElement.EndInit()
à MS.Internal.Xaml.Runtime.ClrObjectRuntime.InitializationGuard(XamlType xamlType, Object obj, Boolean begin)
告诉我 item(0) 没有实例化。
我尝试将它们从 For Each
语句中实例化出来,并且效果很好。但是,同样,我将不得不对我不知道长度的列表执行此操作,这就是我想要这样做的原因。
谁能告诉我为什么这不起作用?
好的,我找到了问题的根源。
For Each
语句引用实例化对象。为了让它工作,我使用了经典的For
。