Dim lbl( ) As Label = New Label( ){...} vs Dim lbl( ) As Label = {...}

  • Thread starter Thread starter Ron
  • Start date Start date
R

Ron

Hello,

I created an array of label controls like this:

Dim lbl() As Label = {Me.lbl1, Me.lbl2, Me.lbl3}

and was able to iterate through the array. But then I saw
another way to declare the same array:

Dim lbl() As Label = New Label(){Me.lbl1, Me.lbl2, Me.lbl3}

Both ways seems to work fine, but I want to get in the
practice of doing this correctly. Is there a difference
between using New or not for this? If so, what is the
difference?

Thanks,
Ron
 
Ron said:
I created an array of label controls like this:

Dim lbl() As Label = {Me.lbl1, Me.lbl2, Me.lbl3}

and was able to iterate through the array. But then I saw
another way to declare the same array:

Dim lbl() As Label = New Label(){Me.lbl1, Me.lbl2, Me.lbl3}

Both ways seems to work fine, but I want to get in the
practice of doing this correctly. Is there a difference
between using New or not for this? If so, what is the
difference?

There is no difference. I would prefer the first solution because of the
higher readability.
 
Ron,
As Herfried suggests I normally use the first syntax, however you need the
second syntax when you want an array, but you don't want or need the
variable.

For example:

Sub Test(labels() As Label)
End Sub

Test(New Label(){Me.lbl1, Me.lbl2, Me.lbl3})

Here I want to send an array of Labels to the Test method, however I don't
want or need an array variable to hold them.

Hope this helps
Jay
 
Hi.
What if I have, let´s say 300 labels? Can I use a string for that?

dim allLabels as string="{lbl1, lbl2, lbl3.......lbl300}"
Dim lbl() As Label = allLabels

Thanks
 
Back
Top