Reflection IT Blog

Benieuwd naar de laatste ontwikkelingen rondom software ontwikkeling en Reflection IT? Onze slimme koppen delen regelmatig hun kennis en ervaring. Zo weet jij wat er speelt!

2004 Blog posts

SmartPart for SharePoint

10-Jun-2004

Together with Jan Tielens, I have created a improved version of my User Control Container Web Part called the SmartPart for SharePoint.

Jan has written an introduction and even created a video demonstrating it in action.

We have created a GotDotNet Workspace for the SmartPart. Over there you can download an installation package that will install the SmartPart, the source code and an example user control. At this point there isn’t very much documentation, but I will work on that. :-) There are a lot of cool ideas which (I hope) will be added to the SmartPart (for example a connectable SmartPart). I’ve IM-ed with Patrick today and he has another cool idea which he discussed in one of his latest posts: using the User Interface Process block together with web parts. Anyway, some nice ideas are waiting to be implemented!

I will be at TechEd 2004 Amsterdam

12-May-2004

This will be my 6th TechED (97 and 98 in Nice, 99 and 2000 in Amsterdam, 2002 in Barcelona).

It is a great event in which I learn a lot and get lot's of inspiration. It's not cheap but it is worth every peny!

InitialFocus on a ASP.NET WebForm

28-Apr-2004

The PageUtil class has a static method SetInitialFocus(control) which can be used to generate a JavaScript for an ASP.NET page (WebForm), which sets the focus on a (given) control.

        private voidPage_Load(objectsender, System.EventArgs e)
        {
            // Set the InitialFocus on TextBox1
            ReflectionIT.Web.PageUtil.SetInitialFocus(TextBox1);
        }

TextBox1: 

String Array Sorting

27-Apr-2004

It took me today some time to figure out how to sort an array of strings case-sensitively. The default behavior of the Array.Sort() uses an default Comparer object which should be case-sensitive. When I tested this I found out that I had misinterpreted the definition of 'case-sensitive'. What I wanted was 'Oridinal' sorting. So I created an OrdinalStringComparer class which implements IComparer and I got what I wanted.

using System;
using System.Collections;

namespace ReflectionIT.Test {

   &nbspclass Class1{

   &nbsp   &nbsp[STAThread]
   &nbsp   &nbspstaticvoid Main(string[]args){

   &nbsp   &nbsp   &nbspTest(new CaseInsensitiveComparer());
   &nbsp   &nbsp   &nbspTest(Comparer.Default);   &nbsp   &nbsp
   &nbsp   &nbsp   &nbspTest(new OridinalStringComparer());
   &nbsp   &nbsp
   &nbsp   &nbsp   &nbspConsole.ReadLine();
   &nbsp   &nbsp}

   &nbsp   &nbspprivatestaticvoid Test(IComparer comparer){

   &nbsp   &nbsp   &nbspstring[]words=newstring[]{"c","a","A","aB","ab","Ab"};

   &nbsp   &nbsp   &nbspArray.Sort(words,comparer);
   &nbsp   &nbsp
   &nbsp   &nbsp   &nbspConsole.WriteLine(comparer.GetType().Name +":");
   &nbsp   &nbsp   &nbspforeach(stringwordinwords){
   &nbsp   &nbsp   &nbsp   &nbspConsole.WriteLine(word);
   &nbsp   &nbsp   &nbsp}
   &nbsp   &nbsp   &nbspConsole.WriteLine();
   &nbsp   &nbsp}
   &nbsp   &nbsp
   &nbsp}
   &nbsp   &nbsp
   &nbsppublicclass OridinalStringComparer : IComparer {
   &nbsp   &nbsp
   &nbsp   &nbspint IComparer.Compare(object x,object y ){
   &nbsp   &nbsp   &nbspreturnstring.CompareOrdinal((string)x,(string)y);
   &nbsp   &nbsp}
   &nbsp   &nbsp
   &nbsp}
   &nbsp   &nbsp
}

And when you run this program you get the following output:

CaseInsensitiveComparer:
A
a
Ab
ab
aB
c

Comparer:
a
A
ab
aB
Ab
c

OridinalStringComparer:
A
Ab
a
aB
ab
c

User Control Container Web Part

20-Apr-2004

SharePoint is a great product. In this article I will demonstrate this by creating a Web Part which acts like a container for normal ASP.NET User Controls (.ascx files). This makes it very easy to customize a portal page by adding normal ASP.NET User Controls to it.

System.Collections and System.Collections.Specialized

08-Mar-2004

This table gives you an overview of all collections in the System.Collections and System.Collections.Specialized namespaces.

Class Key Value Remarks
System.Array   ? Predefined size, used for passing data around.
ArrayList   Object Easy to work with, provides basic collection functionality and can easily be converted to an Array.
BitArray   Boolean
Manages a compact array of bit values, which are represented as Booleans
CollectionBase   ? Abstract base for building your own collection classes.
DictionaryBase Object ? Abstract base for building your own collection classes using a key-value pair.
HashTable Object Object Provides high performance access to items in the key-value pair collection, by a specific key.
Queue   Object Implements the FIFO mechanism.
ReadOnlyCollectionBase   Object Abstract base for building your own read-only collection classes.
SortedList Object (sorted) Object Provides access to items in the collection by a specific key or an index (GetByIndex(int)).
Stack   Object Implements the LIFO mechanism.
Specialized.HybridDictionary
Object Object Uses internally a ListDictionary class when the collection is small, and switches automatically to a Hashtable class when the collection gets large.
Specialized.ListDictionary Object Object Is designed to be used for collections with 10 or less items, while the Hashtable is designed to contain more items.
Specialized.NameObjectCollectionBase String ? Abstract base for building your own collection classes using a key-value pair.
Specialized.NameValueCollection String (sorted) String NameValueCollection class is the strong type equivalent of the SortedList class, for the String class.
Specialized.StringCollection   Object Strongly typed ArrayList for the String class.
Specialized.StringDictionary String String Strongly typed Hashtable for the String class, not sorted.

Read also Jan Tielens article on the MSDN Belux website.

EventsHelper using Fire and Forget

07-Mar-2004

I have used the EventsHelper class to fire events asynchronical. I came across this class in the TechEd 2003 presentation C# Best Practices from Eric Gunnerson and Juval Löwy. Today I noticed a bug. The FireAsync() method uses the 'Fire and Forget' pattern incorrectly.

The 'Fire and Forget' pattern is used when the return value and returned parameters of the call are not required, and when there is no need to synchronize with the asynchronously executing method. In this case, the caller simply needs to call BeginInvoke, passing any normal and ref parameters, and null for the callback and asyncState.

This has been a commonly used pattern. However, there has been a documentation change in version 1.1 of the .NET Framework that has big ramifications for this technique of making async calls. The documentation now states that EndInvoke must be called for a corresponding BeginInvoke--otherwise Microsoft say they may now, or in the future, leak resources. It appears that no resources are leaked under version 1.0 of the framework; however, with this type of warning in place, it is recommended that a call to EndInvoke be made even if the return values of an async call are not required.

It is relatively straightforward to create a helper class that handles this for you--one example I found here.

public class AsyncHelper {

   &nbspdelegatevoid DynamicInvokeShimProc(Delegate d,object[]args);

   &nbspstatic DynamicInvokeShimProc dynamicInvokeShim=new
   &nbsp   &nbspDynamicInvokeShimProc(DynamicInvokeShim);

   &nbspstatic AsyncCallback dynamicInvokeDone=new
   &nbsp   &nbspAsyncCallback(DynamicInvokeDone);

   &nbsppublicstaticvoid FireAndForget(Delegate d,paramsobject[]args){
   &nbsp   &nbspdynamicInvokeShim.BeginInvoke(d,args,dynamicInvokeDone,null);
   &nbsp}

   &nbspstaticvoid DynamicInvokeShim(Delegate d,object[]args){
   &nbsp   &nbspd.DynamicInvoke(args);
   &nbsp}

   &nbspstaticvoid DynamicInvokeDone(IAsyncResult ar){
   &nbsp   &nbspdynamicInvokeShim.EndInvoke(ar);
   &nbsp}
}

DateDiff() function in C#

11-Feb-2004 4 Comments

I have written the following DateDiff() function in C#. VB.NET users already had it using the Micrsoft.VisualBasic.dll assembly. Now you can use it without referencing this 'ugly' extra assembly.

using System;


namespace ReflectionIT.System {

   &nbsppublicenum DateInterval {
   &nbsp   &nbspYear,
   &nbsp   &nbspMonth,
   &nbsp   &nbspWeekday,
   &nbsp   &nbspDay,
   &nbsp   &nbspHour,
   &nbsp   &nbspMinute,
   &nbsp   &nbspSecond
   &nbsp}

   &nbsppublicclass DateTimeUtil {

   &nbsp   &nbsppublicstaticlong DateDiff(DateInterval interval, DateTime date1, DateTime date2){

   &nbsp   &nbsp   &nbspTimeSpan ts=date2-date1;

   &nbsp   &nbsp   &nbspswitch(interval){
   &nbsp   &nbsp   &nbsp   &nbspcase DateInterval.Year:
   &nbsp   &nbsp   &nbsp   &nbsp   &nbspreturndate2.Year -date1.Year;
   &nbsp   &nbsp   &nbsp   &nbspcase DateInterval.Month:
   &nbsp   &nbsp   &nbsp   &nbsp   &nbspreturn(date2.Month -date1.Month)+(12*(date2.Year -date1.Year));
   &nbsp   &nbsp   &nbsp   &nbspcase DateInterval.Weekday:
   &nbsp   &nbsp   &nbsp   &nbsp   &nbspreturn Fix(ts.TotalDays)/7;
   &nbsp   &nbsp   &nbsp   &nbspcase DateInterval.Day:
   &nbsp   &nbsp   &nbsp   &nbsp   &nbspreturn Fix(ts.TotalDays);
   &nbsp   &nbsp   &nbsp   &nbspcase DateInterval.Hour:
   &nbsp   &nbsp   &nbsp   &nbsp   &nbspreturn Fix(ts.TotalHours);
   &nbsp   &nbsp   &nbsp   &nbspcase DateInterval.Minute:
   &nbsp   &nbsp   &nbsp   &nbsp   &nbspreturn Fix(ts.TotalMinutes);
   &nbsp   &nbsp   &nbsp   &nbspdefault:
   &nbsp   &nbsp   &nbsp   &nbsp   &nbspreturn Fix(ts.TotalSeconds);
   &nbsp   &nbsp   &nbsp}
   &nbsp   &nbsp}

   &nbsp   &nbspprivatestaticlong Fix(double Number){
   &nbsp   &nbsp   &nbspif(Number >=0){
   &nbsp   &nbsp   &nbsp   &nbspreturn(long)Math.Floor(Number);
   &nbsp   &nbsp   &nbsp}
   &nbsp   &nbsp   &nbspreturn(long)Math.Ceiling(Number);
   &nbsp   &nbsp}
   &nbsp}
}


Get in touch

Met dit formulier kunt u informatie over een In-Company of Small-Group training aanvragen. U kunt in het bericht aangeven welke training u wilt, voor hoeveel personen, wanneer deze verzorgd moet worden en op welke locatie. Wij nemen vervolgens contact met u op.

U kunt ons ook bereiken via telefoonnummer +31 (0)493-688810 of per mail training@reflectionit.nl.