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. Other Discussions
  3. The Weird and The Wonderful
  4. Detect user in group only once

Detect user in group only once

Scheduled Pinned Locked Moved The Weird and The Wonderful
databasedata-structuresquestionworkspace
7 Posts 4 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.
  • S Offline
    S Offline
    SortaCore
    wrote on last edited by
    #1

    Good code or bad code? Runs during a form constructor (testUser is a class bool).

    public class a : Form
    {
    bool testUser;
    a()
    {
    testUser = Array.IndexOf(new string[] { "mrock", "kasante" }, Environment.UserName.ToLower()) != -1;
    // ...
    }
    }

    I think my SQL experience influenced me slightly on this one.

    L P M 3 Replies Last reply
    0
    • S SortaCore

      Good code or bad code? Runs during a form constructor (testUser is a class bool).

      public class a : Form
      {
      bool testUser;
      a()
      {
      testUser = Array.IndexOf(new string[] { "mrock", "kasante" }, Environment.UserName.ToLower()) != -1;
      // ...
      }
      }

      I think my SQL experience influenced me slightly on this one.

      L Offline
      L Offline
      Lutoslaw
      wrote on last edited by
      #2

      Looks OK for a test code. And it's easy to extend. It would be bad if it was in a released code.

      Greetings - Jacek

      S 1 Reply Last reply
      0
      • L Lutoslaw

        Looks OK for a test code. And it's easy to extend. It would be bad if it was in a released code.

        Greetings - Jacek

        S Offline
        S Offline
        SortaCore
        wrote on last edited by
        #3

        It's not in test code - it just sets a bool to determine whether features still being tested are available or not.

        L 1 Reply Last reply
        0
        • S SortaCore

          It's not in test code - it just sets a bool to determine whether features still being tested are available or not.

          L Offline
          L Offline
          Lutoslaw
          wrote on last edited by
          #4

          SortaCore wrote:

          It's not in test code (...) features still being tested

          With a "test code" I (imprecisely) meant a "code being tested" -- which is the case.

          Greetings - Jacek

          S 1 Reply Last reply
          0
          • L Lutoslaw

            SortaCore wrote:

            It's not in test code (...) features still being tested

            With a "test code" I (imprecisely) meant a "code being tested" -- which is the case.

            Greetings - Jacek

            S Offline
            S Offline
            SortaCore
            wrote on last edited by
            #5

            No, it's used in release code, not as part of testing code - it specifies whether to allow the user to use beta functionality, basically. Sorry if I wasn't clear. :(

            1 Reply Last reply
            0
            • S SortaCore

              Good code or bad code? Runs during a form constructor (testUser is a class bool).

              public class a : Form
              {
              bool testUser;
              a()
              {
              testUser = Array.IndexOf(new string[] { "mrock", "kasante" }, Environment.UserName.ToLower()) != -1;
              // ...
              }
              }

              I think my SQL experience influenced me slightly on this one.

              P Offline
              P Offline
              PIEBALDconsult
              wrote on last edited by
              #6

              As stated, it's OK. You could use a case-insensitive HashSet -- I would make it static in case the set of testers becomes large. And perhaps use the # if DEBUG directive. If you find yourself copying this code to many forms, you could put it in a library method.

              1 Reply Last reply
              0
              • S SortaCore

                Good code or bad code? Runs during a form constructor (testUser is a class bool).

                public class a : Form
                {
                bool testUser;
                a()
                {
                testUser = Array.IndexOf(new string[] { "mrock", "kasante" }, Environment.UserName.ToLower()) != -1;
                // ...
                }
                }

                I think my SQL experience influenced me slightly on this one.

                M Offline
                M Offline
                Marc Clifton
                wrote on last edited by
                #7

                SortaCore wrote:

                testUser = Array.IndexOf(new string[] { "mrock", "kasante" }, Environment.UserName.ToLower()) != -1;

                If you want to get a little tighter about it:

                testUser = Array.Exists(new [] { "a", "b" }, t => t=="a");

                First of all, you don't need string[] because the compiler can infer the array type. Second, using "Exists" makes it clearer to the reader what you're doing, especially since you're not interested in the index, you just want to know of the value exists. Lastly, the use of the predicate t => t == 'a' means you could do some fancier things if you wanted later on -- it's a more general purpose solution. (Where 'a' would actually be in your case Environment.UserName.ToLower() Marc

                Day 1: Spider Database Navigator Unit Testing Succinctly

                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