473,586 Members | 2,702 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

My silliest extention made yet 2008...

... and it is only January. This might get worse. =)

I really like languages like C#, that has a foreach on IEnumerable<T>.

But sometimes you need to go by index and you have to for-loop.
In C-dervived languages it typically looks like this:

for (int i = 0; i < theList.Count -1 ; i++) { [etc.. etc..] }

I have have always liked 0-based offsets, even if we talk in terms of index.
But for being a index, I have always disliked the -1 part of the code on
Count/Length.

I don't think I am alone when confessing that sometimes you miss that little
minus one, with a index out of bounds exception. A lame reason for a bug and
your program to be flawed.

So I tried creating extentions on IList<T>
Simple function that simply returns Count-1

First up was UpperBound()
for (int i = 0; i < theList.UpperBo und() ; i++) { [etc.. etc..] }

That made it look way tooo much like Visual Basic for me, so no thanks.

In Delphi/Pascal you have pred() and succ() on ordinal types, so you could
(and should) do:
for i := 0 to pred(theList.Co unt) do begin [etc.. etc..] end;

I tried that:
for (int i = 0; i < theList.PredCou nt() ; i++) { [etc.. etc..] }

That made no sense at all, so I tried end:
for (int i = 0; i < theList.End() ; i++) { [etc.. etc..] }

I've tried several combinations of stop and end words and the best I can
come with is .ToEnd()

for (int i = 0; i < theList.ToEnd() ; i++) { [etc.. etc..] }

Any thoughts, except that I should ToEnd even thinking of this? =)

- Michael Starberg

Jan 17 '08 #1
22 1344
Michael Starberg wrote:
.. and it is only January. This might get worse. =)
There's some inspiring quote about a day without stupid questions being like
a day without Usenet.

Actually, I just made that up, but there ought to be.
I really like languages like C#, that has a foreach on IEnumerable<T>.

But sometimes you need to go by index and you have to for-loop.
In C-dervived languages it typically looks like this:

for (int i = 0; i < theList.Count -1 ; i++) { [etc.. etc..] }

I have have always liked 0-based offsets, even if we talk in terms of index.
But for being a index, I have always disliked the -1 part of the code on
Count/Length.
Probably because it's wrong. Your loop will go from 0 through theList.Count
- 2... You're missing the last element, which is theList.Count - 1. That is,
unless the list is empty. Then your loop will immediately barf on an invalid
index, since 0 is certainly not smaller than -1.

for (int i = 0; i != theList.Count; ++i) { ... }

This is a perfectly natural way of writing things. In fact, there's a reason
people prefer zero-based indexing: it gives you much less opportunity for
off-by-one errors.
I don't think I am alone when confessing that sometimes you miss that little
minus one, with a index out of bounds exception. A lame reason for a bug and
your program to be flawed.
See above... don't write that, then.
Any thoughts, except that I should ToEnd even thinking of this? =)
Zero is the number thou shalt start from, and the number of the starting
shall be zero. One thou shalt not start from, unless thou by counting
apples. Two is right out.

--
J.
Jan 17 '08 #2
On Thu, 17 Jan 2008 09:59:46 -0800, Michael Starberg
<mi************ *************** @gmail.comwrote :
But sometimes you need to go by index and you have to for-loop.
In C-dervived languages it typically looks like this:

for (int i = 0; i < theList.Count -1 ; i++) { [etc.. etc..] }

I have have always liked 0-based offsets, even if we talk in terms of
index.
But for being a index, I have always disliked the -1 part of the code on
Count/Length.
I have no idea what you're talking about. If you want to enumerate every
element in the list, by index, and the condition is "<" instead of "<="
(which is typical), you need "theList.Count" , not "theList.Co unt - 1".

The code you posted is going to miss the last element.

Pete
Jan 17 '08 #3
Michael Starberg wrote:
for (int i = 0; i < theList.Count -1 ; i++) { [etc.. etc..] }

I have have always liked 0-based offsets, even if we talk in terms of
index. But for being a index, I have always disliked the -1 part of
the code on Count/Length.
Huh? In C#, you don't use -1 at all (you must be confusing it with a
Delphi for loop):

for (int i = 0; i < theList.Count; ++i)

Note that the second is a while condition, IOW:

for (A; B; C)
{
D;
}

is equivalent to:

A;
while (B)
{
D;
C;
}

--
Rudy Velthuis http://rvelthuis.de

"A lie gets halfway around the world before the truth has a
chance to get its pants on."
-- Sir Winston Churchill (1874-1965)
Jan 17 '08 #4
"Jeroen Mostert" <jm******@xs4al l.nlwrote in message
news:47******** *************** @news.xs4all.nl ...
Michael Starberg wrote:
Actually, I just made that up, but there ought to be.
I will do my best =)
Zero is the number thou shalt start from, and the number of the starting
shall be zero. One thou shalt not start from, unless thou by counting
apples. Two is right out.
Does that works on cute mammals with sharp teath, garding a cave?

I like this quote:

Should array indices start at 0 or 1? My compromise of 0.5 was rejected
without, I thought, proper consideration. (Stan Kelly-Bootle)

Jan 17 '08 #5
"Peter Duniho" <Np*********@nn owslpianmk.comw rote in message
news:op******** *******@petes-computer.local. ..
On Thu, 17 Jan 2008 09:59:46 -0800, Michael Starberg
<mi************ *************** @gmail.comwrote :

>I have no idea what you're talking about. The code you posted is going to
miss the last element.
>Pete
Glad you noticed. The whole thread is like a joke, or just a way to get some
input.
But you get the gold-medal for spotting the problem.

Maybe some future maintainer would see the ToEnd, write the loop with i <
myList.ToEnd(), and then miss two elements.

My post was to show that there is always a problem with for-loops and why
foreach is better. =)

- Michael Starberg


Jan 17 '08 #6
"Rudy Velthuis" <ne********@rve lthuis.dewrote in message
news:xn******** ********@news.m icrosoft.com...
Michael Starberg wrote:
Thanks dentist! =)

- Michael Starberg
Jan 17 '08 #7
On Thu, 17 Jan 2008 10:59:22 -0800, Michael Starberg
<mi************ *************** @gmail.comwrote :
[...]
My post was to show that there is always a problem with for-loops and why
foreach is better. =)
So, when will you actually show that? We await with bated breath...

Pete
Jan 17 '08 #8
Michael Starberg <mi************ *************** @gmail.comwrote :
.. and it is only January. This might get worse. =)

I really like languages like C#, that has a foreach on IEnumerable<T>.

But sometimes you need to go by index and you have to for-loop.
I realise this thread is a joke, but you could easily write an
extension method of:

public static void ForEach<T>(this IEnumerable<Tso urce,
Action<T,intact ion)
{
int index=0;
foreach (T t in source)
{
action (t, index++);
}
}

Then call it with:

theList.ForEach ((value, index) =>
{
Console.WriteLi ne ("The value at index {0} is {1}",
index, value);
});

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
Jan 17 '08 #9
"Peter Duniho" <Np*********@nn owslpianmk.comw rote in message
news:op******** *******@petes-computer.local. ..
On Thu, 17 Jan 2008 10:59:22 -0800, Michael Starberg
<mi************ *************** @gmail.comwrote :
>[...]
My post was to show that there is always a problem with for-loops and why
foreach is better. =)
>So, when will you actually show that? We await with bated breath...
>Pete
Then you have to wait long, Pete.
I don't start silly threads and then explain myself.
I just had fun doing some extentions, no harm done.

- Michael Starberg
Jan 17 '08 #10

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

Similar topics

2
5014
by: Jesper Olsen | last post by:
I have a python extention implemented in C/C++ - the extention itself is internally implemented in C++, but the interface is pure C, so that it can easily be called from a python C-wrapper. The extention is compiled with distutils, and this has worked well in both Windows and Linux environments. Now, however, I get an
0
1837
by: Atul Kshirsagar | last post by:
I am embedding python in my C++ application. I am using Python *2.3.2* with a C++ extention DLL in multi-threaded environment. I am using SWIG-1.3.19 to generate C++ to Python interface. Now to explain it in details, 1. Python initialization and finalization is done in the *main* thread. 2. For each new thread I create a separate...
1
2592
by: Carl Ogawa | last post by:
How do I make .cgi extention work? I installed ActivePerl 5.8. My CGI scripts work fine with .PL extention but not .CGI extention although I associated CGI extention as exactly same as PL extention in WinXP Prof . Where else should I change to make it work?
1
3077
by: MFA | last post by:
Hi all I have installed Front page server Extention on IIS 5.0 it was working fine. I was able to load project from Visual Interdav and doing well. Yesterday I installed a security update from Microsoft and after that every thing stop working and I am getting the error below when ever I am trying to connect to server using V Interdev. Error...
3
1778
by: Tuvas | last post by:
I am currently writing an extention module that needs to recieve a list of characters that might vary in size from 0 to 8. This is written as a list of characters rather than a string because it's easier to manipulate. However, when I pass this list of characters into the extention module, it keeps giving errors. Is there a way to do one of...
1
1639
by: godfather96 | last post by:
I am using a web control called eXml which is an extention to ms xml web control which supports xslt 2.0 when trying to group elements i receive the following error: group-by is not a valid extention element is there a namespace or something a need to add. my source is below
5
3125
by: Krustov | last post by:
I have the following list of image files . When searching the latest (numbered file) in this particular case its background_4.*** and its a .jpg file - but - the latest file in the list could also be a .gif file at times . zimages_background/background_1.jpg zimages_background/background_2.jpg zimages_background/background_3.gif...
26
2832
by: Max2006 | last post by:
Hi, C# 3.0 extension methods become useful for us. Do we have the similar concept for extension properties? Thank you, Max
0
7911
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
8200
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
8338
jinu1996
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...
0
6610
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...
0
5390
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
3864
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2345
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1448
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1179
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.