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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T23:50:32+00:00 2026-05-17T23:50:32+00:00

I’ve looked at the various options that seem to be available on the .NET

  • 0

I’ve looked at the various options that seem to be available on the .NET framework (I think). In particular, I’ve looked at
1. TypeConverter
2. Convert.Toxxx
3. Convert.ChangeType

Each of them for one reason or another don’t work for me. I’m quite surprised that there doesn’t seem to be a solution in the .NET framework for this sort of thing. I’m assuming I’m not the only one who needs this 😉

Here is what I’m trying to do. Essentially, I’ve got a bunch of html forms that are used in an application. These forms allow users to enter data (obviously) and I need to extract this data into various data types.

These data types are simple types, primitive types, value types, reference types and custom reference types. In other words:
1. Int32, Int64, double, decimal, DateTime etc.
2. Int32[], double[], string[] and possibly other arrays of primitive types
3. various custom DTO objects that have the above types as properties.

The “input” is in the form of a string typically since I’m in Http land. To give you an idea of why the existing solutions don’t work for me, take a look at the following “string” inputs that need converting

  1. “1,234” -> Int32
  2. “$1,234” ->Int32
  3. ” $ 1,234 ” -> Int32
  4. “” -> Int32
  5. “1,2,3,4,5” ->Int32[]
  6. “0” OR “false” OR “False” OR “off” -> Boolean false

I know how to convert each of these cases by hand, but I am looking for either something that’s part of the framework that I’ve missed or a decent solution that handle the types of inputs listed above.

  • 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-17T23:50:33+00:00Added an answer on May 17, 2026 at 11:50 pm

    Jackie,

    This is a very common requirement in the Web Development world and as you’ve noted there is no built-in way to achieve this and what is there falls short in the simplest of cases.

    @Chris these rules are not arbitrary by any shot of the imagination. They are in fact very common when it comes to dealing with user input, especially over the web. In fact the Boolean conversions are also pretty common given that check boxes in ASP.NET return on/off (for some reason).

    You have a a couple of options. One is a simplistic approach and another an extensible solution. It all starts from the way you’d like to use this functionality from within your application. Since you’ve not shed a lot of light on what it is you are doing currently or how you’d like to do this, I’ve taken the liberty of making a few assumptions.

    The primary assumption being that the values come to you via the Request.Form or Request.QueryStrings (both of which are NameValueCollections that can hold multiple values for a given name).

    Let’s assume you’d like a method called ChangeType that given a name of the parameters and the Type you’d like to change it to will return the value as the required type. So the method signature might look like this:

    public T ChangeType<T>(string parameterName, T defaultValue = default(T))

    And so you can use it like so:

    int someId = ChangeType<int>("id", -1);
    

    So the value of the variable someId will either be the value extracted from Request.Form or Request.QueryStrings in the form of an int (if it exists) or -1 if it does not.

    A more complete implementation is as follows:

        static T ChangeType<T>(string value) where T: struct
    {
      Type typeOft = typeof(T);
      if (String.IsNullOrEmpty(value.Trim()))
        return default(T);
      if (typeOft == typeof(Int32))
        return (T)ConvertToInt32(value);
      else if (typeOft == typeof(Int64))
        return (T)ConvertToInt64(value);
      else if (typeOft == typeof(Double))
        return (T)ConvertToDouble(value);
      else if (typeOft == typeof(Decimal))
        return (T)ConvertToDecimal(value);
      return default(T);
    }
    
    static object ConvertToInt32(string value)
    {
      return Int32.Parse(value,
        NumberStyles.Currency ^ NumberStyles.AllowDecimalPoint);
    }
    
    static object ConvertToInt64(string value)
    {
      return Int64.Parse(value,
        NumberStyles.Currency ^ NumberStyles.AllowDecimalPoint);
    }
    
    static object ConvertToDouble(string value)
    {
      return Double.Parse(value, NumberStyles.Currency);
    }
    
    static object ConvertToDecimal(string value)
    {
      return Decimal.Parse(value, NumberStyles.Currency);
    }
    

    If you need to be able to handle more types, simply implement the required method (such as ConvertToInt32 for example) and add another else condition in the ChangeType<T> method and you’re done.

    Now if you’re looking for an extensible solution such that you can add additional capability without having to modify the primary code and be able ot handle your own custom types, then please take a look at this blog post of mine.
    [ChangeType – Changing the type of a variable in C#][1]
    http://www.matlus.com/2010/11/changetypet-changing-the-type-of-a-variable-in-c/

    Hope this Helps.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
I know there's a lot of other questions out there that deal with this
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post

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.