468,765 Members | 1,017 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,765 developers. It's quick & easy.

Problem initializing global label array outside of procedure

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
Nov 21 '05 #1
5 2196
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
Nov 21 '05 #2
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

"Dragon" wrote:
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

Nov 21 '05 #3
"Rich" <Ri**@discussions.microsoft.com> schrieb
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

Nov 21 '05 #4
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

"Armin Zingler" wrote:
"Rich" <Ri**@discussions.microsoft.com> schrieb
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

Nov 21 '05 #5
> ~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=]
Nov 21 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Gurpreet Sachdeva | last post: by
4 posts views Thread by James E Koehler | last post: by
10 posts views Thread by dof | last post: by
3 posts views Thread by Suhail Salman | last post: by
4 posts views Thread by Bass Pro | last post: by
2 posts views Thread by sorobor | last post: by
6 posts views Thread by Jai Prabhu | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.