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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T10:32:01+00:00 2026-05-12T10:32:01+00:00

I have become accustomed to using TryParse for attempting to parse unknown types: Dim

  • 0

I have become accustomed to using TryParse for attempting to parse unknown types:

Dim b As Boolean
Dim qVal As Boolean = If(Boolean.TryParse(Request.QueryString("q").Trim(), b), b, False)

or

bool b;
bool qVal = (Boolean.TryParse(Request.QueryString("q").Trim(), out b) ? b : false;

So, just curious if someone knows a better way of doing this other than using a ternary operator.


Solution

Hello Again,

Since the post has been closed, I’m sure this solution will get buried out there, but I created a pretty cool class that solves the problem above using the advice I was given. Just wanted to put the code out there in case some one stumbles upon this thread and would like to use it:

public static class PrimitiveType
{
    /// <summary>
    /// This function will return a parsed value of the generic type specified.
    /// </summary>
    /// <typeparam name="valueType">Type you are expecting to be returned</typeparam>
    /// <param name="value">String value to be parsed</param>
    /// <param name="defaultValue">Default value in case the value is not parsable</param>
    /// <returns></returns>
    public static valueType ParseValueType<valueType>(string value, valueType defaultValue)
    {
        MethodInfo meth = typeof(valueType).GetMethod("Parse", new Type[] { typeof(string) });
        if (meth != null)
        {
            try
            {
                return (valueType) meth.Invoke(null, new object[] { value });
            }
            catch (TargetInvocationException ex)
            {
                if (ex.InnerException.GetType() == typeof(FormatException) || ex.InnerException.GetType() == typeof(OverflowException))
                {
                    return defaultValue;
                }
                else throw ex.InnerException;
            }
        }
        else
        {
            throw new ArgumentException("Generic type must be a valid Value Type that has a Parse method.");
        }
    }
}

It’s pretty simple to use. Just pass in the type you’re expecting as the generic and provide a string value to be parsed and a default value in case the string is not parsable. If you provide a class instead of a primitive type it will throw new ArgumentException("Generic type must be a valid Value Type that has a Parse method.");

  • 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-12T10:32:01+00:00Added an answer on May 12, 2026 at 10:32 am

    I’ve previously wrapped querystrings in my own class. Then I can do something like the following:

    var qs = new QueryString(Request.QueryString);
    bool q = qs.Get<bool>("q");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 179k
  • Answers 179k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer would this not be easier like this? <noscript> <meta http-equiv="refresh"… May 12, 2026 at 3:55 pm
  • Editorial Team
    Editorial Team added an answer You need to use the pluginspage attribute to tell the… May 12, 2026 at 3:55 pm
  • Editorial Team
    Editorial Team added an answer var panelItemId = 12345; var param1 = 'expandPanel'; var obj… May 12, 2026 at 3:55 pm

Related Questions

I have become accustomed to using F6 to compile the current document. A third-party
I worked on an embedded system this summer written in straight C. It was
I have a page that is currently using the datetime microformat to display a
I'm thinking about porting a database from Sql Server 2005 to MySQL. I've become

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.