Sunday, March 27, 2005

I recently got caught out when writing some operator overloading code. I had originally checked if either of the objects were null in order to prevent a null reference exception not realising that the check itself was actually creating an infinite loop. Doh! Below is the correct code skeleton.

public static bool operator == (ObjectType c1,ObjectType c2)
{
    if( !(c1 is ObjectType) )
    {
        return !(c2 is ObjectType);
    }

    return c1.Equals(c2);
}

public static bool operator != (ObjectType c1, ObjectType c2)
{
    if( !(c1 is ObjectType) )
    {
        return (c2 is ObjectType);
    }

    return !c1.Equals(c2);
}


public override bool Equals(object obj)
{
    if(obj is ObjectType)
    {
        // do compare logic here
        return ((ObjectType)obj).Value == this.Value;
    }
    else
    {
        return false;
    }
}

Sunday, March 27, 2005 12:04:31 PM (GMT Daylight Time, UTC+01:00)  #    Comments [1]Trackback
 Friday, March 25, 2005

When working in ASP.Net there is often the need to store an object between page postbacks which is accomplished using ViewState. Sometimes the object happens to be a property of the subclassed Page or UserControl and I’ve noticed that a lot of people use this below technique.

Bad:

public ObjectType PropertyName
{
    get
    {
        ObjectType obj = this.ViewState["PropertyName"] as ObjectType;
        
        if( obj == null )
        {
            obj = new GetFromDataBase(); // or = new ObjectType();
        }
        
        return obj;
    }
    set
    {
        this.ViewState["PropertyName"] = value;
    }
}

Problems:

Every time the property is accessed you either load from ViewState and cast or save to ViewState. (performance hit)

If GetFromDataBase() returns null you will be accessing the database on every get. (performance hit)

If you instead instantise the object the property can never be null. (bad logic)

 


 

Good:

ObjectType _PropertyName;

private void Page_Load(object sender, System.EventArgs e)
{
    if (!this.IsPostBack)
    {
        _PropertyName = GetFromDataBase(); // or = new ObjectType();
    }
}


public ObjectType PropertyName
{
    get
    {
        return _PropertyName;
    }
    set
    {
        _PropertyName = value;
    }
}



protected override object SaveViewState()
{
    this.ViewState["PropertyName"] = _PropertyName;

    return base.SaveViewState ();
}

protected override void LoadViewState(object savedState)
{
    base.LoadViewState (savedState);

    _PropertyName = this.ViewState["PropertyName"] as ObjectType;
}

 

 

Friday, March 25, 2005 11:46:55 AM (GMT Standard Time, UTC+00:00)  #    Comments [1]Trackback

Not the best of introductions to the blogging world - I had just finished my first post and clicked on the submit button only to get this message "An error has been encountered while processing the page. We have logged the error condition and are working to correct the problem. We apologize for any inconvenience. ".

Friday, March 25, 2005 11:16:58 AM (GMT Standard Time, UTC+00:00)  #    Comments [0]Trackback