المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : How to exit a while loop inside of a while loop



C# Programming
11-18-2009, 01:28 AM
For all, this is a question on the best/most elegant way to do this.

I am reading records from a database. I start the read with a database reader.ExecuteReader() method. Then I put a while Reader.read() statement to start the loop. While within this loop, I have to add up figures for certain records (store numbers). For this I use another while loop (while store number == store number). Within this while loop, I advance the records read with another reader.read() method.
The problem with this is that when it reaches the last record, the test for store number is not valid because the next record is null.

I called over my colleague to help me out with this problem, and he suggested a break on an if (!dbreader.read()). I thought this was a dirty way to get our of this, but it works!

I know I have not supplied code, but I hope you all can see it the way I explained it (this code is on a virtual machine that I do not have remote access to at the moment), But if needed I will provide.

The end question is, is it ok to use break? How often is it really used? I have always been taught not to use break/goto/gosub in C# and I just thought I would get some input here.

Thanx in advance!

Jude