I want to know if a variable has already been created. I have an IF that instantiates a class and a variable is initialized.
if (nivel == "1") { HtmlGenericControl li = new HtmlGenericControl("li"); HtmlGenericControl a = new HtmlGenericControl("a"); HtmlGenericControl ul = new HtmlGenericControl("ul"); tree2.Controls.Add(li); a.Attributes.Add("href", "#"); a.InnerHtml = titulo; //nivel 1 li.Controls.Add(a); }
In this case I want to know if the variable li was already created. Something like this:
if(exists li){ if (nivel == "2") { HtmlGenericControl ul2 = new HtmlGenericControl("ul"); li.Controls.Add(ul2); HtmlGenericControl li2 = new HtmlGenericControl("li"); ul2.Controls.Add(li2); HtmlGenericControl a2 = new HtmlGenericControl("a"); a2.Attributes.Add("href", "#"); a2.InnerHtml = nombre; li2.Controls.Add(a2); HtmlGenericControl ul3 = new HtmlGenericControl("ul"); li2.Controls.Add(ul3); } }
Since that variable is only created at runtime, I need to make that condition.