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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T13:52:15+00:00 2026-05-24T13:52:15+00:00

I am trying to verify whether a variable that is passed can be converted

  • 0

I am trying to verify whether a variable that is passed can be converted to a specific type. I have tried the following but can’t get it to compile so I assume I’m going about it the wrong way (I’m new to C#)

string myType = "System.Int32";
string myValue = "42";

bool canBeCast = false;

try
{
  // try to convert the value to it's intended type to see if it's valid.
  var result = (Type.GetType(typeString))dataValue;
  canBeCast = true;
}
catch
{
  canBeCast = false;
}

I’m basically trying to avoid a massive switch statement along the lines of

  switch(myType){
    case "System.Int32":
      try
      {
        var convertedValue = Convert.ToInt32(myValue);
      }
      catch (Exception)
      {
        canBeConverted = false;
      }
      break;
    case "another type":
      ...
  }

EDIT:

Ok, basically I have a db table of known input types that looks like:

CREATE TABLE [dbo].[MetadataTypes] (
    [typeName]  VARCHAR (50)  NOT NULL,
    [dataType]  VARCHAR (50)  NOT NULL,
    [typeRegex] VARCHAR (255) NULL
);

which may have data such as

"StartTime","System.DateTime",null
"TicketId","System.String","$[Ff][0-9]{7}^"

And the input to my function would be a KeyValuePair along the lines of

myInput = new KeyValuePair<string,string>("StartTime","31/12/2010 12:00");

I need to check that the value of the KeyValuePair is of the correct datatype expected by the MetaDataType.

EDIT FOR ANSWER:

Leon got really close to the solution I finally came up with.

For reference my function now looks like this:

public Boolean ValidateMetadata(KeyValuePair<string, string> dataItem)
{

  // Look for known metadata with name match
  MetadataType type = _repository.GetMetadataTypes().SingleOrDefault(t => t.typeName == dataItem.Key);
  if (type == null) { return false; }

  // Get the data type and try to match to the passed in data item.
  Boolean isCorrectType = false;
  string typeString = type.dataType;
  string dataValue = dataItem.Value;

  try
  {
    var cValue = Convert.ChangeType(dataValue, Type.GetType(typeString));
    isCorrectType = true;
  }
  catch
  {
    isCorrectType = false;
  }

  //TODO: Validate against possible regex here....            

  return isCorrectType;

}
  • 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-24T13:52:15+00:00Added an answer on May 24, 2026 at 1:52 pm

    Use the “as” operator to attempt a cast:

    var myObject = something as String;
    
    if (myObject != null)
    {
      // successfully cast
    }
    else
    {
      // cast failed
    }
    

    If the cast fails, no exception is thrown, but the destination object will be Null.

    EDIT:

    if you know what type of result you want, you can use a helper method like this:

    public static Object TryConvertTo<T>(string input)
    {
        Object result = null;
        try
        {
            result = Convert.ChangeType(input, typeof(T));
        }
        catch
        {
        }
    
        return result;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to verify whether or not you can run reports against from the
Trying to make a make generic select control that I can dynamically add elements
Trying to create a QtRuby application, I get the following error: /usr/lib64/ruby/site_ruby/1.8/Qt/qtruby4.rb:2144: [BUG] Segmentation
I'm currently trying to verify whether or not, given an unsorted array A of
I am trying to verify whether an object is null or not, using this
I'd just like someone to verify whether the following problem is NP-complete or if
I've been trying to verify in my tests (GTM) that when a nib file
In my form, I'm trying to verify that the user fills in the same
In a given string, I'm trying to verify that there are at least two
I have an X.509 certificate that contains a set of data with the following

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.