Click here to Skip to main content
15,906,626 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.

 
GeneralRe: Property or a method. This seems wrong. Pin
Gary Wheeler21-May-14 0:28
Gary Wheeler21-May-14 0:28 
GeneralRe: Property or a method. This seems wrong. Pin
Chris Maunder21-May-14 4:15
cofounderChris Maunder21-May-14 4:15 
GeneralRe: Property or a method. This seems wrong. Pin
BobJanova21-May-14 1:11
BobJanova21-May-14 1:11 
GeneralRe: Property or a method. This seems wrong. Pin
Chris Maunder21-May-14 4:17
cofounderChris Maunder21-May-14 4:17 
GeneralRe: Property or a method. This seems wrong. Pin
Jeremy Falcon21-May-14 1:11
professionalJeremy Falcon21-May-14 1:11 
GeneralRe: Property or a method. This seems wrong. Pin
Marc Clifton21-May-14 2:05
mvaMarc Clifton21-May-14 2:05 
GeneralRe: Property or a method. This seems wrong. Pin
Chris Maunder21-May-14 4:18
cofounderChris Maunder21-May-14 4:18 
GeneralRe: Property or a method. This seems wrong. Pin
#realJSOP21-May-14 2:22
professional#realJSOP21-May-14 2:22 
I agree with the premise that properties should never throw exceptions, and should instead return a reasonable default value if an error condition presents itself. If you want to throw an exception based on a property value, you should do it from the method that is using the property.

In most circumstances, an empty get/set is all that is needed. However, there are exceptions (pun - LOL). I abhor the all-or-nothing XML serialization/deserialization mechanism built into .Net, and wrote my own extension methods that can handle faulty data that returns reasonable default values for expected fields. Yeah, it takes longer to write the code, but I have a LOT more control over what's happening. As a result, pretty much all of my data-based objects contain a property called AsXElement, and a constructor overload that accepts an XElement. If there's a problem with the expected data, no exception is thrown, and the code proceeds as expected.

C#
public class MyObject
{
    public string Text { get; set; }

    public XElement AsXElement
    {
        get
        {
            XElement value = new XElement("ElementName",
                                          new XAttribute("text", this.Text));
            return value;
        }
        set
        {
            if (value != null)
            {
                this.Text = value.GetAttribute("text", "NO DATA");
            }
        }         
    }

    public MyObject(XElement value)
    {
        this.AsXElement = value;
    }
    
}


Of course, different platforms require different approaches. WPF and Silverlight almost require code in property get/set, especially if they're bound to a control, and or the property's value is considered due to the use of the INotifyPropertyChanged interface.
".45 ACP - because shooting twice is just silly" - JSOP, 2010
-----
You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
-----
When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013


GeneralRe: Property or a method. This seems wrong. Pin
Chris Maunder21-May-14 4:20
cofounderChris Maunder21-May-14 4:20 
GeneralRe: Property or a method. This seems wrong. Pin
Gary Wheeler21-May-14 4:21
Gary Wheeler21-May-14 4:21 
GeneralRe: Property or a method. This seems wrong. Pin
Ravi Bhavnani21-May-14 2:37
professionalRavi Bhavnani21-May-14 2:37 
GeneralThis question reminds me of the anecdote Pin
Ennis Ray Lynch, Jr.21-May-14 3:49
Ennis Ray Lynch, Jr.21-May-14 3:49 
GeneralNagy & DD Pin
thatraja20-May-14 17:36
professionalthatraja20-May-14 17:36 
GeneralRe: Nagy & DD Pin
Dalek Dave20-May-14 21:46
professionalDalek Dave20-May-14 21:46 
GeneralD-Wave Quantum Computer: does true/false super-fast, but, can it do "maybe" ? Pin
BillWoodruff20-May-14 14:27
professionalBillWoodruff20-May-14 14:27 
GeneralRe: D-Wave Quantum Computer: does true/false super-fast, but, can it do "maybe" ? Pin
Marc Clifton20-May-14 15:17
mvaMarc Clifton20-May-14 15:17 
GeneralRe: D-Wave Quantum Computer: does true/false super-fast, but, can it do "maybe" ? Pin
BillWoodruff20-May-14 15:43
professionalBillWoodruff20-May-14 15:43 
GeneralRe: D-Wave Quantum Computer: does true/false super-fast, but, can it do "maybe" ? Pin
Roger Wright20-May-14 18:33
professionalRoger Wright20-May-14 18:33 
GeneralRe: D-Wave Quantum Computer: does true/false super-fast, but, can it do "maybe" ? Pin
Gary Wheeler21-May-14 4:25
Gary Wheeler21-May-14 4:25 
GeneralRe: D-Wave Quantum Computer: does true/false super-fast, but, can it do "maybe" ? Pin
Roger Wright22-May-14 17:28
professionalRoger Wright22-May-14 17:28 
GeneralRe: D-Wave Quantum Computer: does true/false super-fast, but, can it do "maybe" ? Pin
Gary Wheeler23-May-14 0:24
Gary Wheeler23-May-14 0:24 
GeneralRe: D-Wave Quantum Computer: does true/false super-fast, but, can it do "maybe" ? Pin
OriginalGriff20-May-14 20:03
mveOriginalGriff20-May-14 20:03 
GeneralRe: D-Wave Quantum Computer: does true/false super-fast, but, can it do "maybe" ? Pin
Tim Carmichael21-May-14 1:30
Tim Carmichael21-May-14 1:30 
RantCancel - OK Pin
Colin Mullikin20-May-14 8:24
professionalColin Mullikin20-May-14 8:24 
GeneralRe: Cancel - OK Pin
Pualee20-May-14 8:33
Pualee20-May-14 8:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.