By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,844 Members | 1,859 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,844 IT Pros & Developers. It's quick & easy.

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

P: n/a
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.

--
Alan Silver
(anything added below this line is nothing to do with me)
Nov 19 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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.

"Alan Silver" <al*********@nospam.thanx> wrote in message
news:uc**************@nospamthankyou.spam...
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.

--
Alan Silver
(anything added below this line is nothing to do with me)

Nov 19 '05 #2

P: n/a
>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!!

--
Alan Silver
(anything added below this line is nothing to do with me)
Nov 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.