2

I looked previous questions but it didn't help. I have a very simple function written in c#. It give me compiler error "Use of unassigned local variable 'linea' " (line (2)). What could be wrong? And could tell me how to correct it please?

    public void llenarTabla()
    {

        int idx;
        string[] linea; (1)
        for (idx = 0; idx < numListas; idx++)
        {
            linea[0] = Convert.ToString(idx); // (2)
            switch(OrdenListas[idx]){
                case 0: linea[1] = "Crescente"; break;
                case 1: linea[1] = "Decrescente"; break;
                case 2: linea[1] = "Aleatorio"; break;
                default: linea[1] = "No especificado" ; break;
            }
            linea[2] = Convert.ToString(LongitudListas[idx]);
        }
    }

I already saw the reference "Compiler error if a variable is used but it might be not initialized. But as far I see the variable string[] (indexes:0,1,2) is inizialized in every case except numlistas = 0 (numlistas is a class parameter and its value is supposed to be >=1).

I also tried to change line (1) and (2) to:

(1) List<string> linea;

(2) linea.Add(Convert.ToString(idx));

but same error (when I tried to change line (1) and (2) I put as a comment all the following lines.

Thank you in advance for every help

1
  • Would you mind marking one of the answers as accepted? Commented Mar 4, 2012 at 15:50

4 Answers 4

2

Just do:

string[] linea = new string[3];//3 is the length of your array
//you can store 3 elements in linea [0] [1] and [2]

See MSDN reference for arrays in C#

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

Comments

2

You have declared a variable of type string[] (array of string), but you have not assigned it a value. In other words, the linea variable is not yet initialized when you use it.

Your line (1) needs to look like:

string [] linea = new string[numListas];

Comments

1

The problem is you declared the variable string[] linea but you never initialized it to a specific value before using it within the body of the for loop. Based on the usage within the loop you want it to have 3 elements so the following will work

string[] linea = new string[3];

Comments

1

Change to this:

  public void llenarTabla()
    {

        int idx;
        List<string> linea=new List<string>();
        for (idx = 0; idx < numListas; idx++)
        {
            linea.Add(Convert.ToString(idx)); // (2)
            switch(OrdenListas[idx]){
                case 0: linea.Add("Crescente"); break;
                case 1:linea.Add("Decrescente"); break;
                case 2: linea.Add("Aleatorio"); break;
                default:linea.Add("No especificado" ); break;
            }
           linea.Add(Convert.ToString(LongitudListas[idx]));
        }
    }

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.