0

I am trying to write a function to convert the contents of a string "12345" to an int.

If the string is blank i would like to return null (uninitialized), not the value 0.

Problem is, functions do not return un-initialized values.

My code will not compile as Retval can return an uninitialized value......

My attempt so far:

public int ConvertStringToNumber(String TheString)
{
    // Uninitialized
    int Retval;

    if (TheString.Length > 0)
    {
        // We have a valid string
        if (Int32.TryParse(TheString, out Retval))
        {
            // We have a valid Number
        }
    }

    // Return the number or null
    return Retval;
}

6 Answers 6

8

Can you use Nullable int ? it will allow set as nullable . See here : http://www.codeproject.com/Articles/11854/C-2-0-Nullable-Types

Sign up to request clarification or add additional context in comments.

Comments

1

You can use a nullable int (more info here).

Nullable types can represent all the values of an underlying type, and an additional null value.

public int? ConvertStringToNumber(String TheString)
{
    int retval;
    bool isInt = Int32.TryParse(TheString, out retval);
    return isInt ? retval : null;
}

Note: When using nullable types, you'll need to use a cast or get it's value. See here.

Example:

int? n = ConvertStringToNumber("123");

int value = n.Value;
// or
int value = (int)n;

Comments

0

If you assigned a value to the Retval object AT THE FIRST TIME, then the value is valid in THAT area ONLY. So, Retval is null when you return it.

Comments

0

since Int32.TryParse(TheString, out Retval) require int type not nullable

public int? ConvertStringToNumber(String TheString)
{

    // Uninitialized
    int Retval;

    if (TheString.Length > 0)
    {

        // We have a valid string

        if (Int32.TryParse(TheString, out Retval))
        {

            // We have a valid Number
            return Retval;
        }

    }

            // Return the number or null

    return null;
}

Comments

-1

Simple extension method to resolve your problem

using System;

namespace temp
{
    class Program
    {
        static void Main(string[] args)
        {
            string valu = "";
            Console.WriteLine(valu.ToInt32());
            Console.ReadLine();
        }
    }
    public static class MyExtentions
    {
        public static int ToInt32(this string s)
        {
            int x;
            if (s != null)
            {
                if (s.Length > 1)
                    x = Convert.ToInt32(s);
                else
                    x = 0;
            }
            else
            {
               x= 0;
            }
            return x;
        }
    }
}

Comments

-2
int? x = ConvertStringToNumber("1");

int value = x.Value;

String to numeric conversion in c#

Comments

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.