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 16 10863
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
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. :)
lol, thats funny. it was starting to give me a headache as well :)
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
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 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
> Does that help?
Oh, yes. Tremendously, thanks. That's why I love this forum: I learn
something new every day. :)
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
|
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...
|
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
|
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?
|
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...
| |
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)
|
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;
|
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.
|
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...
|
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
|
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...
| |
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...
|
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. ...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
|
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...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |