String Problem

  • Thread starter Thread starter Pablo Salazar
  • Start date Start date
P

Pablo Salazar

Hi people
I try to fill treeview but when i compile the programa it gave me the
following error :
"Use of Unassigned local variable 'nombre' "

public void Agregar(TreeNode x,int tipo)
{
string nombre;
switch (tipo)
{
case 1 :
nombre = "Elemento";
break;
case 2 :
nombre = "Atributo";
break;
case 3 :
nombre = "Comentario";
break;
case 4 :
nombre = "Texto";
break;
}
TreeNode t = new TreeNode(nombre );
t.SelectedImageIndex = tipo ;
t.ImageIndex = tipo;
t.Tag = "";

x.Nodes.Add (t);
}


Thanks.
 
Pablo:

Use:

string nombre = null;

This will make your variable "assigned" and able to be used in your program.

John
 
Add:

default:
nombre = <an appropriate default value>

to your switch block.

The C# compiler is fairly smart about figuring out if there is a possible
code path that will result in an unassigned variable.

Colin
 
your function is being called with the parameter of 'tipo' containing a
value other than 1,2,3, or 4.
Your switch statement results in no assignment.

look for a bug that would cause a different value to come into the function.

Good luck,
--- Nick
 
Back
Top