Thread: Variable Properties vs Public Variables

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    14

    Variable Properties vs Public Variables

    I've read many books teaching the basics of C#,
    and all of them (+ couple of ppl who told me the same thing) say that using properties of local variables instead of making methods to change private variables or just making the a public variable...

    They all said it was "better programming".

    But i want to understand why...

    lets say i've made the following class
    Code:
    namespace exampleNamespace
    {
      class exampleClass
      {
         int firstVar;
         int secondVar;
      }
    }
    now, i want to let another class initiate and change the vars' values.

    so what's the difference between this code
    Code:
    namespace exampleNamespace
    {
      class exampleClass
      {
         public int firstVar;
         public int secondVar;
      }
    }
    and this code

    Code:
    namespace exampleNamespace
    {
      class exampleClass
      {
         int firstVar;
         public int FirstVar
         {get (return firstVar)
           set (firstVar = Value)}
    
         int secondVar;
         public int SecondVar
         {get (return secondVar)
           set (secondVar = Value)}
      }
    }
    i know you can write only the get part - but it's just like making it readonly...

    and i never really understood why you have to make the local variables private when you make methods to change them....
    what's the point in that? :P

    Roy

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    4
    The purpose of public properties or methods is to hide private data, and to maintain its integrity (encapsulation). You can also assign different access levels to the get or set to make them read-only, for example if you only want the value to be changed by the class's methods. Below is an example that shows why you would want to use properties, and also makes it so the property is only set from a private scope, in the constructor in this case.

    Code:
    namespace exampleNamespace
    {
        public class exampleClass
        {
            double mAngle;
            public double Angle
            {
                get
                {
                    return mAngle;
                }
                private set
                {
                    value = value % 360;
                    if (value < 0)
                        value += 360;
                    mAngle = value;
                }
            }
    
            public exampleClass(double angle)
            {
                Angle = angle;
            }
        }
    }
    The reason you would want to make local variables private is so they're not modified when you don't want them to be. That is, they should only be modified by the classes methods or properties to ensure they're not corrupted. It is syntactically correct to make everything public, but in most cases it's a bad idea. The only time it would be smart is for very simple classes where any value is valid and it doesn't matter who changes it.

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    14
    Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. Swap two variables with out third variable
    By nkanthikiran in forum C Programming
    Replies: 3
    Last Post: 01-30-2005, 01:33 PM
  3. Replies: 5
    Last Post: 02-09-2003, 10:03 AM
  4. Variable Allocation in a simple operating system
    By awkeller in forum C Programming
    Replies: 1
    Last Post: 12-08-2001, 02:26 PM
  5. Exporting Object Hierarchies from a DLL
    By andy668 in forum C++ Programming
    Replies: 0
    Last Post: 10-20-2001, 01:26 PM