473,326 Members | 2,680 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

warning CS0168: The variable 'el' is declared but never used?

Suppose that I just want to count the elements in a collection, so I do
this:

int i = 0;
foreach (MyElement el in MyCollection)
{
i++;
}
return i;

Is there any way in the 2003 compiler to avoid the warning:
warning CS0168: The variable 'el' is declared but never used?

I don't want to turn off the warning (can't anyway in VS 2003) I want
to fix the code without doing something wasteful...

Thanks!

And yes, I do know that you could just get the length of the
collection... my real example is a bit more complex than that presented
here.

-Kelly

--------------------------------
From: Kelly Anderson

Feb 8 '06 #1
16 10813
use a for loop instead of a foreach loop:

int count = 0;
int i;
for (i = 0; i<MyCollection.Count; i++) {
count++;
}

return count;

that should work

Feb 8 '06 #2
Could you give more details about the "real example"?

Trying to think of a situation in which you would want to iterate over
the members of a collection but not actually do anything with the
individual members is giving me a headache. If you could give more
details, perhaps it would help my dearth of imagination. :)

Feb 8 '06 #3
lol, thats funny. it was starting to give me a headache as well :)

Feb 8 '06 #4

Bruce Wood wrote:
Could you give more details about the "real example"?

Trying to think of a situation in which you would want to iterate over
the members of a collection but not actually do anything with the
individual members is giving me a headache. If you could give more
details, perhaps it would help my dearth of imagination. :)


Ok, sure. In the real example, there is an iterator. The iterator is
incredibly complex, skipping items in the real collection depending on
a huge number of variables. I want to know how many items will be
iterated over prior to the iteration beginning for various reasons. So
basically, I'm wanting to know how many items there are, but Count
won't work because many items in the actual collection will be skipped
according to user preferences, and many other business rules.

So I want a Count, but based on how many items the iterator will visit,
not how many items are actually in the collection.

All I really want to do is get rid of the warning without disabling the
warning. For exception handling, you can do this by getting rid of the
declaration. For example:

try
{
// something
}
catch(SomeException) // no declaration
{
// so something, but without looking at the exception.
}

If I try a similar approach with foreach, it doesn't compile:

foreach (Element in MyCollection) // doesn't compile.
{
}

Does that help?

-Kelly

Feb 8 '06 #5

DKode wrote:
use a for loop instead of a foreach loop:

int count = 0;
int i;
for (i = 0; i<MyCollection.Count; i++) {
count++;
}

return count;

that should work


If it were a simple collection, it would. But I have a complex iterator
that I'm using to iterate over only SOME members of a complex
collection. I just want to know how many will be iterated over without
the compiler warning. The for won't work in this case.

-Kelly

Feb 8 '06 #6
ke************@gmail.com <ke************@gmail.com> wrote:
Ok, sure. In the real example, there is an iterator. The iterator is
incredibly complex, skipping items in the real collection depending on
a huge number of variables. I want to know how many items will be
iterated over prior to the iteration beginning for various reasons. So
basically, I'm wanting to know how many items there are, but Count
won't work because many items in the actual collection will be skipped
according to user preferences, and many other business rules.

So I want a Count, but based on how many items the iterator will visit,
not how many items are actually in the collection.


So you could do:

int count=0;
for (IEnumerator iterator = MyCollection.GetEnumerator();
iterator.HasNext;
iterator.MoveNext())
{
count++;
}

That's doing what C# would be doing under the covers (barring disposal
of the enumerator) and it makes it clearer why you're doing it. It's
more code, but it's clearer in terms of intent, IMO.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 8 '06 #7
> Does that help?

Oh, yes. Tremendously, thanks. That's why I love this forum: I learn
something new every day. :)

Feb 8 '06 #8
On 8 Feb 2006 11:49:30 -0800, "DKode" <dk****@gmail.com> wrote:
lol, thats funny. it was starting to give me a headache as well :)


On the other hand, I'm slowly working my way through the, ahem, hard
work you two have already done and I'm having a giggle. ;-)

Ken Wilson
Seeking viable employment in Victoria, BC
Feb 8 '06 #9
On 8 Feb 2006 12:42:06 -0800, "ke************@gmail.com"
<ke************@gmail.com> wrote:

Bruce Wood wrote:
Could you give more details about the "real example"?

Trying to think of a situation in which you would want to iterate over
the members of a collection but not actually do anything with the
individual members is giving me a headache. If you could give more
details, perhaps it would help my dearth of imagination. :)


Ok, sure. In the real example, there is an iterator. The iterator is
incredibly complex, skipping items in the real collection depending on
a huge number of variables. I want to know how many items will be
iterated over prior to the iteration beginning for various reasons. So
basically, I'm wanting to know how many items there are, but Count
won't work because many items in the actual collection will be skipped
according to user preferences, and many other business rules.

So I want a Count, but based on how many items the iterator will visit,
not how many items are actually in the collection.

All I really want to do is get rid of the warning without disabling the
warning. For exception handling, you can do this by getting rid of the
declaration. For example:

try
{
// something
}
catch(SomeException) // no declaration
{
// so something, but without looking at the exception.
}

If I try a similar approach with foreach, it doesn't compile:

foreach (Element in MyCollection) // doesn't compile.
{
}

Does that help?

-Kelly

Why do you want to get rid of the warning in the first place?

Otis Mukinfus
http://www.otismukinfus.com
http://www.tomchilders.com
Feb 9 '06 #10

<ke************@gmail.com> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com...

Bruce Wood wrote:
Could you give more details about the "real example"?

Trying to think of a situation in which you would want to iterate over
the members of a collection but not actually do anything with the
individual members is giving me a headache. If you could give more
details, perhaps it would help my dearth of imagination. :)


Ok, sure. In the real example, there is an iterator. The iterator is
incredibly complex, skipping items in the real collection depending on
a huge number of variables. I want to know how many items will be
iterated over prior to the iteration beginning for various reasons. So
basically, I'm wanting to know how many items there are, but Count
won't work because many items in the actual collection will be skipped
according to user preferences, and many other business rules.

So I want a Count, but based on how many items the iterator will visit,
not how many items are actually in the collection.


While its not a direct answer to your question, why not just filter the
collection down in successive layers? That will give you a complete
collection at the otehr end with all the benefits of a collection. I can't
help thinking that a complicated iterator is going to be hard to maintain
and pehaps a risky spot, performance wise.
Feb 9 '06 #11
Otis Mukinfus <ph***@emailaddress.com> wrote:

<snip>
Why do you want to get rid of the warning in the first place?


I don't know about Kelly, but I view warnings as errors - I don't allow
my code to compile with warnings. Because *some* warnings are
definitely significant, you want to make sure you don't have any of
those - and the easiest way of doing that is to ensure that there are
never any warnings at all.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 9 '06 #12
On Thu, 9 Feb 2006 07:09:31 -0000, Jon Skeet [C# MVP]
<sk***@pobox.com> wrote:
Otis Mukinfus <ph***@emailaddress.com> wrote:

<snip>
Why do you want to get rid of the warning in the first place?


I don't know about Kelly, but I view warnings as errors - I don't allow
my code to compile with warnings. Because *some* warnings are
definitely significant, you want to make sure you don't have any of
those - and the easiest way of doing that is to ensure that there are
never any warnings at all.

Normally I would agree 100% Jon. But with that error I'd be tempted
to let it slide, or as many have suggested, do the process another
way. He is using foreach as convenience anyway (as we all do).

Couldn't one loop through each element of the list and check it's type
in order to do what he wants? There's probably a case to be made
against that too (speed comes to mind) ;o)

Otis Mukinfus
http://www.otismukinfus.com
http://www.tomchilders.com
Feb 9 '06 #13
Otis Mukinfus <ph***@emailaddress.com> wrote:
I don't know about Kelly, but I view warnings as errors - I don't allow
my code to compile with warnings. Because *some* warnings are
definitely significant, you want to make sure you don't have any of
those - and the easiest way of doing that is to ensure that there are
never any warnings at all.
Normally I would agree 100% Jon. But with that error I'd be tempted
to let it slide, or as many have suggested, do the process another
way. He is using foreach as convenience anyway (as we all do).
The trouble is that if you let even one warning slide, it's harder to
tell when you get more - especially when you then find that there are a
few other things that you might as well let slide to the same amount,
for instance. You soon get into the stage where you can't *really
easily* tell whether or not the code you've just added has introduced a
new warning. Much better to just be draconian to start with :)
Couldn't one loop through each element of the list and check it's type
in order to do what he wants? There's probably a case to be made
against that too (speed comes to mind) ;o)


More for duplication of code reasons, I'd say. It sounds like Kelly
already has that code somewhere, so we might as well make use of it
again...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 9 '06 #14
> Couldn't one loop through each element of the list and check it's type
in order to do what he wants? There's probably a case to be made
against that too (speed comes to mind) ;o)


In this particular case, it's not an issue of type, it's an issue of
business object state.

Just to give a simplified taste of the problem, I'm iterating over
teeth and data about teeth in a mouth object, and I want to skip
missing teeth and such. I might also skip teeth (or certain information
about the tooth) based upon states of the user interface (stuff hidden
and such) or some pretty complex user preferences. Whether the tooth is
missing comes from yet another object, so I don't have a collection, I
have a set of business object states. I have six classes involved in
the iteration computation, many many hundreds of lines of code.

It's not one of the more simple cases you run into in the normal course
of dealing with objects. The actual logic is brutally complex. Just
GetNext is 43 lines of code. It's ugly stuff.

By the way, I really liked the solution of using the
enumerator/iterator directly. I'll probably just do that. Thanks!

-Kelly

Feb 9 '06 #15

Otis Mukinfus wrote:
On Thu, 9 Feb 2006 07:09:31 -0000, Jon Skeet [C# MVP]
<sk***@pobox.com> wrote:
Why do you want to get rid of the warning in the first place?


I don't know about Kelly, but I view warnings as errors - I don't allow
my code to compile with warnings. Because *some* warnings are
definitely significant, you want to make sure you don't have any of
those - and the easiest way of doing that is to ensure that there are
never any warnings at all.

Normally I would agree 100% Jon. But with that error I'd be tempted
to let it slide, or as many have suggested, do the process another
way. He is using foreach as convenience anyway (as we all do).

Couldn't one loop through each element of the list and check it's type
in order to do what he wants? There's probably a case to be made
against that too (speed comes to mind) ;o)


The problem is that these spurious warnings can mask serious issues. If
I get rid of the spurious warnings, then I can see the warnings that I
care about. If there are spurious warnings, then one I care about could
be lurking in the list, and I wouldn't see it as something that needed
immediate attention.

-Kelly

Feb 9 '06 #16
On 9 Feb 2006 14:05:33 -0800, "ke************@gmail.com"
<ke************@gmail.com> wrote:

Otis Mukinfus wrote:
On Thu, 9 Feb 2006 07:09:31 -0000, Jon Skeet [C# MVP]
<sk***@pobox.com> wrote:
>> Why do you want to get rid of the warning in the first place?
>
>I don't know about Kelly, but I view warnings as errors - I don't allow
>my code to compile with warnings. Because *some* warnings are
>definitely significant, you want to make sure you don't have any of
>those - and the easiest way of doing that is to ensure that there are
>never any warnings at all.

Normally I would agree 100% Jon. But with that error I'd be tempted
to let it slide, or as many have suggested, do the process another
way. He is using foreach as convenience anyway (as we all do).

Couldn't one loop through each element of the list and check it's type
in order to do what he wants? There's probably a case to be made
against that too (speed comes to mind) ;o)


The problem is that these spurious warnings can mask serious issues. If
I get rid of the spurious warnings, then I can see the warnings that I
care about. If there are spurious warnings, then one I care about could
be lurking in the list, and I wouldn't see it as something that needed
immediate attention.

-Kelly

All good reasoning gentlemen.

Otis Mukinfus
http://www.otismukinfus.com
http://www.tomchilders.com
Feb 10 '06 #17

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

35
by: jerrygarciuh | last post by:
Hi all, I was just wondering what popular opinion is on PHP giving this warning: Warning: Invalid argument supplied for foreach() in /home/boogerpic/public_html/my.php on line 6 when...
3
by: William Payne | last post by:
Hello, when compiling my program I get a warning in one of my header files (globals.h) each time a source file includes it. The warning reads: globals.h:28: warning: `const...
7
by: chand | last post by:
Hi., In my api.py file 'g_opt_list' is defined globally g_opt_list =,,,,,,] I am using this global list in the fucntion def function (): gloabl g_opt_list
15
by: Robert | last post by:
Hi, Flexelint says: Warning 528: Symbol 'MY_STATIC_CONST_INT_VAR' (line 426, file headfile.h) not referenced I do understand why, but I'm not sure I wanna change the code to remove the...
3
by: Bas Wassink | last post by:
Hello there, I'm having trouble understanding a warning produced by 'splint', a code-checker. The warning produced is: keywords.c: (in function keyw_get_string) keywords.c:60:31: Released...
7
by: ChrisB | last post by:
Hello: I notice that the following statements generate a "the variable 'e' is declared but never used" warning: try { Company.Fetch(CompanyID); } catch(RecordNotFoundException e)
3
by: kellyatdentrix | last post by:
Suppose that I just want to count the elements in a collection, so I do this: int i = 0; foreach (MyElement el in MyCollection) { i++; } return i;
27
by: Terry | last post by:
I am getting the following warning for the below function. I understand what it means but how do I handle a null reference? Then how do I pass the resulting value? Regards Warning 1...
3
by: preitymathur0422 | last post by:
I m working on a MDI application. Here on the parent form I had two variables declared as public int curUserID = 0, ViewUserID = 1; the form is inherited by the "Form" class. I m using these...
4
by: cody | last post by:
It is possible to declare and use/instantiate a class with a uninitialized readonly field without even a compiler warning. Why don't I get warnings? public class Stuff { public readonly int a;...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.