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
16 10875
<ke************ @gmail.com> wrote in message
news:11******** *************@z 14g2000cwz.goog legroups.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.
Otis Mukinfus <ph***@emailadd ress.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.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
On Thu, 9 Feb 2006 07:09:31 -0000, Jon Skeet [C# MVP]
<sk***@pobox.co m> wrote: Otis Mukinfus <ph***@emailadd ress.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
Otis Mukinfus <ph***@emailadd ress.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.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
> 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
Otis Mukinfus wrote: On Thu, 9 Feb 2006 07:09:31 -0000, Jon Skeet [C# MVP] <sk***@pobox.co m> 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
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.co m> 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 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 this variable? It's used at three places
throughout the program. I have two static const int...
|
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 inhibit warning)
keywords.c:60:11: Storage Keywords.Keyword released
| |
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 because
it is a field of a marshal-by-reference class"
|
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 usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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 captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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 into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |