Tuesday, July 28, 2015

Get; Set;

Taking a step backwards from my normal posts to some more basics.. I wanted to explain Get; Set; on properties to anyone new to software. I felt like some of the descriptions online were a little non-user friendly and wanted to help clarify it. This is done in C#, but the concepts are the same idea with other languages.

        public class Person
        {
            public string Name { get; set; }
            // Let's investigate this one
            public string Phone { get; set; }
        }

        // This is called an auto-implemented property. What does this really mean?
        public string Phone { get; set; }

        // This is the longhand / what really happens / the manual way to do this. 
        private string phone;
        public string Phone
        { 
            get { return phone; }
            set { phone = value; }
        }

        // Now what is THIS doing? Example time..
        // Make a Person1 of type Person. 
        Person Person1 = new Person();

        // Add a value to the property of Person. 
        Person1.Phone = "123-4567"; // **#1 see below

        // phone field
        private string phone; 
        
        // phone property
        public string Phone // **#1 hits this first, causing you to enter the braces..
        { 
            get { return phone; } // #3 returns little case phone, which has been assigned to value as property Phone
            set { phone = value; } // #2 sets the magical VALUE to little case phone
        }

        // So now if we ask for..
        // thingy will be "123-4567";
        var thingy = Person1.Phone;

        // where do we get value from????
        // "The word value references the value that client code is attempting to assign to the property." - MSDN
        // In other words.. Microsoft took care of it for you.
        // You try to access and set a property, it knows to assign that to "value"

        // With this version, you can make a new property based on other properties / customization
        // not there is only a get here. Its like a read only value
        public string PhoneString
        {
            get
            {
                string phoneNumString = "";
                
                // null safe it! 
                if (Person.Phone == null) return string.Empty;

                // if there's an area code, add it on! 
                if (!string.IsNullOrWhiteSpace(Person.Phone.AreaCode))
                    phoneNumString = "(" + Person.Phone.AreaCode + ") " + Person.Phone.PhoneNumber;
                else phoneNumString = Person.Phone.PhoneNumber; // if not, no biggie. 

                // if there's an extension, add it on too! 
                if (!string.IsNullOrWhiteSpace(Person.Phone.Extension)) phoneNumString += "ext." + Person.Phone.Extension;
                    
                return phoneNumString;
            }
        }

        // So, running with the example above... here's what you can and can't do.. 
        // NOPE. Can't set this. There is only a get. 
        Person1.Phonestring = "(813)123-4567 ext. 1000"; // This will fail and throw you an error. ANGRY RED SQUIGGLYS. 

        // But! You can do this. And then... 
        Person1.AreaCode = "813";
        Person1.Phone = "123-4567";
        Person1.Extension = "1000";

        //  result = "(813)123-4567 ext. 1000"
        var result = Person1.Phonestring;