foreach enhancement

D

Daniel O'Connell [C# MVP]

Michael C said:
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].
 
M

Michael C

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);
 
D

Daniel O'Connell [C# MVP]

Michael C said:
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.
 
M

Michael C

Daniel O'Connell said:
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.
 
D

Daniel O'Connell [C# MVP]

Michael C said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top