A situation where you have to use goto
-
foreach(DataRow dr in dataset.Tables["principal"].Rows) { if (dr["name"].ToString()==szPrincipal_name) { nPrincipal_Id = Convert.ToInt32(dr["principal_Id"].ToString()); bEncontrou = true; break; } nPrincipal_Id++; }
_labelRowDeleted: foreach (DataRow myRow in dataset.Tables["grant"].Rows) { if (myRow["principal_Id"].ToString() == nPrincipal_Id.ToString()) { myRow.Delete(); goto _labelRowDeleted; } }
Solutions are welcome. -
foreach(DataRow dr in dataset.Tables["principal"].Rows) { if (dr["name"].ToString()==szPrincipal_name) { nPrincipal_Id = Convert.ToInt32(dr["principal_Id"].ToString()); bEncontrou = true; break; } nPrincipal_Id++; }
_labelRowDeleted: foreach (DataRow myRow in dataset.Tables["grant"].Rows) { if (myRow["principal_Id"].ToString() == nPrincipal_Id.ToString()) { myRow.Delete(); goto _labelRowDeleted; } }
Solutions are welcome.How about this?
bool finished = false;
while (!finished)
{
try
{
foreach (DataRow myRow in dataset.Tables["grant"].Rows)
{
if (myRow["principal_Id"].ToString() == nPrincipal_Id.ToString())
{
myRow.Delete();
throw new Exception();
}
}
finished = true;
}
catch
{
finished = false;
}
}Just an ideea ;)
There are 10 kinds of people: those who understand binary and those who don't
-
foreach(DataRow dr in dataset.Tables["principal"].Rows) { if (dr["name"].ToString()==szPrincipal_name) { nPrincipal_Id = Convert.ToInt32(dr["principal_Id"].ToString()); bEncontrou = true; break; } nPrincipal_Id++; }
_labelRowDeleted: foreach (DataRow myRow in dataset.Tables["grant"].Rows) { if (myRow["principal_Id"].ToString() == nPrincipal_Id.ToString()) { myRow.Delete(); goto _labelRowDeleted; } }
Solutions are welcome. -
foreach(DataRow dr in dataset.Tables["principal"].Rows) { if (dr["name"].ToString()==szPrincipal_name) { nPrincipal_Id = Convert.ToInt32(dr["principal_Id"].ToString()); bEncontrou = true; break; } nPrincipal_Id++; }
_labelRowDeleted: foreach (DataRow myRow in dataset.Tables["grant"].Rows) { if (myRow["principal_Id"].ToString() == nPrincipal_Id.ToString()) { myRow.Delete(); goto _labelRowDeleted; } }
Solutions are welcome.Bummer8 wrote:
_labelRowDeleted: foreach (DataRow myRow in dataset.Tables["grant"].Rows) { if (myRow["principal_Id"].ToString() == nPrincipal_Id.ToString()) { myRow.Delete(); goto _labelRowDeleted; } }
do
{
bool restart = false;
foreach (DataRow myRow in dataset.Tables["grant"].Rows)
{
if (myRow["principal_Id"].ToString() == nPrincipal_Id.ToString())
{
myRow.Delete();
restart = true;
break;
}
}
}
while (restart == true);Although I'm not entirely sure why you are starting the loop over again when you are deleting a row. I would imagine the following would have the same effect and be more efficient (by traversing the rows backwards)
DataRows rows = dataset.Tables["grant"].Rows;
for(int i=rows.Length-1; i>=0; i--)
{
DataRow myRow = rows[i];
if (myRow["principal_Id"].ToString() == nPrincipal_Id.ToString())
{
myRow.Delete();
}
}
Upcoming FREE developer events: * Glasgow: db4o: An Embeddable Database Engine for Object-Oriented Environments, Mock Objects, SQL Server CLR Integration, Reporting Services ... My website
-
How about this?
bool finished = false;
while (!finished)
{
try
{
foreach (DataRow myRow in dataset.Tables["grant"].Rows)
{
if (myRow["principal_Id"].ToString() == nPrincipal_Id.ToString())
{
myRow.Delete();
throw new Exception();
}
}
finished = true;
}
catch
{
finished = false;
}
}Just an ideea ;)
There are 10 kinds of people: those who understand binary and those who don't
You should NEVER throw an exception unless it is an EXCEPTIONal case (the hint is in the name). This is plainly not an exceptional case. This is an even worse solution to the one that calls goto.
Upcoming FREE developer events: * Glasgow: db4o: An Embeddable Database Engine for Object-Oriented Environments, Mock Objects, SQL Server CLR Integration, Reporting Services ... My website
-
foreach(DataRow dr in dataset.Tables["principal"].Rows) { if (dr["name"].ToString()==szPrincipal_name) { nPrincipal_Id = Convert.ToInt32(dr["principal_Id"].ToString()); bEncontrou = true; break; } nPrincipal_Id++; }
_labelRowDeleted: foreach (DataRow myRow in dataset.Tables["grant"].Rows) { if (myRow["principal_Id"].ToString() == nPrincipal_Id.ToString()) { myRow.Delete(); goto _labelRowDeleted; } }
Solutions are welcome.Why start over every time you find a row? Just store the rows you want to delete, and do it separately.
List<DataRow> removal = new List<DataRow>();
foreach (DataRow row in dataset.Tables["grant"].Rows) {
if ((int)row["principal_Id"] == nPrincipal_Id) {
removal.Add(row)
}
}
foreach (DataRow row in removal) {
row.Delete();
}--- single minded; short sighted; long gone;
-
foreach(DataRow dr in dataset.Tables["principal"].Rows) { if (dr["name"].ToString()==szPrincipal_name) { nPrincipal_Id = Convert.ToInt32(dr["principal_Id"].ToString()); bEncontrou = true; break; } nPrincipal_Id++; }
_labelRowDeleted: foreach (DataRow myRow in dataset.Tables["grant"].Rows) { if (myRow["principal_Id"].ToString() == nPrincipal_Id.ToString()) { myRow.Delete(); goto _labelRowDeleted; } }
Solutions are welcome. -
Well, none of your solutions work. most give errors. Maybe guffa's will, if he get's his code working ( <--- what's this supposed to be).
-
Well, none of your solutions work. most give errors. Maybe guffa's will, if he get's his code working ( <--- what's this supposed to be).
Bummer8 wrote:
Maybe guffa's will, if he get's his code working
Gets HIS code working?? Who getting paid to write the code?? Hint, it's NOT Guffa... Guffa's code works, for .NET 2.0 and above. Apparently you're using .NET 1.1 or below. Colin's will work on .NET 1.1. In either case, they both will work, based on the very limited information you've given. It's up to you to apply the concepts demonstrated to your exact situation. Buddy, that is what you're getting paid to do! BTW: Your code looks like it's trying to enforce referential integrity in a database. This is something that's better left up to the database itself. And, I haven't used a Goto for any reason what-so-ever, in any language, in, what, the last 12 years...
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007