locked
Compare based on type? RRS feed

  • Question

  •  

    I am trying to compare some xsd rules to the actual values for some sort of validation. I know that what is in the schema will always be a string ("0", "5/27/1957", "2.4", etc.) but the type of what I am comparing to will vary. The object may be of type Int32, DateTime, Decimal, or some other value type. The issue is that I would like to compare the string to this type. I know for instance I can do DateTime.Compare(a, b) but I would need do some sort of switch statement on the type in order to do the comparison. I have the value of an object as an object, the type, and I will always be comparing to string. Ideally I would like to have some uber compare function that takes the object, the type, and the string and it always does the right thing. I can convert the type of the string the the type of the object but how do I compare two value type objects?

     

    Any suggestions?

     

    Thank you.

     

    Kevin

    Monday, December 3, 2007 11:35 PM

Answers

  • If I were you, I'd use the generic Comparer class to do this. You can actually convert most types from a string to the correct type. Here is a function that you can use to do what I think you're trying to do. It will convert the operands to the correct type for you (if necessary) and then compare them. All you have to do is specify the type that you're comparing/converting to.



    Code Block

            int Compare<T>(object x, object y)
            {
                if (!(x is T))
                    x = Convert.ChangeType(x, typeof(T));
                if (!(y is T))
                    y = Convert.ChangeType(y, typeof(T));

                return Comparer<T>.Default.Compare((T)x, (T)y);
            }


    Tuesday, December 4, 2007 12:20 AM

All replies

  • If I were you, I'd use the generic Comparer class to do this. You can actually convert most types from a string to the correct type. Here is a function that you can use to do what I think you're trying to do. It will convert the operands to the correct type for you (if necessary) and then compare them. All you have to do is specify the type that you're comparing/converting to.



    Code Block

            int Compare<T>(object x, object y)
            {
                if (!(x is T))
                    x = Convert.ChangeType(x, typeof(T));
                if (!(y is T))
                    y = Convert.ChangeType(y, typeof(T));

                return Comparer<T>.Default.Compare((T)x, (T)y);
            }


    Tuesday, December 4, 2007 12:20 AM
  • Thank you for your contribution. Your time is greatly appreciated.

     

    Unless I have missuderstood something I would need to have a switch statement for the different types that I would be expecting. This is certainly better than the solution that I came up with but I was hoping for something even better but maybe this is as good as it gets. I am thinking I would have something like (pseudo code) where y is the string:

     

    switch(type)

    {

        case decimal:

              Compare<decimal>(x, y);

              break;

        case int:

               Compare<int>(x,y);

               break;

        case DateTime:

                Compare<DateTime>(x,y);

    . . . ..

    }

     

    Right?

    Tuesday, December 4, 2007 9:43 AM
  • Yes, if there was a way to dynamically use generics w/o switches (that I knew of), then you wouldn't have to.
    Tuesday, December 4, 2007 12:36 PM