At least one object must implement IComparable


At least one object must implement IComparable
First of all, if there is any one doesn’t like this kind of emails, please feel free to say so in a private email , and I will not send again to him/her any more J

now, under the namespace System.Collections.Generic, there is more than one class that I love, and really I mean it, I love them all, one of them is SortedDictionary class.

The SortedDictionary is a Key/Value pairs sorted on the key, the key and the value can be any object type.
I created a simple class and I want its objects to be added in to a SortedDictionary object as a key.
/// my class
class MyClass
    {
        private string _myClassName;

        public string MyClassName
        {
            get { return _myClassName; }
            private set { _myClassName = value; }
        }

        public MyClass(string className)
        {
            MyClassName = className;
        }
    }

 /// adding my class to the SortedDictionary Object
MyClass mc = new MyClass("myClassName");
MyClass anotherMc = new MyClass("myClassNameAgain");

SortedDictionary<MyClass, string> SD = new SortedDictionary<MyClass, string>();
SD.Add(mc, mc.MyClassName);
SD.Add(anotherMc, anotherMc.MyClassName);

The first item will be added smooth, there is no complain from the compiler, the second Item the compiler will rise an argument exception “At least one object must implement IComparable”
First we have to understand why this exception is raised?
When we added the first item “mc” there was no other items into the SortedDictionary object, so the SortedDictionary object does not need to sort anything yet. But when we try to add the next item “anotherMc” , the SortedDictionary object try to sort them, actually it try to compare between them.
But I didn’t provide any method to compare between 2 objects from my class.
So the compiler asked me to implement IComparable Interface.
My class after implementing IComparable Interface

  class MyClass : IComparable<MyClass>
    {
        private string _myClassName;

        public string MyClassName
        {
            get { return _myClassName; }
            private set { _myClassName = value; }
        }

        public MyClass(string className)
        {
            MyClassName = className;
        }



        #region IComparable<MyClass> Members

        public int CompareTo(MyClass other)
        {
            if (Equals(other))
                return 0;
            else
                return MyClassName.CompareTo(other.MyClassName);
        }

        #endregion
    }

No I will define the equality if the same object has been added twice
or the name of the first object MyClassName are the same as the second object.

now you can add your objects into the sortedDictionary collection class 

Invalid Cast Exception Between an Inherited Class and Its Base




Problem :  I want to cast my object to its base class
Parent  p  = (Child) c; // this can be done
Child c  = (Parent)  p; // this can’t be done

/// Parent class
public class Parent
    {
        public string Name { get; set; }
    }

/// Child class
public class Child : Parent
    {
        public string Extrainfo { get; set; }
    }

Well to convert between two classes you need to define  implicit operator and it must be static

So the Child class will be

///Child class
public class Child : Parent
    {
        public static implicit operator Parent(Child c)
        {
            return new Parent();
        }
    }

But the compiler will raise an error “user-defined conversions to or from a base class are not allowed”
the compiler prevent the conversions to or form a base class
L


To solve this you need to add a new constructor to your derived class (Child) and pass the parent into this constructor and by using this Key word
you can copy your information from the base class to the derived class.

so the new child object will have all the information from the base class and will also have the default values for the extra properties into the child class.


/// Child class
public class Child : Parent
    {
        public string Extrainfo { get; set; }

        public Child()
        {
        }

        public Child(Parent p)
        {
            this.Name = p.Name;
        }
}


class Program
    {
        static void Main(string[] args)
        {
            Parent p = new Parent();
            p.Name = "something";
            Child c = new Child(p);           
        }
    }

filter attribute that checks whether current connection is secured

using APS.net Core to mark all website pages working with https protocol we will do this using IAuthorizationFilter. and here is an exampl...