Type.GetType
expects a fully-qualified type name but you are trying to pass it name of the variable.And also it returns a Type
instance so you can't cast it to Label
.It seems you are confused about types,instances and variables. If you have labels and you want to access them you can use Controls
collection of your Form
.
foreach (var s in sections)
{
var name = string.Format("lbl{0}", s);
if(this.Controls.ContainsKey(name))
{
var currentLabel = this.Controls[name] as Label;
if(currentLabel != null) currentLabel.Text = "Test";
}
}
Edit: If you are devoloping an ASP.NET
project then you can use FindControl
method to get your labels by name:
foreach (var s in sections)
{
var name = string.Format("lbl{0}", s);
var currentLabel = FindControl(name) as Label;
if(currentLabel != null) currentLabel.Text = "Test";
}