Confused - control not set to instance inside foreach loop - please help

A

Alan Silver

Hello,

I have code like the following...

foreach (Control ctl in Page.Controls) {
if (ctl.ID.StartsWith("X_")) {
// do stuff
}
}

but this gives a run-time error on the second line of "Object reference
not set to an instance of an object" which confuses me.

Surely the foreach loop should ensure that ctl is always set to an
object?

The ID property is a string, so that shouldn't be causing the problem.
If the control doesn't have an ID (if that's possible), the ID should be
"" and the StartsWith() method should return false.

<pause>
I just found something even more weird!! I changed the code to...

foreach (Control ctl in Page.Controls) {
string ctlId = ctl.ID;
Trace.Warn("ctlId = @" + ctlId + "@");
if (ctlId != "") {
if (ctlId.StartsWith("X_")) { // RUN-TIME ERROR HERE
// do stuff
}
}
}

and it bombed out on the same line, even though the tracing shows that
the ID was "". I don't understand how it *got* to that line when the
previous lines checks if the ID is "". Either I've done something
blindingly stupid here (not unlikely!!), or something very weird is
happening.

So, anyone able to explain to me what's going on here? TIA.
 
K

Kevin Spencer

I believe you have at least one Control with a null id. Unfortunately, it is
not an error to concatenate a string with a null string. So:

Trace.Warn("ctlId = @" + ctlId + "@");

will not throw an exception. Neither will:

if (ctlId != "")

If ctlId is null, it will evaluate to true (ctlId is not equal to an empty
string).

Also, you should be awaare that the Page.Controls Collection is only a
Collection of those Controls immediately under the Page. You would need a
recursive function to get at all the nested Controls in the Page.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
A watched clock never boils.
 
A

Alan Silver

I believe you have at least one Control with a null id.

How can the ID be null? If the control itself is not null, and the ID is
a string (according to the SDK), then surely the ID should give you an
empty string. Please explain how it can be null.

I changed my code to...

if ((ctlId != null) && (ctlId.StartsWith("X_"))) {

.... and it works fine now. So you were absolutely correct, although I
still don't really understand how the situation could arise.
Unfortunately, it is
not an error to concatenate a string with a null string. So:

Trace.Warn("ctlId = @" + ctlId + "@");

will not throw an exception. Neither will:

if (ctlId != "")

If ctlId is null, it will evaluate to true (ctlId is not equal to an empty
string).

Hmm, that's really stupid. Still forewarned is forearmed as they say ;-)
Also, you should be awaare that the Page.Controls Collection is only a
Collection of those Controls immediately under the Page. You would need a
recursive function to get at all the nested Controls in the Page.

Ha, here's me thinking that it's not an issue in this case, and feeling
smug that I didn't make *that* mistake, when I realised that the
controls I wanted were all inside a placeholder, and so didn't show up
in the loop I coded. I needed to loop over the placeholder's control
collection.

Ho hum. Thanks for the warning there, you saved me hours of debugging!!
 

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

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top