The Artima Developer Community
Sponsored Link

.NET Buzz Forum
Accessors for Object Assignment

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Jonathan Crossland

Posts: 630
Nickname: jonathanc
Registered: Feb, 2004

Jonathan Crossland is a software architect for Lucid Ocean Ltd
Accessors for Object Assignment Posted: Jan 5, 2009 11:31 AM
Reply to this message Reply

This post originated from an RSS feed registered with .NET Buzz by Jonathan Crossland.
Original Post: Accessors for Object Assignment
Feed Title: Jonathan Crossland Weblog
Feed URL: http://www.jonathancrossland.com/syndication.axd
Feed Description: Design, Frameworks, Patterns and Idioms
Latest .NET Buzz Posts
Latest .NET Buzz Posts by Jonathan Crossland
Latest Posts From Jonathan Crossland Weblog

Advertisement
How much code do you write, how many bugs occur because one of your objects is null, not set by some piece of code? How many lines of code includes, if (this!=null) etc ? I have seen my fair share of null reference exceptions and you have probably too.

So with Null Reference Exceptions being a very big part of development still, we really do need a way to combat it better. One way I believe would help is by giving us the opportunity to have Accessors on an Object. Just like we have a Constructor, the Accessor, should have the same name, but contain a get and set declararion.

The main reason is to inform us, of when our object is being set. Accessors on Properties is now normal, and I thinks its about time we get Accessors on Objects, where we still do not have any events to tell us when its been replaced by another. if you write the following code, you wont know it happened as it happens. No event is fired.

[code:c#]

   myobj.Name = "Name" // you know when this happens. In the Set accessor of the property


   myobj = yourobj; //BUT how will you know this happened?

[/code]


below is how I think the syntax should be.
[code:c#]

 class Class1
    {
        public Class1() // Constructor has same name as class
        {

        }

        public Class1 // Accessor has same name as class
        {
            get
            {
                return this; //default behaviour, which we could change, 
                //to return a base type, a different instance or other
            }
            set
            {
                //If its not null, we set this, 
                //else we can do something else, throw an exception
                if (value!=null)
                    this = value;
            }
        } 
    }
[/code]


Consider, that we spend a lot of time and a lot of lines of code, writing if statements to check if objects are null or not null. Consider when you have a collection or array within your class, you often set it, initialize it to an instance in your constructor. as in
[code:c#] array = new ArrayList(). [/code]

You do this so that the object does not have a chance to be null when someone writes
[code:c#]
    myobj = new MyObj()
    myobj.Array(0); //which will fail if you have not created a new instance
[/code]

So firstly from a Null reference point of view and cleaning up null checks within our code, the Object Accessor could be helpful.

Return Base Type or Proxy at will

It can also be useful for polymorphic behaviour within abstract factories design pattern or similar uses. Allowing you to 'substitite' 'this' by saying this = someOtherInstance, would offer good flexibility.

As long as the Type we return is a base type of the Type, we can then return other classes within the hierarchy.

Extension Methods

Lastly, since we can set the this keyword, it would make Extension methods a little nicer too.

[code:c#]
    public static class XmlCategoryReader
    {

        public static void Read(this Category obj, string filename)
        {
            obj = new Category(); //WHY NOT? :)
        }
    } 
[/code]


Why Not?

The fact that the keyword 'this' may refer to a different interface is perhaps a problem, but it can surely be overcome. if Duck Typing was introduced, it may play well here, or at least this would be another extension point for it.

Read: Accessors for Object Assignment

Topic: Interface Contracts and Access Modifiers Previous Topic   Next Topic Topic: Introducing Expression Blend to Silverlight 2 Developers

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use