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 6840271
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:49:21+00:00 2026-05-26T23:49:21+00:00

I have to validate user input data and ensure a string value is convertible

  • 0

I have to validate user input data and ensure a string value is convertible to a type specified at run-time. I don’t necessarily need to do the actual conversion, just test to make sure the input value is valid. I haven’t found a built in class or method that will perform this type of evaluation, but if I am missing one, please let me know. I’m working with C#4.0, if there is any version specific solutions available.

The method only has to deal with the “standard” types (built-in value data types plus String). The only custom type I would need to evaluate is specific enum types that are defined in the library.

I have 2 solutions I’m currently weighing, but neither is perfect, so I was hoping there was a 3rd option (or something built into the framework that I missed). I am heavily leaning towards Solution #2 since using the try-catch in Solution #1 just seems wrong.

Solution 1: Convert.ChangeType() with try/catch

public Boolean CheckType(String value, Type type)
{
    try
    {
        var obj = Convert.ChangeType(value, type);
        return true;
    }
    catch(InvalidCastException)
    {
        return false;
    }
    catch(FormatException)
    {
        return false;
    }
    catch(OverflowException)
    {
        return false;
    }
    catch(ArgumentNullException)
    {
        return false;
    }
}

Solution 2 if/else chain with Type check and TryParse

public Boolean CheckType(String value, Type type)
{
    if (type == typeof(String))
    {
        return true;
    }
    else if (type == typeof(Boolean))
    {
        Boolean b;
        return Boolean.TryParse(value, out b); 
    }
    else if (type == typeof(Int32))
    {
        Int32 i;
        return Int32.TryParse(value, out i); 
    }
    else if (type == typeof(Int64))
    {
        Int64 l;
        return Int64.TryParse(value, out l); 
    }
    // similar code to check all other types 
    // (Int16, UInt32, UInt64, UInt16, Byte, SByte, Single, Double, Decimal,
    //  Enum, Char, DateTime)
    .
    .
    .
    .
    .
    else
        throw new ArgumentException("Invalid type evaluation");

}

This method may be called several hundred or even a thousand times in a short interval if the input data is seriously messed up or corrupted, so I’m worried that the repeated if/else checks will be a drag on performance (I’m not necessarily trying to optimize at this point, I just want to make sure I’m considering other options).

The other issue I have with both solutions is that both actually convert the string value to a new value of the expected type, and in both cases, I’m swallowing the result.

  • 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-26T23:49:22+00:00Added an answer on May 26, 2026 at 11:49 pm

    I found a better solution than either of my initial ideas in another question that was recently asked.

    parapura rajkumar was on the right track with the TypeConverter class, but the required exception handling for the CanConvertFrom method for non-exceptional events was what I was trying to avoid.

    The TypeConverter.IsValid method solved my problem, although it is not ideal because the IsValid method is just a wrapper for the CanConvertFrom method and the required exception handling.

    private Boolean CanCovert(String value, Type type)
    {
        TypeConverter converter = TypeDescriptor.GetConverter(type);
        return converter.IsValid(value);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have loop designed to validate the user input on a question, it was
How can I validate the user input by using scanf. Right now I have
I have several javascript functions to validate the input data in textbox, so it
i have a data validation class method where i check the user input before
I have a Win32 C++ program that validates user input and updates the UI
Say we have a form where the user types in various info. We validate
For example, if I have a user model and I need to validate login
I have the following piece of code in my DataModel.cs class: public User ValidateUser(string
I'm coding an italian website where I need to validate some input data with
Is it better to validate user input before it's sent to the server with

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.