Does For Each reference objects itself or their values?

0

Here is my problem : i'm working with some ComboBox in my WPF app, and I want to fill them with ComboBoxItemPlus, which are basicly ComboBoxItem with a few more properties in order to link them with data more easily. My problem is that I want to fill these ComboBox automaticly. For this, I declare a list of ComboBoxItemPlus, instanciate them one by one, and then attribute them a value like this : (in this case, I could do it one by one because there are only seven, but I will have to do this with much more elements after)

 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

And it returns me the following error :

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)

telling me that item(0) is not instanciated.

I've try to instanciate them out of the For Each statement, and it works fine. But, again, I'll have to do this with lists I don't know the length, and that's why I want to do it like this.

Can anyone tell me why this doesn't work ?

vb.net
foreach
combobox

1 Answer

0

Okay I found the source of my problem.

the For Each statement refers to instanciated objects. In order to make it work, i Used a classic For.

answered on Stack Overflow Dec 13, 2019 by Benoit Bouckaert

User contributions licensed under CC BY-SA 3.0