Problem initializing global label array outside of procedure

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

The following code snipet won't run if I initialize my global label array
outside of a procedure - say Form_Load. If I initialize the array inside
Form_Load then it works. Is there a way to initialize the label array
outside of a procedure as I am doing? Or... (see below this snipet)
---------------------------------------------------------------------------------
Public Class Form1
Inherits System.Windows.Forms.Form

" Windows Form Designer generated code "

Dim arrlbl As Label() = {lbl0, lbl1, lbl2, lbl3}

Private Sub Form1_Load(...)
Dim i As Integer
For i = 0 To arrlbl.Length - 1 : arrlbl(i).Text = "test" : Next
....
---this not working
----------------------------------------------------------------

Or...

Dim arrlbl(3) As Label

Private Sub Form1_Load(...)
Dim i As Integer
arrlbl(0) = lbl0
arrlbl(1) = lbl1
arrlbl(2) = lbl2
arrlbl(3) = lbl3
For i = 0 To arrlbl.Length - 1 : arrlbl(i).Text = "test" : Next
-------------------------------------------------------------------------

this works but adds an additional 4 rows of code to my project. Is this the
only way to initialize my global array?

Thanks,
Rich
 
Hello Rich,

In your first example you initialize arrlbl at class level. Actually it
happens during the MyBase.New() statement.
lbl* variables are initialized in InitializeComponent() method which
executes later.

Thus, at the time ~arrlbl As Label() = {lbl0, lbl1, lbl2, lbl3}~ executes,
lbl's are still uninitialized, so you get an array full of Nothing's.
this works but adds an additional 4 rows of code to my project. Is this the
only way to initialize my global array?

No! You can shorten arrlbl(*) = lbl* statements by ~arrlbl = New Label()
{lbl0, lbl1, lbl2, lbl3}~; this way, you don't need to specify bounds in Dim
statement.
You also might want to put this in New() after InitializeComponent() to
simulate initialization at class level.

HTH

Roman
 
Rich said:
The following code snipet won't run if I initialize my global label
array outside of a procedure - say Form_Load. If I initialize the
array inside Form_Load then it works. Is there a way to initialize
the label array outside of a procedure as I am doing? Or... (see
below this snipet)
---------------------------------------------------------------------------------
Public Class Form1
Inherits System.Windows.Forms.Form

" Windows Form Designer generated code "

Dim arrlbl As Label() = {lbl0, lbl1, lbl2, lbl3}

Private Sub Form1_Load(...)
Dim i As Integer
For i = 0 To arrlbl.Length - 1 : arrlbl(i).Text = "test" : Next
...
---this not working
----------------------------------------------------------------


Dim arrlbl As Label()

Private Sub Form1_Load(...)
arrlbl = new label() {lbl0, lbl1, lbl2, lbl3}
'...


Armin
 
Thanks very much for your reply - question:
You can shorten arrlbl(*) = lbl* statements by ~arrlbl = New Label()
{lbl0, lbl1, lbl2, lbl3}~; this way, you don't need to specify bounds in Dim
statement.
You also might want to put this in New() after InitializeComponent() to
simulate initialization at class level.
<<

-----------------------------------------------------------------------
Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

~arrlbl = New Label(){lbl0, lbl1, lbl2, lbl3}~

End Sub
-------------------------------------------------------------------

Do I Dim arrlbl? Dim inside of New( )? Appologize for my lack of brain
cells on this. May I request if you could show me a snippet of code what you
are suggesting on how to declare and initialize my global label array?

I'm not clear on your suggestion here.

Thanks,
Rich
 
OK. Thanks very much. Now I get it. I know I have done this before. I
just couldn't find my old code.

Thanks again,
Rich
 
~arrlbl = New Label(){lbl0, lbl1, lbl2, lbl3}~

Eek! "~" was kinda quotation mark. You hadn't to type them.
Do I Dim arrlbl? Dim inside of New( )? Appologize for my lack of brain
cells on this. May I request if you could show me a snippet of code what you
are suggesting on how to declare and initialize my global label array?

No, I meant you should declare arrlbl at class level:

~
Private arrlbl() As Label
~

but initialize in New():

~
arrlbl = New Label() {lbl0, lbl1, lbl2, lbl3}
~
I'm not clear on your suggestion here.

I hope it's clear now. 8=]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top