I have an array of RichTextBoxes and I would like to initialise them using Array.ForEach.
I have made the following attempt with no success:
Dim aRTB(5) as RichTextBox
Array.ForEach(aRTB, Function() New RichTextBox)
This code was the only one to make it past the compiler however it doesn’t initialise any part of the array.
Code which failed to past the compiler includes:
Array.ForEach(aRTB, Function() Return New RichTextBox)
Array.ForEach(aRTB, Function(rtb as RichTextBox) rtb = New RichTextBox)
I realise I could easily set up a a regular loop (For, Do, While) to perform the same action however I would like to see if this way can work.
Thank you for any help.
You cannot. Array.ForEach performs an action on every element of the array, i.e., it passes the elements of the array to your function a parameter:
The parameter is
ByVal, i.e., you cannot use this to change the contents of the array:Since your array is initially empty, you can’t do anything useful with Array.ForEach on it.
Sorry, but you will have to use a classic
Forloop for this:The
0 To 5is not a typo… in VB.NET array declarations specify the upper bound, not the size.