Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. A situation where you have to use goto

A situation where you have to use goto

Scheduled Pinned Locked Moved C#
9 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    Bummer8
    wrote on last edited by
    #1

    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.

    A J C G B 5 Replies Last reply
    0
    • B Bummer8

      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.

      A Offline
      A Offline
      Andrei Ungureanu
      wrote on last edited by
      #2

      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

      C 1 Reply Last reply
      0
      • B Bummer8

        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.

        J Offline
        J Offline
        JSSUML
        wrote on last edited by
        #3

        try this... do { foreach (DataRow myRow in dataset.Tables["grant"].Rows) { if (myRow["principal_Id"].ToString() == nPrincipal_Id.ToString()) { myRow.Delete(); continue; } } break; }while(true); ================================ Thanks();

        1 Reply Last reply
        0
        • B Bummer8

          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.

          C Offline
          C Offline
          Colin Angus Mackay
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          • A Andrei Ungureanu

            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

            C Offline
            C Offline
            Colin Angus Mackay
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            • B Bummer8

              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.

              G Offline
              G Offline
              Guffa
              wrote on last edited by
              #6

              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;

              1 Reply Last reply
              0
              • B Bummer8

                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.

                B Offline
                B Offline
                Bummer8
                wrote on last edited by
                #7

                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).

                D D 2 Replies Last reply
                0
                • B Bummer8

                  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).

                  D Offline
                  D Offline
                  Dan Neely
                  wrote on last edited by
                  #8

                  Templating. a C# 2.0 feature.

                  -- If you view money as inherently evil, I view it as my duty to assist in making you more virtuous.

                  1 Reply Last reply
                  0
                  • B Bummer8

                    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).

                    D Offline
                    D Offline
                    Dave Kreskowiak
                    wrote on last edited by
                    #9

                    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

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups