473,587 Members | 2,413 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 10869
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(SomeExcep tion) // 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.Ge tEnumerator();
iterator.HasNex t;
iterator.MoveNe xt())
{
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.co m>
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.c om> 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(SomeExce ption) // 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

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

Similar topics

35
3199
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 presented with an indefined variable. It makes sense to me to warn if an unacceptably defined var is passed but it
3
17342
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 char*g_mdi_child_class_name' defined but not used line 28 of globals.h is: static const char* g_mdi_child_class_name = "MDIChildClass"; Why do I get this warning for...
7
1983
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
5169
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 warning. How do I inhibit the warning for just MY_STATIC_CONST_INT_VAR?
3
2712
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 storage Keywords.Keyword reachable from global A global variable does not satisfy its annotations when control is transferred. (Use -globstate to...
7
3476
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
479
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
3106
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 Function 'Dec2hms' doesn't return a value on all code paths. A null reference exception could occur at run time when the result is used.
3
14495
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 variables on all the other forms. But this gives the warning - "Accessing a member on 'LabTest.frmSBTUSA.ViewUserID' may cause a runtime exception...
4
2144
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; } By definition, readonly fields can only be initialized inside a
0
7920
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7849
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8215
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
6626
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5718
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5394
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3844
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1454
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1189
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.