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

  • Home
  • SEARCH
  • 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 4617682
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T02:09:32+00:00 2026-05-22T02:09:32+00:00

Consider the following function: public enum Operator { EQUAL = 1, GREATER_THAN = 2

  • 0

Consider the following function:

public enum Operator
{
    EQUAL = 1,
    GREATER_THAN = 2
}

public class checkString
{
    public static bool isValid(string inputString, string checkString, Operator operation)
    {
        switch (operation)
        {
            case Operator.EQUAL:
                if (inputString == checkString)
                    return true;

                break;

            case Operator.GREATER_THAN:

                // Numeric check for greater than
                try
                {
                    double inputDouble, checkDouble;

                    inputDouble = Convert.ToDouble(inputString);
                    checkDouble = Convert.ToDouble(checkString);

                    if (inputDouble > checkDouble)
                        return true;
                }
                catch (Exception) 
                { }

                // Date check for greater than
                try
                {
                    DateTime inputDate, checkDate;

                    inputDate = DateTime.Parse(inputString);
                    checkDate = DateTime.Parse(inputString);

                    if (inputDate. > checkDate)
                        return true;
                }
                catch (Exception)
                { }

                break;
        }
        return false;
    }
}

Parameters

  • inputString: What we want to evaluate
  • checkString: The criteria (value) that the input must evaluate against
  • Operator: Enum for the operation we want to perform

Other things to know

  • Each line in a file is evaluated against this method to return if a condition has been met
  • The process of evaluating the records in the file checks line by line, in one instance that its equal to the condition. It may also check that the same line is also greater than the condition. Once the checks are done, it moves to the next record
  • There are no additional event listeners hooked up other than whatever defaults are in place, I am not pushing extra data to debug or trace logs

Problem

What people are going to evaluate is unknown to me at any point in this process but I need to be able to check that ‘something’ (regardless of what) is equal to, greater than or less than something else. Sure I check other things but I’ve simplified this function greatly.

That said, using EQUAL or NOT_EQUAL runs fast as can be, processing records in a very large file against said criteria pretty quick and efficiently. Once I added the GREATER_THAN logic, its slow… to the point where it takes minutes to process 20 meg files that used to take half a minute tops.

From what I can tell:

  • Exceptions are thrown all over the place. There is no guarantee that a field is going to be numeric or of date type. So I must attempt to cast to these data types to attempt to evaluate the condition
  • When exceptions are thrown, the console gets output where I have not instructed it to do so, its sort of automated

Yes I have a lack of experience in this area and am looking to learn more about exception handling and what really happens behind the scenes because when the other 80% of the records are not numeric, thats a lot of exceptions in a 20 meg, 80 thousand record file.

Is there a better way to handle the cast itself to increase efficiency? I’ve seen double.Parse / TryParse and can direct cast in front but am not sure which benefits the most.

  • 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-05-22T02:09:33+00:00Added an answer on May 22, 2026 at 2:09 am

    You can use TryParse() on those data types. Exceptions are messy and expensive. TryParse will return true/false if it worked or not while NOT throwing an exception. So you can just check the results of the call. Much more efficient than exceptions.

    Convert.ToDouble() and Double.Parse() will throw exceptions.

    try this code. It isn’t the best, but it’s better than what you have now considering you don’t know what the type could be:

    public static bool isValid(string inputString, string checkString, Operator operation)     
            {         
                double dblTmp1;
                double dblTmp2;
    
                if (Double.TryParse(inputString, out dblTmp1) && double.TryParse(checkString, out dblTmp2))
                {
                    return Compare<Double>(dblTmp1, dblTmp1, operation);
                }
    
                DateTime dtTmp1;
                DateTime dtTmp2;
                if (DateTime.TryParse(inputString, out dtTmp1) && DateTime.TryParse(checkString, out dtTmp2))
                {
                    return Compare<DateTime>(dtTmp1, dtTmp2, operation);
                }
    
                throw new InvalidOperationException("Unknown type");
    
            }
    
            public static bool Compare<T>(T obj1, T obj2, Operator operation) where T : IComparable      
            {
                switch (operation)
                {
                    case Operator.EQUAL:
                        {
                            return obj1.Equals(obj2);
                        }
                    case Operator.GREATER_THAN:
                        {
                            return obj1.CompareTo(obj2) > 0;
                        }
                    default:
                        {
                            throw new InvalidOperationException("Unknown operation");
                        }
                }
            }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider following example : public class SomeBusinessLayerService : DataService<MyEntityContainer> { [WebInvoke] void DoSomething(string someParam)
Consider the following method that stops a service: Public Function StopService(ByVal serviceName As String,
Consider following class class test { public: test(int x){ cout<< test \n; } };
I need to report progress changed. Consider the following code: Public Class Calculator Public
Consider the following: TFieldType = class fValue: string; end; TMainClass = class private Ffield:
Consider the following function that implements non-blocking access to only the one thread. public
Consider the following piece of code: class foo { private function m() { echo
Consider the following python ctypes - c++ binding: // C++ class A { public:
Given the following declaration: <Extension()> Public Function ToJSON(ByVal target As Object) As String Dim
Consider the following code: struct Vec2 : IEquatable<Vec2> { double X,Y; public bool Equals(Vec2

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.