Category: Uncategorized

  • ?? operator

    I just discovered a new Operator in .Net 2.0…

    the ?? operator.

    One of the most anoing things to do when retrieving data, is checking for null values:

    string myValue = dataReader.IsDBNull(2) ? string.Empty : dataReader.GetString(2);

    But the ?? operator makes it a lot easier:

    string myValue = dataReader.GetString(2)??string.Empty;

    Why didn’t anybody tell me about this before?????

    Read more here : http://msdn2.microsoft.com/en-us/library/ms173224(VS.80).aspx

  • Sorting a List object using different sort criterias

    I have often had a list of objects that i need to sort. This can easyly be done through a SortedList, but this will only sort on the provided key. I would like to descide if i want to sort by my object.Property1 or object.Property2, ascending or descending etc. Also the SortedList give me problems if i have multiple objects with the same sort-ordinal.

    The List<> type has a method, List<>.Sort(…) that can help out on this. In the example below, I have implemented a Person class, a PersonCollection : List and a PersonComparer : IComparer class. The PersonComparer has a constructor that takes a sortorder and a sortdirection as parameters. Calling the mypersonlist.Sort(…) method takes this PersonComparer object and carries out the actual sorting:

    1
    <br />using System;<br />using System.Collections.Generic;<br />using System.Text;<br />namespace SortingExample<br />{<br />        class Program<br />        {<br />                static void Main(string[] args)<br />                {<br />                        // Create a friendscollection<br />                        PersonCollection friends = new PersonCollection();<br />                        friends.Add(new Person("Kaj", 32));<br />                        friends.Add(new Person("Frank", 33));<br />                        friends.Add(new Person("Morten", 42));<br />                        Console.WriteLine("Age, Asc");<br />                        // Sort by age asc<br />                        PersonComparer sorter = new PersonComparer(PersonSort.Age, SortDirection.Ascending);<br />                        friends.Sort(sorter);<br />                        PrintNames(friends);<br />                        Console.WriteLine("Age, Desc");<br />                        // Sort by age desc<br />                        sorter.SortDirection = SortDirection.Descending;<br />                        friends.Sort(sorter);<br />                        PrintNames(friends);<br />                        Console.WriteLine("Name, Asc");<br />                        // Sort by name asc<br />                        sorter.SortDirection = SortDirection.Ascending;<br />                        sorter.Sort = PersonSort.Name;<br />                        friends.Sort(sorter);<br />                        PrintNames(friends);<br />                        Console.WriteLine("Name, Desc");<br />                        // Sort by name desc<br />                        sorter.SortDirection = SortDirection.Descending;<br />                        friends.Sort(sorter);<br />                        PrintNames(friends);<br />                }<br />


    1
                    /// <summary><br />                /// Printing out the persons of a PersonCollection<br />                /// </summary><br />                /// <param name="persons">A list of persons.</param><br />                public static void PrintNames(PersonCollection persons)<br />                {<br />                        foreach (Person friend in persons)<br />                        {<br />                                Console.WriteLine(string.Format("{0} – {1} years", friend.Name, friend.Age));<br />                        }<br />                }<br />        }


    1
    <br />        /// <summary><br />        /// The personobject will hold info about a person.<br />        /// </summary><br />        public class Person<br />        {<br />                private string _name;<br />                private int _age;<br />               <br />                /// <summary><br />                /// The name of the person.<br />                /// </summary><br />                public string Name<br />                {<br />                        get { return _name; }<br />                        set { _name = value; }<br />                }<br /><br />                /// <summary><br />                /// The age of the person.<br />                /// </summary><br />                public int Age<br />                {<br />                        get { return _age; }<br />                        set { _age = value; }<br />                }<br /><br />                /// <summary><br />                /// Initialize the person object with a name and an age.<br />                /// </summary><br />                /// <param name="name">The name of the person.</param><br />                /// <param name="age">The age of the person.</param><br />                public Person(string name, int age)<br />                {<br />                        Name = name;<br />                        Age = age;<br />                }<br />        }<br /><br />        /// <summary><br />        /// A list of persons…<br />        /// </summary><br />        public class PersonCollection : List<person><br />        {<br />        }<br /><br />        /// <summary><br />        /// The different sort orders<br />        /// </summary><br />        public enum PersonSort<br />        {<br />                Name,<br />                Age<br />        }<br /><br />        /// <summary><br />        /// Sort direction<br />        /// </summary><br />        /// <remarks>The emum values are used for multiplying on a compare result.<br />        /// This makes descending sorts appear in reverse order.<br />        /// </remarks><br />        public enum SortDirection<br />        {<br />                Ascending = 1,<br />                Descending = -1<br />        }<br /><br />        /// <summary><br />        /// This class takes care of comparing two persons. The<br />        /// class uses sorting and sort direction to determine which<br />        /// of the two objects has the highest value.<br />        /// </summary><br />        public class PersonComparer : IComparer<person><br />        {<br />                private PersonSort _sort;<br />                private SortDirection _sortDirection;<br />                public PersonSort Sort<br />                {<br />                        get { return this._sort; }<br />                        set { this._sort = value; }<br />                }<br />                public SortDirection SortDirection<br />                {<br />                        get { return this._sortDirection; }<br />                        set { this._sortDirection = value; }<br />                }<br /><br />                public PersonComparer(PersonSort sort, SortDirection direction)<br />                {<br />                        this._sort = sort;<br />                        this._sortDirection = direction;<br />                }<br />                #region IComparer<person> Members<br />                public int Compare(Person x, Person y)<br />                {<br />                        if (_sort == PersonSort.Age)<br />                        {<br />                        return x.Age.CompareTo(y.Age)*(int)_sortDirection;<br />                        }<br />                        else<br />                        {<br />                        return x.Name.CompareTo(y.Name)*(int)_sortDirection;<br />                        }<br />                }<br />                #endregion<br />        }<br />}<br />

  • How to avoid Code Analysis CA1002

    I recently made some code like this:
    public class MenuItem
    {
    private List _subItems;

    public List SubItems
    {
    get { return _subItems;}
    }

    … …
    }

    This caused Code Analysis to throw a CA1002 warning – Do not expose generic lists.
    (see ms help: http://msdn2.microsoft.com/en-us/library/ms182142(VS.90).aspx)

    The help didn’t help much, but i solved it pretty simply by making a class that inherits the List generic class:

    public class MenuItemCollection : List
    {
    }

    public class MenuItem
    {
    private MenuItemCollection _subItems;

    public MenuItemCollection SubItems
    {
    get { return _subItems;}
    }

    … …
    }

    This will avoid the CA1002 warning. Remember to name the collection class “Collection” in the end. Otherwise you will get a CA1710 warning, Identifies should have correct suffix. (http://msdn2.microsoft.com/en-us/library/ms182244(VS.90).aspx)

  • Export of Sharepoint Profile properties

    I have to export the properties from on Sharepoint environment to another (from development to production).
    In my development environment, i create the properties i need directly in the Sharepoint administration interface. When everything is approved, i export the properties directly in the SQL server. This is done by copying part of the contents from Profile database on the development environment to the production environment. The informations about the profiles are stored in these tables:

    • PropertyList
    • ProfileUI
    • DataServicePropMap

    The propertyList table contains the actual definitions of the properties. The profileUI contains information about the presentation of the property, both in edit and presentationmode. The DataServicePropMap is used to map AD fields to the values of the properties.

    Note that the values of the users isn’t exported by using this method.