Standing on the shoulders of giants. RSS 2.0
Previous Page Page 3 of 11 in the Development category Next Page
# Monday, July 18, 2005

SeaMen screenshot

Mart Muller: There is a lot of talk about AJAX lately. It's not new but there are a lot of cool things that you could do with it. Some guys at Tam Tam dove into it and gave a session about it last week. One of the "demo's" they've built is "Seamen - Massive Multiplayer Battleships". A cool AJAX example I do not want to keep away from you!

[note: it’s kinda slow right now, but that’s probably due to the hosting, it’s hosted from a home-adsl connection.]

Monday, July 18, 2005 1:34:34 AM (Pacific Daylight Time, UTC-07:00)  #    Comments [0] - Trackback
Development | Fun
# Friday, July 15, 2005

Returning an ArrayList is as bad or worse as returning an object from most methods, so don’t. When you return an ArrayList, anything could be in there, so I have to try and read your code, or hope your documentation is still correct, to find out what’s in there. A much nicer and more type-safe solution is to return an Array of the objects in your ArrayList, which is only 1 line of code more.

    public ArrayList BadNumbers{
        get{
            return this.items;
        }
    }
    
    public int[] GoodNumbers{
        get{
            if( numbers == null ){
                // it's not nice to return a null reference, when
                // an Array is expected.
                // That would mess up your foreach loop badly.
                return new int[0];
            }
            
            // conversion is done here instead of at the call site
            return (int[])numbers.ToArray(typeof(int));            
        }
    }
    
    private Arraylist numbers = new ArrayList();

Friday, July 15, 2005 2:33:58 AM (Pacific Daylight Time, UTC-07:00)  #    Comments [1] - Trackback
Codesnippet | Development
# Sunday, May 15, 2005

When serializing a class with an event, all classes subscribing to that event have to be serializable aswell. Usually you don't have any control over the classes subscribing to that event, or you don't want them to be remoted in the first place.

By adding the NonSerialized attribute to the event, with the field keyword, the field that holds the delegate for the event is stored is not serialized. So those subsribers no longer need to be serializable.

// ===============================================================================
// Copyright (C) 2005 Paul van Brenk
// All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR
// FITNESS FOR A PARTICULAR PURPOSE.
// ==============================================================================
// Serializing a class with an event sample.
// ==============================================================================


using System;

namespace Paulb.CodeSnippets
{
    [Serializable]
    public class SerializableClass
    {
        public SerializableClass()
        {
            ///
        }
        
        // adding the NonSerialized attribute this way,
        // we don't serialize the field where the delegate for this event is stored,
        // that way those subscribers don't need to be serializable.
        [field: NonSerialized]
        public event EventHandler Event;
    }

    /// <summary>
    /// This class can not be serialized, since the serializable attribute is missing.
    /// </summary>
    public class NotSerializedClass{

        public NotSerializedClass(){
        
            serializable.Event +=new EventHandler(serializable_Event);
        }

        private SerializableClass serializable = new SerializableClass();

        private void serializable_Event(object sender, EventArgs e) {
            // handle event here
        }
    }
}

Some background on how events are compiled from "The C# programming language" p330:

"When compiling a field-like event, the compiler automatically creates storage to hold the delegate and created accessors for the event that add or remove event handlers to the delegate field."

This results in the pseudo code generated for the SerializableClass:

.class public auto ansi serializable beforefieldinit SerializableClass
extends object
{
.event [mscorlib]System.EventHandler Event
{

// event accessors

.addon instance void Paulb.CodeSnippets.SerializableClass::add_Event([mscorlib]System.EventHandler)
.removeon instance void Paulb.CodeSnippets.SerializableClass::add_Event([mscorlib]System.EventHandler)
}


.method public hidebysig specialname rtspecialname instance void .ctor() cil managed
{
}

// delegate storage
.field private [mscorlib]System.EventHandler Event

}


disclaimer: Use at your own risk. This code is not threatsafe. Bugs, omissions let me know.

SerializingSample.cs (1.42 KB)
Sunday, May 15, 2005 7:19:56 AM (Pacific Daylight Time, UTC-07:00)  #    Comments [0] - Trackback
Codesnippet | Development

I decided to add a new category, Codesnipper, to my blog, where I can post small classes, pieces of sample code and other usefull pieces of code. That way I'll be able to find it again and maybe it's usefull to someone else aswell. So without further ado: the Most Recently Used Hashtable.

Hashtable with a fixed capacity; removing the last one touched when inserting a new entry once the capacity has been reached. Returns null for items older then the maximum age.

// ===============================================================================
// Copyright (C) 2005 Paul van Brenk
// All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR
// FITNESS FOR A PARTICULAR PURPOSE.
// ==============================================================================
// Hashtable with a fixed capacity; removing the last one touched when inserting a
// new entry once the capacity has been reached.
// Returns null for items older then the maximum age.
// ==============================================================================

using System;
using System.Collections;

namespace PaulB.Collections {

    /// <summary>
    /// CustomHashtable with a fixad capacity removing the oldest item first.
    /// </summary>
    public class MRUHashtable : IEnumerable {
    
        /// <summary>
        ///
        /// note: no max. caching time.
        /// </summary>
        /// <param name="capacity">number of items</param>
        public MRUHashtable( int capacity ) : this( capacity, TimeSpan.MaxValue ){
            //...    
        }

        /// <summary>
        ///
        /// </summary>
        /// <param name="capacity">number of items</param>
        /// <param name="maxAge">time to cache items in minutes (0 means no max. caching time)</param>
        public MRUHashtable( int capacity, int maxAge ) : this ( capacity, new TimeSpan(0, maxAge, 0) ){
            ///....
        }

        /// <summary>
        ///
        /// </summary>
        /// <param name="capacity">number of items</param>
        /// <param name="maxAge">time to cache items</param>
        public MRUHashtable( int capacity, TimeSpan maxAge ){
            this.itemTable = new Hashtable( capacity );
            this.timeTable = new Hashtable( capacity );
            this.capacity = capacity;
            this.maxAge = maxAge;
        }

        public void Add( object key, object value ){
            
            if( this.Count >= this.capacity ){
                this.RemoveOldestItem();        
            }

            this.timeTable.Add(key, DateTime.UtcNow);
            this.itemTable.Add(key, value);
        }

        public void Clear(){
            this.itemTable.Clear();
            this.timeTable.Clear();
        }

        private void RemoveOldestItem(){
            object item = null;
            DateTime lastAccess = DateTime.MinValue;
            foreach( object key in this.itemTable.Keys ){
                if( item != null && (DateTime)this.timeTable[key] > lastAccess ){
                    continue;
                }
                item = this.itemTable[key];
                lastAccess = (DateTime)this.timeTable[key];
            }
            this.Remove(item);
        }

        public IEnumerator GetEnumerator(){
            return itemTable.GetEnumerator();
        }

        public void Remove(object key){
            this.timeTable.Remove(key);
            this.itemTable.Remove(key);
        }

        public bool Contains( object key ){
            
            CheckAccessTime(key);
            this.UpdateAccessTime(key);
            return this.itemTable.Contains(key);
        }

        private void UpdateAccessTime(object key){
            if( this.timeTable[key] != null ){
                this.timeTable[key] = DateTime.UtcNow;
            }
        }

        private void CheckAccessTime(object key){
         //prevent stale information
            if( timeTable[key] == null || (DateTime.UtcNow - (DateTime)timeTable[key]) > maxAge ){
                this.Remove(key);
            }
        }

        public object this[object key]{
            get{
                CheckAccessTime(key);
                UpdateAccessTime(key);
                return this.itemTable[key];
            }
            set{
                if( this.itemTable[key] == null ){
                    this.timeTable.Add(key, value);
                }else{
                    this.UpdateAccessTime(key);
                }
                this.itemTable[key] = value;
            }
        }

        public int Count{
            get{
                return this.itemTable.Count;
            }
        }

        public object SyncRoot{
            get{
                return this.itemTable.SyncRoot;
            }
        }

        public ICollection Values{
            get{
                return this.itemTable.Values;
            }
        }

        private int capacity;
        private TimeSpan maxAge;
        private Hashtable itemTable;
        private Hashtable timeTable;
    }
}

disclaimer: Use at your own risk. This code is not threatsafe. Bugs, omissions let me know.

MRUHashtable.cs (3.85 KB)
Sunday, May 15, 2005 6:04:54 AM (Pacific Daylight Time, UTC-07:00)  #    Comments [0] - Trackback
Codesnippet | Development
# Sunday, February 13, 2005
luke nyswonger posts an incredible amount of powertoys for biz talk server 2004 collected by Erik Leaseburg.
Sunday, February 13, 2005 10:20:04 PM (Pacific Standard Time, UTC-08:00)  #    Comments [0] - Trackback
Development
# Wednesday, December 01, 2004

The Netherlands Forensic Institute recently released TULP2G as opensource. This is an application, written in C#, to extract data from mobile phones, using a data cable.

Wednesday, December 01, 2004 3:23:26 PM (Pacific Standard Time, UTC-08:00)  #    Comments [0] - Trackback
Development
# Wednesday, October 20, 2004

Instead of writing my own reports on DevCon 5, I'm just going to point you to Rory's blog, since he's doing an excellent job in not only reporting on the talks, but on the facilities as well

Wednesday, October 20, 2004 11:28:03 AM (Pacific Daylight Time, UTC-07:00)  #    Comments [0] - Trackback
Development
# Tuesday, September 07, 2004
Matt Pietrek posts a link to a very nice trick to start a different executable than the original program, the "Image File Execution Options" trick.
Tuesday, September 07, 2004 9:02:42 AM (Pacific Daylight Time, UTC-07:00)  #    Comments [0] - Trackback
Development
# Thursday, September 02, 2004

Some changes in roles, no more program- and product manager and more integration with VS Team System.

MSF Agile

Thursday, September 02, 2004 9:38:59 AM (Pacific Daylight Time, UTC-07:00)  #    Comments [0] - Trackback
Development
# Tuesday, August 17, 2004
Tuesday, August 17, 2004 2:28:02 PM (Pacific Daylight Time, UTC-07:00)  #    Comments [0] - Trackback
Development
Ads
About
© Copyright 2012
Paul van Brenk
Sign In
newtelligence dasBlog 2.3.12105.0
All Content © 2012, Paul van Brenk
DasBlog theme 'Business' created by Christoph De Baene (delarou)