0

I want to convert a string to specific type based on the value of other string

Suppose I have two strings str1, str2.

If str2 is double then I want to call convert.ToDouble(str1) and return the Double value. Is there any way to accomplish this?

I tried using reflections (using methodInfo object to invoke)

But still it returns again an object for which I need to convert.

Please help..

1
  • You mean str2 represents a double number or str2 is "double" ? Commented Sep 28, 2011 at 5:53

4 Answers 4

3

Use the double.TryParse Method.

The method attempts to convert a string to a double and if it fails it returns false.

If I'm understanding you correctly, this is what you want:

private static double ConditionalConvertToDouble(string str1, string str2) {
    double converted;
    if (double.TryParse(str2, out converted)) {
        // str2 can be converted to a double, so return str1 converted to a double.
        return Convert.ToDouble(str1);
    } else {
        // I'm throwing an exception here if str1 cannot be converted to a double; you 
        // might want to do something different.
        throw new ArgumentException("str1 cannot be converted to a double");
    }
}

Call the method like this:

var d = ConditionalConvertToDouble("11", "22");
Sign up to request clarification or add additional context in comments.

Comments

1
double result;
Double.TryParse(str1, out result);

if str1 is double, result will have its value, otherwise result will have 0. It will not throw any exception.

Hope it helps.

Comments

1

The following method will attempt to get the double value from "str1" if and only if "str2" also represents a double. If either of the numbers are not doubles, it will return double.NaN.

This method avoids throwing exceptions, but still allows you to check if you have a valid number.

public double GetFirstDoubleIfSecond(string str1, string str2)
{
    double myDouble;

    if(double.TryParse(str2, out myVal) && double.TryParse(str1, out myVal))
    {
        return myDouble
    }

    return double.NaN;
}

If you are expecting NaN values in your string, then you can use the following method, which will throw an exception if either of the strings are not doubles. Otherwise, it will return the string value represented in "str1".

public double GetFirstDoubleIfSecondWithExceptions(string str1, string str2)
{
    double.Parse(str2);

    return double.Parse(str1);
}

Comments

-2

I think that switch-case operator should help to you. You should to specify some cases depending on one of your input string and make right decision. Your method should return object, if you really don't know which type exactly will return your method.

public object MyMethod(string str1, string str2)
{
    switch(something)
    {
            case 1:
            case 2:
                return double.Parse(str1);
            break;
            case 3:
            case 4:
                return int.Parse(str1);
            break;
            default
                return null;
    }
}

where something is the criterion you should to specify.

After that System.Object.GetType() method can help you to detirmine which type was returned by your method.

7 Comments

In no way does this simplify the code being written. It would take more logic (and code) to determine and use the type of the returned reference than to use simple TryParse's through your code.
I want to convert a string to specific type based on the value of other string - (Q). if specific type is only double - your are right; else: not all types have TryParse() methods. double can parse string which contains int, for example.
true, but the argument still stands. Having multiple methods with descriptive return types is better than a single method with a non-desciptive return type. Unless, for some reason, you don't care about the returned reference's type... In which case, you could use the multiple descriptive methods within your non-descriptive method.
Having multiple methods with descriptive return types is better than a single method with a non-desciptive return type. - why? in both cases you should switch, which converter to use.
nice example. thanks for explanation! (in my time zone 11 AM :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.