473,395 Members | 1,968 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,395 software developers and data experts.

foreach enhancement

What about an enhancement of foreach loops which allows a syntax like that:

foeach(int i in 1..10) { } // forward
foeach(int i in 99..2) { } // backwards
foeach(char c in 'a'..'z') { } // chars
foeach(Color c in Red..Blue) { } // using enums

It should work with all integral datatypes. Maybe we can step a bit further:

foeach(int i in 1..10, 30..100) { } // from 1 to 10 and 30 to hundred

And maybe we could also try to enhance the loop further and make it usable
for floating point types too:

foeach(float f in 1.0..9.5 : 0.5) { } // from 1.0 to 9.5 in steps of 0.5

This is far more readable as a for loop and makes the intend much clearer.
I always have to think if I write loops like "for (int i=list.Count-1; i<=0;
i--)".
This is unproductive and errorprone and hard to maintain. Thus, my proposal.

--
cody

[Freeware, Games and Humor]
www.deutronium.de.vu || www.deutronium.tk
Nov 16 '05
104 7026

"Michael C" <no****@lol.net> wrote in message
news:jF*******************@news4.srv.hcvlny.cv.net ...

"Daniel O'Connell [C# MVP]" <onyxkirx@--NOSPAM--comcast.net> wrote in
message news:#Z**************@tk2msftngp13.phx.gbl...
Note that back to back in's is a big reason why I was pushing for isin in
another portion of the thread.
Yeah, when I saw how the comprehensions interact with other code, the 'in'
problem became clear.
>
> Range = 1..5;
> List = [Range];
> Range = 6..10;
>


In that case its irrelevent.
Range = 6...10; creates a *new* object. You would just be replacing a
variables value, not the variable itself. IMHO, ranges should be
immutable
and joins should result in an entirely new range being generated.


That may be the case, but if we're assigning the range 1..5 to a variable,
and later assign the range 6..10 to the same variable... I suppose my
first
question is what happens to the original 1..5 range? I would expect that
(if it were an object) it would just be disposed of and the 6..10 range
replace it. The main question though was what does the variable List
contain after the instructions above? Is it [1..5], [6..10] or some other
value/values?


Tell me, what does
ArrayList list = new ArrayList();
string x="one";
list.Add(x);
x = "two";

what does Console.WriteLine(list[0]); write?
I see no reason to change existing behaviour here, a such the variable list
would be [1...5].

Nov 16 '05 #101

"Daniel O'Connell [C# MVP]" <onyxkirx@--NOSPAM--comcast.net> wrote in
message news:%2****************@TK2MSFTNGP12.phx.gbl...
Tell me, what does
ArrayList list = new ArrayList();
string x="one";
list.Add(x);
x = "two";

what does Console.WriteLine(list[0]); write?
I see no reason to change existing behaviour here, a such the variable list would be [1...5].


That's what I was wondering. So you'll basically be assigning the value of
the variable to the list, as opposed to assigning the variable itself, like
happens with TreeViews and TreeNodes.

// Generates an error
TreeNode tvn = new TreeNode("Node A");
tvw.Nodes.Add (tvn);
tvn.Text = "Node B";
tvw.Nodes.Add (tvn);

Nov 16 '05 #102

"Michael C" <mi*******@optonline.net> wrote in message
news:Oo**************@TK2MSFTNGP09.phx.gbl...

"Daniel O'Connell [C# MVP]" <onyxkirx@--NOSPAM--comcast.net> wrote in
message news:%2****************@TK2MSFTNGP12.phx.gbl...
Tell me, what does
ArrayList list = new ArrayList();
string x="one";
list.Add(x);
x = "two";

what does Console.WriteLine(list[0]); write?
I see no reason to change existing behaviour here, a such the variable list
would be [1...5].


That's what I was wondering. So you'll basically be assigning the value
of
the variable to the list, as opposed to assigning the variable itself,
like
happens with TreeViews and TreeNodes.

// Generates an error
TreeNode tvn = new TreeNode("Node A");
tvw.Nodes.Add (tvn);
tvn.Text = "Node B";
tvw.Nodes.Add (tvn);


Well, in this case you are assigning a tree node variable to the tree view
and then changing one of it its properties. This isn't the same as creating
a new object in the variable tvn.
In the case of ranges, each range results in a new object being assigned to
the variable, and as such no issues like this.

Nov 16 '05 #103

"Daniel O'Connell [C# MVP]" <onyxkirx@--NOSPAM--comcast.net> wrote in
message news:u4*************@TK2MSFTNGP10.phx.gbl...

Well, in this case you are assigning a tree node variable to the tree view
and then changing one of it its properties. This isn't the same as creating a new object in the variable tvn.
In the case of ranges, each range results in a new object being assigned to the variable, and as such no issues like this.


Yes, that's what I was trying to get clarification on. If, when you create
a list, it would be assigned the Range variable like a treeview node, or if
you would be assigning just the value of the variable, and if that would
require an ICloneable interface or not.

Thanks,
Michael C.
Nov 16 '05 #104

"Michael C" <no****@lol.net> wrote in message
news:jE*********************@news4.srv.hcvlny.cv.n et...

"Daniel O'Connell [C# MVP]" <onyxkirx@--NOSPAM--comcast.net> wrote in
message news:u4*************@TK2MSFTNGP10.phx.gbl...

Well, in this case you are assigning a tree node variable to the tree
view
and then changing one of it its properties. This isn't the same as

creating
a new object in the variable tvn.
In the case of ranges, each range results in a new object being assigned

to
the variable, and as such no issues like this.


Yes, that's what I was trying to get clarification on. If, when you
create
a list, it would be assigned the Range variable like a treeview node, or
if
you would be assigning just the value of the variable, and if that would
require an ICloneable interface or not.


Nope, due to immutability of ranges I don't think this will be an issue. The
principal behind why is different but the result is the same.
Nov 16 '05 #105

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

Similar topics

0
by: cody | last post by:
What about an enhancement of foreach loops which allows a syntax like that: foeach(int i in 1..10) { } // forward foeach(int i in 99..2) { } // backward foeach(char c in 'a'..'z') { } // chars...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
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...
0
tracyyun
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...
0
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,...

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.