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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T19:32:20+00:00 2026-06-03T19:32:20+00:00

in one of my projects I’m using following two methods. 1. GetDoubleValue and 2.

  • 0

in one of my projects I’m using following two methods. 1. GetDoubleValue and 2. GetIntValue. GetDoubleValue uses double.TryParse to parameter str string and returns 0 if it fails while GetIntValue tries int.TryParse to parameter str string and returns 0 if it fails. What I want is to combine these two methods into one generic method that alongwith string str receives parameter T as well so that if I want to use GetDoubleValue Method I can use double for the parameter T and if I want to use GetIntValue Method I can use Int for the parameter T

public double GetDoubleValue(string str)
{
    double d;
    double.TryParse(str, out d);
    return d;
}
public int GetIntValue(string str)
{
    int i;
    int.TryParse(str, out i);
    return i;
}

Note: I have tried something like this;

private T GetDoubleOrIntValue<T>(string str) where T : struct 
{
    T t;
    t.TryParse(str, out t);
    return t;
}

EDIT

In my database I have more than 30 columns in differenct tables having numeric datatype. I want to insert 0 in each column if user does not type anything in the textbox i.e he leaves all or some of the textboxes empty. If I don’t use the GetIntValue method I will have to use the method body more than 30 times. that is why I am doing this through method approach. I’m writing three of more than thirty examples for instance

cmd.Parameters.Add("@AddmissionFee", SqlDbType.Decimal).Value = GetIntValue(tbadmissionfee.Text);
cmd.Parameters.Add("@ComputerFee", SqlDbType.Decimal).Value = GetIntValue(tbcomputerfee.Text);
cmd.Parameters.Add("@NotesCharges", SqlDbType.Decimal).Value = GetDoubleValue(tbnotescharges.Text);

I want to combine the aforesaid two methods because today I’m having two methods like this which if combined will not give any better improvement in the programming but tomorrow I may have tens of methods like this that will better be combined into one generic method. e.g I may have GetInt32Value, GetShortValue etc. hope it is now cleared why I want this???

  • 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-03T19:32:21+00:00Added an answer on June 3, 2026 at 7:32 pm

    You could do something like this:

       public static T GetDoubleOrIntValue<T>(this string str) where T : IConvertible
    {
        var thisType = default(T);
        var typeCode = thisType.GetTypeCode();
        if (typeCode == TypeCode.Double)
        {
            double d;
            double.TryParse(str, out d);
            return (T)Convert.ChangeType(d,typeCode) ;
        }
        else if (typeCode == TypeCode.Int32)
        {
            int i;
            int.TryParse(str, out i);
            return (T)Convert.ChangeType(i, typeCode);
        }
        return thisType;
    }
    

    Then when you call it:

    string d = "1.1";
    string i = "3";
    
    double doubleValue = d.GetDoubleOrIntValue<double>();
    int intValue = i.GetDoubleOrIntValue<int>();
    

    But the whole thing seems kinda silly to me.

    EDIT: Saw someone else using Convert.ChangeType…which provides for a generic return type.

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

Sidebar

Related Questions

I'm merging two projects one uses delegates for logging and the other uses events.
For one of my projects, I'm using the DOMDocument class to load and manipulate
I built a really basic php/mysql site for an architect that uses one 'projects'
okay so I have two projects one in which I have done all the
I've two Visual Basic 2008 projects - one is a class library project and
I have a solution with two service projects (one is hosting a unit test
I have an older asp.net mvc solution (s#arp framework) whith two projects one for
Having two projects: one is a class library (a .dll assembly) and the other
I find the whole process of merging two projects (one committed to SVN the
I have two Eclipse projects: one that depends upon a certain library (android.jar) and

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.