Declaring control array

N

Nathan

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
 
A

Armin Zingler

Nathan said:
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.
 
H

Hexathioorthooxalate

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
 
H

Hexathioorthooxalate

I mean length 6 :(


Hexathioorthooxalate said:
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





If
 
C

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
 
A

Armin Zingler

Cor said:
LabelArray(a) = new Label

I hope this helps,

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

Cor

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

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?

Cor
 
A

Armin Zingler

Cor said:
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
 
H

Herfried K. Wagner [MVP]

* "Nathan said:
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.
 
N

Nathan

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.
 
T

Tom Leylan

Nathan said:
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
 
C

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
 
N

Nathan

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.
 
D

David

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
 
N

Nathan

Once again, thank you!

David said:
Private LabelArray() As Label

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

Armin Zingler

David said:
Private LabelArray() As Label

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


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

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
 
A

Armin Zingler

Cor said:
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).
 
C

Cor

Hi Armin,

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
 

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

Top