[Gtk-sharp-list] Design for Indexer for GLib.List to allow [] based addressing.

Joe Shaw joe@ximian.com
Wed, 22 Oct 2003 11:27:03 -0400


On Tue, 2003-10-21 at 16:39, Daniel Kornhauser wrote:
> The code :
> ==========
> 
> In ListBase.cs
> 
> 
> public object this[int index] { 
>     get {  
> 	IntPtr data = list.NthData (index);
> 	object ret = null;
> 	if (list.element_type != null)
> 	    {
> 		    if (list.element_type == typeof (string))
> 			    ret = Marshal.PtrToStringAnsi (data);
> 		    else if (list.element_type == typeof (int))
> 			    ret = (int) data;
> 		    else if (list.element_type.IsValueType)
> 			    ret = Marshal.PtrToStructure (data,
> 							  list.element_type);
> 		    else
> 			    ret = Activator.CreateInstance (
> 							    list.element_type,
> 							    new object[] {data});
> 	     }
> 	else if (Object.IsObject (data))
> 		ret = GLib.Object.GetObject (data, false);
> 	
> 	return ret;
> 	
>     }
> }

Since this is basically an exact copy of Current(), it might make sense
to move this code out into a private method and have Current() and the
indexer method call that.  15 lines of code saved, only one place to fix
bugs, code reuse, blah blah blah.

Joe