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. Design question

Design question

Scheduled Pinned Locked Moved C#
questiondesignbeta-testingcode-review
8 Posts 5 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
    Stefan Troschuetz
    wrote on last edited by
    #1

    Hi everybody! I'm currently working on a class that has some public properties of type double. For some of them i have to restrict the set accessor, so that only values greater 0 can be assigned. Currently I'm ignoring an assigned value that is smaller 0

    public double Alpha
    {
    set
    {
    if (value > 0)
    this.alpha = value;
    }
    }

    but I'm not really happy with this solution, cause the user has no feedback if the value specified by him is really assigned. Is it good practice to add an else-block and throw an ArgumentOutOfRangeException there? If so, should i offer some method to check if a double value would be valid for alpha or some static min and max fields? Thanks in advance!


    www.troschuetz.de

    V S W 3 Replies Last reply
    0
    • S Stefan Troschuetz

      Hi everybody! I'm currently working on a class that has some public properties of type double. For some of them i have to restrict the set accessor, so that only values greater 0 can be assigned. Currently I'm ignoring an assigned value that is smaller 0

      public double Alpha
      {
      set
      {
      if (value > 0)
      this.alpha = value;
      }
      }

      but I'm not really happy with this solution, cause the user has no feedback if the value specified by him is really assigned. Is it good practice to add an else-block and throw an ArgumentOutOfRangeException there? If so, should i offer some method to check if a double value would be valid for alpha or some static min and max fields? Thanks in advance!


      www.troschuetz.de

      V Offline
      V Offline
      ventomito
      wrote on last edited by
      #2

      You can add a bool variable which indicate if the value has been assigned. Like this: public double Alpha { set { if (value > 0){ assigned = true; this.alpha = value; } } } But im not sure i got your question! I hope it helps.. VentoEngine corp. Program your life ^^

      1 Reply Last reply
      0
      • S Stefan Troschuetz

        Hi everybody! I'm currently working on a class that has some public properties of type double. For some of them i have to restrict the set accessor, so that only values greater 0 can be assigned. Currently I'm ignoring an assigned value that is smaller 0

        public double Alpha
        {
        set
        {
        if (value > 0)
        this.alpha = value;
        }
        }

        but I'm not really happy with this solution, cause the user has no feedback if the value specified by him is really assigned. Is it good practice to add an else-block and throw an ArgumentOutOfRangeException there? If so, should i offer some method to check if a double value would be valid for alpha or some static min and max fields? Thanks in advance!


        www.troschuetz.de

        S Offline
        S Offline
        Stan Shannon
        wrote on last edited by
        #3

        This is a design decision that depends on many factors. However, generally speaking, if it were me and this variable was exposed to the user via the UI, I would put my "feedback" mechanism directly into the UI rather than putting it into the class itself. That is, if the user defined this value in a textbox, for example, I would write the textbox in such a way that it simply would not allow the user to enter an incorrect value. I would then put something into the help file to explain the behavior. I think that is better than cluttering up your classes with a lot of exception crap and the UI with pop up warning boxes etc.

        S 1 Reply Last reply
        0
        • S Stefan Troschuetz

          Hi everybody! I'm currently working on a class that has some public properties of type double. For some of them i have to restrict the set accessor, so that only values greater 0 can be assigned. Currently I'm ignoring an assigned value that is smaller 0

          public double Alpha
          {
          set
          {
          if (value > 0)
          this.alpha = value;
          }
          }

          but I'm not really happy with this solution, cause the user has no feedback if the value specified by him is really assigned. Is it good practice to add an else-block and throw an ArgumentOutOfRangeException there? If so, should i offer some method to check if a double value would be valid for alpha or some static min and max fields? Thanks in advance!


          www.troschuetz.de

          W Offline
          W Offline
          WillemM
          wrote on last edited by
          #4

          One solution indeed could be to throw an exception when an invalid value is entered. But you should capture this error in the user interface before the data is entered into the model you are using. The exception is only for the other developers that use your model in their applications. WM.
          What about weapons of mass-construction?

          S 1 Reply Last reply
          0
          • S Stan Shannon

            This is a design decision that depends on many factors. However, generally speaking, if it were me and this variable was exposed to the user via the UI, I would put my "feedback" mechanism directly into the UI rather than putting it into the class itself. That is, if the user defined this value in a textbox, for example, I would write the textbox in such a way that it simply would not allow the user to enter an incorrect value. I would then put something into the help file to explain the behavior. I think that is better than cluttering up your classes with a lot of exception crap and the UI with pop up warning boxes etc.

            S Offline
            S Offline
            Stefan Troschuetz
            wrote on last edited by
            #5

            At first thanks for the answer. The class I'm writing is part of a class library, so the variable may be exposed to the user via GUI but didn't necessarily have to. So I'm searching for a way, to let an user of the class check for validity of an assigned value no matter if it happens directly in code or via GUI. Currently I favor the following solution:

            public double Alpha
            {
            set
            {
            if (value <= 0)
            throw new ArgumentOutOfRangeException("Assigned value has to be greater 0");

            this.alpha = value;
            

            }
            }
            public bool IsValidAlpha(double value)
            {
            return value > 0;
            }
            }

            //User of the class then can do
            if (obj.IsValidAlpha(1.0))
            obj.Alpha = 1.0;
            else
            ...


            www.troschuetz.de -- modified at 5:41 Thursday 1st December, 2005

            1 Reply Last reply
            0
            • W WillemM

              One solution indeed could be to throw an exception when an invalid value is entered. But you should capture this error in the user interface before the data is entered into the model you are using. The exception is only for the other developers that use your model in their applications. WM.
              What about weapons of mass-construction?

              S Offline
              S Offline
              Stefan Troschuetz
              wrote on last edited by
              #6

              At first thanks for the answer. The class I'm writing is part of a class library, so I don't write a GUI myself. I'm searching for a way, to let an user of the class check for validity of an assigned value no matter if it happens directly in code or via GUI. Currently I favor the following solution:

              public double Alpha
              {
              set
              {
              if (value <= 0)
              throw new ArgumentOutOfRangeException("Assigned value has to be greater 0");

              this.alpha = value;
              

              }
              }
              public bool IsValidAlpha(double value)
              {
              return value > 0;
              }
              }

              //User of the class then can do
              if (obj.IsValidAlpha(1.0))
              obj.Alpha = 1.0;
              else
              ...


              www.troschuetz.de

              D 1 Reply Last reply
              0
              • S Stefan Troschuetz

                At first thanks for the answer. The class I'm writing is part of a class library, so I don't write a GUI myself. I'm searching for a way, to let an user of the class check for validity of an assigned value no matter if it happens directly in code or via GUI. Currently I favor the following solution:

                public double Alpha
                {
                set
                {
                if (value <= 0)
                throw new ArgumentOutOfRangeException("Assigned value has to be greater 0");

                this.alpha = value;
                

                }
                }
                public bool IsValidAlpha(double value)
                {
                return value > 0;
                }
                }

                //User of the class then can do
                if (obj.IsValidAlpha(1.0))
                obj.Alpha = 1.0;
                else
                ...


                www.troschuetz.de

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

                You should be calling IsValidAlpha from within the setter instead of duplicating the code.

                S 1 Reply Last reply
                0
                • D Dan Neely

                  You should be calling IsValidAlpha from within the setter instead of duplicating the code.

                  S Offline
                  S Offline
                  Stefan Troschuetz
                  wrote on last edited by
                  #8

                  You're right. Thanks for the advice :)


                  www.troschuetz.de

                  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