473,407 Members | 2,306 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,407 software developers and data experts.

Now this is an interesting question

Jax
I have this code, behold:

string message = "Are you sure? You will lose this
customer forever. (Well you'll have re-create from
scratch)";
string caption = "Delete";
MessageBoxButtons buttons = MessageBoxButtons.YesNo;
DialogResult result;
result = MessageBox.Show(this, message, caption, buttons);
if(result == DialogResult.Yes)
{
foreach(ListViewItem lvi in this.lstCustomers.Items)
{
if(lvi.ImageIndex == 1)
{
Customer c = (Customer)lvi.Tag;
alCustomers.Remove(c);
//this.lstCustomers.Items.Remove(lvi);
break;
}
}
}
if(alCustomers.Count<1)
{
this.tabCustomers.Visible = false;
}

Okay what this code does is delete the selected customer
(the one with image an image index of 1) removes the
ListViewItem from the listview then checks to see if there
are no customers left in the array list, if not it hides a
tab control.

What is entertaining is that if you go through this code
with the debugger and remove the slashed out line (where i
remove the listviewitem) alCustomers will never have a
count of zero.
It will for a little while until it hits the slashed out
line, at that point it regains the customer it previously
removed.
Anyone got any ideas why? (I'm assuming it's something to
do with the tag??)

Any help, as always, greatly appreciated, many smiles and
thanks to repliers.

jax
Nov 15 '05 #1
7 1357
Hi Jax,

Try this:

if(result == DialogResult.Yes)
{
ListViewItem tobedeleted = null;
foreach(ListViewItem lvi in this.lstCustomers.Items)
{
if(lvi.ImageIndex == 1)
{
Customer c = (Customer)lvi.Tag;
alCustomers.Remove(c);
tobedeleted = lvi;
break;
}
}
if ( tobedeleted != null )
this.lstCustomers.Items.Remove( tobedeleted);
}

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Jax" <an*******@discussions.microsoft.com> wrote in message
news:00****************************@phx.gbl...
I have this code, behold:

string message = "Are you sure? You will lose this
customer forever. (Well you'll have re-create from
scratch)";
string caption = "Delete";
MessageBoxButtons buttons = MessageBoxButtons.YesNo;
DialogResult result;
result = MessageBox.Show(this, message, caption, buttons);
if(result == DialogResult.Yes)
{
foreach(ListViewItem lvi in this.lstCustomers.Items)
{
if(lvi.ImageIndex == 1)
{
Customer c = (Customer)lvi.Tag;
alCustomers.Remove(c);
//this.lstCustomers.Items.Remove(lvi);
break;
}
}
}
if(alCustomers.Count<1)
{
this.tabCustomers.Visible = false;
}

Okay what this code does is delete the selected customer
(the one with image an image index of 1) removes the
ListViewItem from the listview then checks to see if there
are no customers left in the array list, if not it hides a
tab control.

What is entertaining is that if you go through this code
with the debugger and remove the slashed out line (where i
remove the listviewitem) alCustomers will never have a
count of zero.
It will for a little while until it hits the slashed out
line, at that point it regains the customer it previously
removed.
Anyone got any ideas why? (I'm assuming it's something to
do with the tag??)

Any help, as always, greatly appreciated, many smiles and
thanks to repliers.

jax

Nov 15 '05 #2
Jax <an*******@discussions.microsoft.com> wrote:
I have this code, behold:


<snip>

Could you produce a short but complete program which demonstrates the
problem? See http://www.pobox.com/~skeet/complete.html for what I mean.
It does look interesting, but I'd like to get a complete repro to start
with.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #3
Hi,

I think that it may be either that he is modifying the collection that he
is iterating on:

foreach(ListViewItem lvi in this.lstCustomers.Items)
{
if(lvi.ImageIndex == 1)
{
this.lstCustomers.Items.Remove(lvi);
}
}
Or it may be also due that he is removing the selected element in the
ListView , and he may be handling the ListView.SelectedIndexChanged event.

Jax:
Can you place a breakpoint in the ListView.SelectedIndexChanged handler ?
( if used ). maybe there lay the answer.
Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Jax <an*******@discussions.microsoft.com> wrote:
I have this code, behold:


<snip>

Could you produce a short but complete program which demonstrates the
problem? See http://www.pobox.com/~skeet/complete.html for what I mean.
It does look interesting, but I'd like to get a complete repro to start
with.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #4
<"Ignacio Machin \( .NET/ C# MVP \)" <ignacio.machin AT
dot.state.fl.us>> wrote:
I think that it may be either that he is modifying the collection that he
is iterating on:


Indeed - hadn't spotted that. I'd have expected to see an exception in
that case...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #5
Hi Jon,
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
<"Ignacio Machin \( .NET/ C# MVP \)" <ignacio.machin AT
dot.state.fl.us>> wrote:
I think that it may be either that he is modifying the collection that he is iterating on:
Indeed - hadn't spotted that. I'd have expected to see an exception in
that case...


Yes, I would expect that too, but Jax says nothing about an exception, so I
assume it does not happen.

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #6
Jax
Jon and Ingacio,

Thank you both so much for your replies, I find your input
very helpful.
Unfortunately this is a work problem and i'm now at home
without the code to hand.
I will post up a sample app on monday when I get back to
it.

As far as exceptions go there are none at all, it runs
without a problem.
My main concern is that in modifying the collection while
iterating through it I have messed up something somewhere
and this is a concern because i have used this code
(usually with arraylists or listboxes) quite a few times.
I assumed that the break keyword saved me from any
problems, is that generally the case?

I will try the new code that ingacio recommended, it
sounds a little bit like the fix I applied which was this:

if(result == DialogResult.Yes)
{
foreach(ListViewItem lvi in this.lstCustomers.Items)
{
if(lvi.ImageIndex == 1)
{
Customer c = (Customer)lvi.Tag;
alCustomers.Remove(c);
break;
}
}
if ( alCustomers.Count == 0 )
this.lstCustomers.Items.Clear();
}

I'll get back to this on monday and see if it effects the
Arraylist when there is more then one customer as i'm not
certain that i've checked that out properly or not(i think
I have but i'll double check, which is why my fix only
applies to 0).

Thanks again, you're both a credit to this newsgroup :)

jax

-----Original Message-----
Hi Jon,
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft. com...
<"Ignacio Machin \( .NET/ C# MVP \)" <ignacio.machin AT
dot.state.fl.us>> wrote:
> I think that it may be either that he is modifying
the collection that
he > is iterating on:
Indeed - hadn't spotted that. I'd have expected to see an exception in that case...


Yes, I would expect that too, but Jax says nothing about

an exception, so Iassume it does not happen.

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

.

Nov 15 '05 #7
The problem was due to additional code in selected item
changed event.
I sorted it but thanks for your help :)

jax
-----Original Message-----
Jon and Ingacio,

Thank you both so much for your replies, I find your inputvery helpful.
Unfortunately this is a work problem and i'm now at home
without the code to hand.
I will post up a sample app on monday when I get back to
it.

As far as exceptions go there are none at all, it runs
without a problem.
My main concern is that in modifying the collection while
iterating through it I have messed up something somewhere
and this is a concern because i have used this code
(usually with arraylists or listboxes) quite a few times.
I assumed that the break keyword saved me from any
problems, is that generally the case?

I will try the new code that ingacio recommended, it
sounds a little bit like the fix I applied which was this:

if(result == DialogResult.Yes)
{
foreach(ListViewItem lvi in this.lstCustomers.Items)
{
if(lvi.ImageIndex == 1)
{
Customer c = (Customer)lvi.Tag;
alCustomers.Remove(c);
break;
}
}
if ( alCustomers.Count == 0 )
this.lstCustomers.Items.Clear();
}

I'll get back to this on monday and see if it effects the
Arraylist when there is more then one customer as i'm not
certain that i've checked that out properly or not(i thinkI have but i'll double check, which is why my fix only
applies to 0).

Thanks again, you're both a credit to this newsgroup :)

jax

-----Original Message-----
Hi Jon,
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft .com...
<"Ignacio Machin \( .NET/ C# MVP \)" <ignacio.machin AT dot.state.fl.us>> wrote:
> I think that it may be either that he is modifyingthe collection that
he
> is iterating on:

Indeed - hadn't spotted that. I'd have expected to see
an exception in that case...


Yes, I would expect that too, but Jax says nothing

aboutan exception, so I
assume it does not happen.

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

.

.

Nov 15 '05 #8

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

Similar topics

2
by: pythonUser_07 | last post by:
Is this the correct place to post a jython question? I posted the following in the jython group, but I figured I'd post here too: _________________________________________ I am assuming that...
8
by: ºa¤Ö | last post by:
I find a interesting question, and I cannot solve it @.@ If i want to insert unicode data, I need using recordset.addnew instead of using "insert into table" query or "stored procedure" All...
2
by: Dylan Phillips | last post by:
A strang error is occurring when I run the following code: SqlConnection c = new SqlConnection(); c.ConnectionString = "Initial Catalog=Northwind;user id=sa;password=kat1ie;Data Source=server";...
7
by: git_cs | last post by:
Hey, guys and gals Somedays ago, I had asked for the DES algorithm in C language. Although I have written the algorthim in C myself, I am facing a peculiar problem, which I hope some of u guys and...
12
by: Dino M. Buljubasic | last post by:
I have two applications App1 and App2. App1 is just checking for new version of App2. If it finds newer version of App2 it will download App1 AND App2. (both of them). That is what I am...
6
by: Claude Yih | last post by:
Hi, everyone. I noticed an interesting thing about fread() this afternoon. Well, I can't see why so I post this message in the hope of getting some explanation. Please help me. I wrote the...
26
by: v4vijayakumar | last post by:
Happened to see my old (last millennium) c code. Almost forgot that I wrote it . It is interesting. :-) int sqrt(int no) { int t; no = t * t; return t; }
5
by: Will Honea | last post by:
I've hit an interesting trap trying to migrate data off an OS/2 server running version 7.2 (fp14) over to 8.2 on Linux. Seems that one table has a column defined in the DDL as "BIGINT NOT NULL...
126
by: jacob navia | last post by:
Buffer overflows are a fact of life, and, more specifically, a fact of C. All is not lost however. In the book "Value Range Analysis of C programs" Axel Simon tries to establish a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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,...
0
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...
0
isladogs
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...

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.