469,627 Members | 1,054 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

Declaring control array

If you're wondering why I post so many questions, it's because I want to
make an entry in the Guinness Book of World Records. But because I post so
many, I try to make them simple. Here is (I hope) a simple one:

I've declared a "control array" just after the WFD generated code like this:

Dim LabelArray() as Label = {Label0, Label1, Label2, Label3, Label4, Label5}

But when I try to do something like this:

\\
Private Sub FillLabels
dim a as integer
For a = 0 to 5
Msgbx(LabelArray(a).Text)
Next
End Sub
\\

I get an "Object reference not set to an instance of an object" error. If I
put the Dim statement inside the subroutine, it works fine. Am I doing
something wrong in declaring the array to have class scope?

Once again, thanks for the help.

Nathan


Nov 20 '05 #1
18 1521
"Nathan" <nk*********************@softhome.net> schrieb
I've declared a "control array" just after the WFD generated code
like this:

Dim LabelArray() as Label = {Label0, Label1, Label2, Label3, Label4,
Label5}

But when I try to do something like this:

\\
Private Sub FillLabels
dim a as integer
For a = 0 to 5
Msgbx(LabelArray(a).Text)
Next
End Sub
\\

I get an "Object reference not set to an instance of an object"
error. If I put the Dim statement inside the subroutine, it works
fine. Am I doing something wrong in declaring the array to have
class scope?


The array is filled in the constructor before InitializeComponent is called
and consequently before Label0...Label5 have been set, so you are filling
the array with 6 x Nothing.
--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #2
Nathan, I'd have it a guess that an instance of Label0, Label1, Label2,
Label3, Label4, and Label5 hasn't been created when you declare LabelArray
outside the subroutine FillLabels (and with class scope); LabelArray would
have length 5 but be full of 'Nothing'.

Within your for next loop, add a

if labelarray(a) is nothing then
msgbox "nothing"
end if

to see if this is the case. To elaborate further, if you have your
LabelArray declared at class level, derived from for example
System.Windows.Forms. Form, the "space" for the array will be allocated but
instances of the controls on the form Label0, .... won't have been. What you
are wanting is to populate LabelArray after Form_Load has executed - then
Label0, Label1... etc are instances of Label. As I see it when you are
running your application now, they probably aren't.

Regards
Hexathioorthooxalate

"Nathan" <nk*********************@softhome.net> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
If you're wondering why I post so many questions, it's because I want to
make an entry in the Guinness Book of World Records. But because I post so many, I try to make them simple. Here is (I hope) a simple one:

I've declared a "control array" just after the WFD generated code like this:
Dim LabelArray() as Label = {Label0, Label1, Label2, Label3, Label4, Label5}
But when I try to do something like this:

\\
Private Sub FillLabels
dim a as integer
For a = 0 to 5
Msgbx(LabelArray(a).Text)
Next
End Sub
\\

I get an "Object reference not set to an instance of an object" error. If I put the Dim statement inside the subroutine, it works fine. Am I doing
something wrong in declaring the array to have class scope?

Once again, thanks for the help.

Nathan

Nov 20 '05 #3
I mean length 6 :(
"Hexathioorthooxalate" <ru***@spameremove.clara.co.uk> wrote in message
news:10****************@echo.uk.clara.net...
Nathan, I'd have it a guess that an instance of Label0, Label1, Label2,
Label3, Label4, and Label5 hasn't been created when you declare LabelArray
outside the subroutine FillLabels (and with class scope); LabelArray would
have length 5 but be full of 'Nothing'.

Within your for next loop, add a

if labelarray(a) is nothing then
msgbox "nothing"
end if

to see if this is the case. To elaborate further, if you have your
LabelArray declared at class level, derived from for example
System.Windows.Forms. Form, the "space" for the array will be allocated but instances of the controls on the form Label0, .... won't have been. What you are wanting is to populate LabelArray after Form_Load has executed - then
Label0, Label1... etc are instances of Label. As I see it when you are
running your application now, they probably aren't.

Regards
Hexathioorthooxalate

"Nathan" <nk*********************@softhome.net> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
If you're wondering why I post so many questions, it's because I want to
make an entry in the Guinness Book of World Records. But because I post so
many, I try to make them simple. Here is (I hope) a simple one:

I've declared a "control array" just after the WFD generated code like

this:

Dim LabelArray() as Label = {Label0, Label1, Label2, Label3, Label4,

Label5}

But when I try to do something like this:

\\
Private Sub FillLabels
dim a as integer
For a = 0 to 5
Msgbx(LabelArray(a).Text)
Next
End Sub
\\

I get an "Object reference not set to an instance of an object" error.

If I
put the Dim statement inside the subroutine, it works fine. Am I doing
something wrong in declaring the array to have class scope?

Once again, thanks for the help.

Nathan


Nov 20 '05 #4
Cor
Hi Nathan,

To add to the others how to resolve it
Dim LabelArray() as Label = {Label0, Label1, Label2, Label3, Label4, Label5}
\\
Private Sub FillLabels
dim a as integer
For a = 0 to 5
LabelArray(a) = new Label
Msgbx(LabelArray(a).Text)
Next
End Sub
\\

I hope this helps,

Cor
Nov 20 '05 #5
"Cor" <no*@non.com> schrieb
Dim LabelArray() as Label = {Label0, Label1, Label2, Label3,
Label4,

Label5}

\\
Private Sub FillLabels
dim a as integer
For a = 0 to 5


LabelArray(a) = new Label
Msgbx(LabelArray(a).Text)
Next
End Sub
\\

I hope this helps,


I don't think so. I think he wants the 6 labels in the array, not 6
invisible labels. ;-)
--
Armin

Nov 20 '05 #6
Cor
Hi Armin
I don't think so. I think he wants the 6 labels in the array, not 6
invisible labels. ;-)

Dim LabelArray() as Label = {Label0, Label1, Label2, Label3,
Label4,

Label5}

\\ Private Sub FillLabels
dim a as integer
For a = 0 to 5


LabelArray(a) = new Label
labelarray(i).Width = 40
labelarray(i).Height = 20
labelarray(i).Location = New System.Drawing.Point(10, Cint(i * 21))
labelarray(i).Text = i.ToString
Me.Controls.Add(labelarray(i))

Msgbx(LabelArray(a).Text)
Next
End Sub
\\

Better?

Cor
Nov 20 '05 #7
"Cor" <no*@non.com> schrieb
LabelArray(a) = new Label

labelarray(i).Width = 40
labelarray(i).Height = 20
labelarray(i).Location = New System.Drawing.Point(10, Cint(i *
21)) labelarray(i).Text = i.ToString
Me.Controls.Add(labelarray(i))


Better?


Yes, but what I meant was that I'd created the array later because I think
the labels have been added by the designer.

Private Sub Form1_Load( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load

Me.LabelArray = New Label() _
{Label0, Label1, Label2, Label3, Label4, Label5}
End Sub

--
Armin

Nov 20 '05 #8
* "Nathan" <nk*********************@softhome.net> scripsit:
If you're wondering why I post so many questions, it's because I want to
make an entry in the Guinness Book of World Records. But because I post so
many, I try to make them simple. Here is (I hope) a simple one:

I've declared a "control array" just after the WFD generated code like this:

Dim LabelArray() as Label = {Label0, Label1, Label2, Label3, Label4, Label5}

But when I try to do something like this:

\\
Private Sub FillLabels
dim a as integer
For a = 0 to 5
Msgbx(LabelArray(a).Text)
'MsgBox'...
Next
End Sub
\\

I get an "Object reference not set to an instance of an object" error. If I
put the Dim statement inside the subroutine, it works fine. Am I doing
something wrong in declaring the array to have class scope?


The code should work. Where do you call the procedure? In the form's
constructor prior to calling 'InitializeComponent'? At this time, the
controls are not yet instantiated, so you will get the exception.

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
Nov 20 '05 #9
Thanks for the help so far. To be more specific, I already have the labels
created in the form designer, and I want to add them to a new array. The
array declaration appears after the form designer generated code, and just
before any other written code. I had guessed that for some reason the
labels didn't "exist" yet when the dim statement appeared. I still don't
understand why, though.

I put a msgbox in the for...loop as Hex suggested, and the msgbox appeared
as expected. The sub where the for...loop appears is called by the frm_Load
event. To give you a visual:

\\
+ Windows Form Designer generated code

Dim LabelArray() as Label = {Label0, Label1, Label2, Label3, Label4, Label5}

Private Sub frm1_Load(etc....) Handles MyBase.Load
FillLabels()
End Sub

Private Sub FillLabels()
'Code...
End Sub
\\

I'm guessing that as a workaround I could delete the labels from the form
designer
and insert them via code (as per Cor's post), but surely there's another
way to make this work.

Nov 20 '05 #10
"Nathan" <nk*********************@softhome.net> wrote...
I'm guessing that as a workaround I could delete the labels from the form
designer and insert them via code (as per Cor's post), but surely there's another way to make this work.


You don't have to go to all that trouble.

Your dim statement shouldn't include the assignment of the labels. Objects
aren't created line-by-line while stepping through the code. If you dim the
array in your form and add the labels to it in form_load I would be shocked
if it doesn't work.

Tom
Nov 20 '05 #11
Cor
Hi Nathan,

I did not know what you wanted,
If you want this, the labelarea has to be global
just beneath the Window form designer code
private labelarea(6) as labels

Somewhere else but I think the best place is the formload event you
labelarea(0) = label0
labelarea(1)=label1
etc,

Then your loop works everywhere.

I hope this helps?

Cor
Nov 20 '05 #12
Thanks Cor and Tom. I figured something like that would work; however, is
there a way to add multiple controls to array in one line, without doing it
line by line? To be more truthful with you, I have tons of controls that
need to be added to eleven different arrays. If I must do it line by line,
I can just stick those lines of code in their own sub in a minimized region
where they are out of the way. I was just wondering if there's a way to do
it all in one line, as it is when initializing the array.

"Cor" <no*@non.com> wrote in message
news:%2***************@TK2MSFTNGP11.phx.gbl...
Hi Nathan,

I did not know what you wanted,
If you want this, the labelarea has to be global
just beneath the Window form designer code
private labelarea(6) as labels

Somewhere else but I think the best place is the formload event you
labelarea(0) = label0
labelarea(1)=label1
etc,

Then your loop works everywhere.

I hope this helps?

Cor


Nov 20 '05 #13
On 2004-01-04, Nathan <nk*********************@softhome.net> wrote:
Thanks Cor and Tom. I figured something like that would work;
however, is there a way to add multiple controls to array in one line,
without doing it line by line? To be more truthful with you, I have
tons of controls that need to be added to eleven different arrays. If
I must do it line by line, I can just stick those lines of code in
their own sub in a minimized region where they are out of the way. I
was just wondering if there's a way to do it all in one line, as it is
when initializing the array.


Private LabelArray() As Label

Private Sub Form_Load(...)
LabelArray = new Label() { Label1, Label2, Label3, Label4}
End Sub

--
David
dfoster at
hotpop dot com
Nov 20 '05 #14
Once again, thank you!

"David" <df*****@127.0.0.1> wrote in message
news:sl********************@woofix.local.dom...
On 2004-01-04, Nathan <nk*********************@softhome.net> wrote:
Thanks Cor and Tom. I figured something like that would work;
however, is there a way to add multiple controls to array in one line,
without doing it line by line? To be more truthful with you, I have
tons of controls that need to be added to eleven different arrays. If
I must do it line by line, I can just stick those lines of code in
their own sub in a minimized region where they are out of the way. I
was just wondering if there's a way to do it all in one line, as it is
when initializing the array.


Private LabelArray() As Label

Private Sub Form_Load(...)
LabelArray = new Label() { Label1, Label2, Label3, Label4}
End Sub

--
David
dfoster at
hotpop dot com

Nov 20 '05 #15
"David" <df*****@127.0.0.1> schrieb
Private LabelArray() As Label

Private Sub Form_Load(...)
LabelArray = new Label() { Label1, Label2, Label3, Label4}
End Sub

The same I wrote the day before. ;-)
--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #16
Cor
Hi Armin,

The same I wrote the day before. ;-)


Stimt aber not complete, I was knocking on my head when I saw Davids answer,
that he made as addition on mine and remembered direct your answer.

The difference is that you did not tell that the array had to be declared
global.

(I never thought that it should go because of all the documentation and
samples in this newsgroup accoording control arrays)

However when I saw Tom's message I thought also why not.

And it is of course a nice solution I think.

Cor
Nov 20 '05 #17
"Cor" <no*@non.com> schrieb
The same I wrote the day before. ;-)


Stimt aber not complete, I was knocking on my head when I saw Davids
answer, that he made as addition on mine and remembered direct your
answer.

The difference is that you did not tell that the array had to be
declared global.


"global"? At class level?
Nathan wrote: "I've declared a "control array" just after the WFD generated
code like this:", so I assumed it is declared as at class level (as a
field).

--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #18
Cor
Hi Armin,
WFD


You are right, maybe it is because I never declare something there with Dim

Again a long thread for nothing

(Not about)

(But I still find it a nice solution so I learned from it)

:-))

Cor
Nov 20 '05 #19

This discussion thread is closed

Replies have been disabled for this discussion.

By using this site, you agree to our Privacy Policy and Terms of Use.