Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8274651
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T07:47:18+00:00 2026-06-08T07:47:18+00:00

Synopsis: I have a need to take two generic C# objects, and if they

  • 0

Synopsis: I have a need to take two generic C# objects, and if they are numerical, compare them using either less than or greater than comparisons.

Problem: I can’t figure out how to have my class implement IComparable as described in this post: Having to implement a generic less than and greater than operation. If that’s not even the proper path to take, then I need to know that as well.

Background: I have implemented the RequiredIf ValidationAttribute found at A more complex custom validator but needed the > and < options in addition to an equals comparison.

Code (taken from A more complex custom validator, one-third down the page):

private bool IsRequired(object actualPropertyValue)
{
  switch (Comparison)
  {
    case Comparison.IsLessThan:
    case Comparison.IsLessThanOrEqualTo:
    case Comparison.IsGreaterThan:
    case Comparison.IsGreaterThanOrEqualTo:
      if (!Value.IsNumber())
      {
        throw new Exception("The selected comparison option is only applicable to numeric values");
      }
      break;
  }

  switch (Comparison)
  {
    case Comparison.IsNotEqualTo:
      return actualPropertyValue == null || !actualPropertyValue.Equals(Value);
    case Comparison.IsEqualTo:
      return actualPropertyValue != null && actualPropertyValue.Equals(Value);
    case Comparison.IsGreaterThan:
      // THIS LINE FAILS BECAUSE actualPropertyValue DOESN'T IMPLEMENT IComparable 
      return actualPropertyValue != null && (actualPropertyValue.CompareTo(Value) > 0); 
    // The rest of the comparison cases go here...
    default:
      throw new Exception("Comparison value is not defined");
  }
}

Static Helper Extensions:

public static bool IsNumber(this object value)
{
  if (value is sbyte) return true;
  if (value is byte) return true;
  if (value is short) return true;
  if (value is ushort) return true;
  if (value is int) return true;
  if (value is uint) return true;
  if (value is long) return true;
  if (value is ulong) return true;
  if (value is float) return true;
  if (value is double) return true;
  if (value is decimal) return true;
  return false;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T07:47:19+00:00Added an answer on June 8, 2026 at 7:47 am

    It sounds like you should just be able to cast actualPropertyValue to IComparable:

    IComparable comparable = (IComparable) actualPropertyValue;
    return comparable != null && comparable.CompareTo(Value) > 0;
    

    Note that your use of the word “generic” is curious here. If you actually made it generic, you could write:

    private bool IsRequired<T>(T actualPropertyValue) where T : IComparable
    

    Then you wouldn’t need the cast.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For example, I have this class: using System; using System.Collections.Generic; using System.Linq; using System.Text;
I am creating an XML file using the System.Xml.Serialization module. I have a class
A brief synopsis of the situation: I have a service that takes information in
Ok after my previous post, i have made some progress, yet still i need
Simple synopsis: I have a program which needs authentication from user to get access
I have an sql statement as follow: SELECT s.sp_id,s.synopsis,s.logline,s.user_id,s.bin_status,s.sp_order,s.sp_down_count,s.sp_name, `users`.firstname as `firstname`,`users`.lastname as `lastname`,s.status,
I don't grok Perl subroutine attributes at all. I have never seen them in
TL;DR (Short synopsis): I have recreated the admin Add button in my own project.
Synopsis I have a wrapper, width 980px with padding 10x. The content inside is
I have following pod which I used with getopt::long: =head1 SYNOPSIS foo [OPTION]... [URL]...

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.