Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Confused - control not set to instance inside foreach loop - please help

Reply
Thread Tools

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

 
 
Alan Silver
Guest
Posts: n/a
 
      11-07-2005
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)
 
Reply With Quote
 
 
 
 
Kevin Spencer
Guest
Posts: n/a
 
      11-07-2005
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" <alan-> wrote in message
news:...
> 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)



 
Reply With Quote
 
 
 
 
Alan Silver
Guest
Posts: n/a
 
      11-07-2005
>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)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Triple nested loop python (While loop insde of for loop inside ofwhile loop) Isaac Won Python 9 03-04-2013 10:08 AM
Understanding ForEach() loop with 3 parameters -- please help! almurph@altavista.com C Programming 16 01-30-2009 08:30 AM
Object not set to an instance (ERROR) INDEXOF in do loop? jason@cyberpine.com ASP .Net 2 06-06-2004 07:35 AM
bug in lexically scoped array not reset in foreach loop Gavin Sherlock Perl Misc 1 12-10-2003 10:36 PM
Help requird : got a problem with foreach loop??? Vinod. K Perl Misc 4 08-25-2003 12:54 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57