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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T18:24:56+00:00 2026-06-07T18:24:56+00:00

My function requires you pass it a string and a Type as T. Based

  • 0

My function requires you pass it a string and a Type as T. Based on T i want to parse the string val as that type, but I get the error from the title of this question. Anyone that has any insight or other ways of accomplishing this function, I would greatly appreciate it.

T Parse<T>(string val) where T : System.Object
    {
        TypeCode code = Type.GetTypeCode(typeof(T));
        switch (code)
        {
            case TypeCode.Boolean:
                return System.Boolean.Parse(val);
                break;
            case TypeCode.Int32:
                return Int32.Parse(val);
                break;
            case TypeCode.Double:
                return Double.Parse(val);
                break;
            case TypeCode.String:
                return (string)val;
                break;
        }
        return null;
    }
  • 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-07T18:24:57+00:00Added an answer on June 7, 2026 at 6:24 pm

    Just remove where T : System.Object.

    By stating:

    where T : System.Object
    

    you’re saying that the types T usable in your method Parse must inherit from object.
    But, since every object in C# inherits from System.Object you don’t need that constraint (and probably that’s one of the reasons why the compiler does not allow that).

    Also, since you’re returning null, you should constraint the type T to be a reference type, so:

    where T: class
    

    But in this way you can’t return a boolean, integer or whatever value type.

    However, your code basically mimics the functionality of Convert.ChangeType, the only difference is that you’re using generics to return the correct type instead of object, but is basically equal to this:

    T Parse<T>(string val)
    {
        return (T)Convert.ChangeType(val,typeof(T));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a PHP function that requires can take 3 parameteres... I want to
I have a Map[String,String] and a third party function that requires a Map[String,Seq[String]] Is
I have a function which requires me to pass a UTF-8 string pointed by
$(.container).hover( function(){ $(.child-1).hide(0); $(.child-2).show(0); },function(){ $(.child-1).show(0); $(.child-2).hide(0); }); A project I have requires that
For example : function masterMethod(this, action){ // (action); <-- But action requires $(this) to
require_once('/recaptcha/recaptchalib.php'); was in the correct place but the error says: Warning: require_once() [function.require-once]: open_basedir
I'm attempting to create a function where I can pass names and values that
I want to pass an argument to a function, which takes a const char
I have an array of type string which I want to re-use inside a
I am trying to create a function that will pass some parameters to a

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.