TroggleDorf
TroggleDorf

Reputation: 420

Error with local variable

The following function is generating an error, "Use of unassigned local variable 'intLast'. I am not sure what I am doing wrong.

    static string RemovePath(string strInput)
    {

        int intLast;

        for (int i = 1; i < strInput.Length; i++)
        {
            if (strInput.Substring(i, 1) == @"\")
            {
                intLast = i;
            }
        }

        string strOutput = strInput.Substring(strInput.Length - intLast);

        return strOutput;
    }

Upvotes: 4

Views: 168

Answers (7)

CloudyMarble
CloudyMarble

Reputation: 37566

You are using the variable intLast at the following line:

string strOutput = strInput.Substring(strInput.Length - intLast);

But the variable will have a value only under certain conditions (strInput.Length > 1 and strInput.Substring(i, 1) == @"\"). therefor the error.

To solve this provide a default value on the declaration:

int intLast = 0; // or any default value.

Upvotes: 2

Freelancer
Freelancer

Reputation: 9074

Just Edit:

int intLast = 0;

Whole Code will look like this:

static string RemovePath(string strInput) { int intLast = 0;

    for (int i = 1; i < strInput.Length; i++)
    {
        if (strInput.Substring(i, 1) == @"\")
        {
            intLast = i;
        }
    }

    string strOutput = strInput.Substring(strInput.Length - intLast);

    return strOutput;
}

You just have to assign intLast to zero initially.

Upvotes: 0

Adil
Adil

Reputation: 148120

Initialize intLast with some value as loop can not ensure that value is assigned as the execution of loop is decided on runtime.

 int intLast = 0;

The following lines is asking for unassigned variable as control might not go into loop to assign value.

string strOutput = strInput.Substring(strInput.Length - intLast);

Upvotes: 0

Upendra Chaudhari
Upendra Chaudhari

Reputation: 6543

You should initialized your variable like

int intLast = 0;

Because it might remain unassigned if it code not reach in if condition.

Upvotes: 0

Soner G&#246;n&#252;l
Soner G&#246;n&#252;l

Reputation: 98750

You should initialized your intLast variable.

The compiler doesn't know that variable like intLast will be assigned no matter what.

 int intLast = 0;

Upvotes: 0

rhughes
rhughes

Reputation: 9583

You need to assign a default value to intLast.

Try this:

static string RemovePath(string strInput)
{
    int intLast = 0;

    for (int i = 1; i < strInput.Length; i++)
    {
        if (strInput.Substring(i, 1) == @"\")
        {
            intLast = i;
        }
    }

    string strOutput = strInput.Substring(strInput.Length - intLast);

    return strOutput;
}

Upvotes: 0

jonvuri
jonvuri

Reputation: 5930

Since you assign intLast only inside of a conditional, from the compiler's perspective it is possible for you to use it without initializing it.

You should initialize it to some default value at the start even if you do not expect to ever use that.

int intLast = 0

Upvotes: 0

Related Questions