开发者

Why this error ? "The type or namespace name 'c' could not be found (are you missing a using directive or an assembly reference?)"

The type or namespace name 'c' could not be found (are you missing a using directive or an assembly reference?)

I do have this namespace added "using System.Web.UI.WebControls;" why this error ??

 protected void DoSomething(Control control)(
    {

        foreach (Control c in control.Controls)
        { 
            if(typeof(c).Equals(Telerik.Web.UI.RadEditor))
            {
               Telerik.Web.UI.RadEditor rad = c as Telerik.Web.UI.RadEditor;
               rad.CssClass = "MyStyle";
                  label1.Visible = true; label1.Text = "dhchk";
               // control.CssFiles.Add("~/styles/myStyle.css"); 
            }
            else
            {
             开发者_开发知识库     DoSomething(c);
            }

        }

    }


You are using c is if it was a type, not a varaible.

Change this:

if (typeof(c).Equals(Telerik.Web.UI.RadEditor))

into:

if (c.GetType().Equals(typeof(Telerik.Web.UI.RadEditor)))

or simply:

if (c is Telerik.Web.UI.RadEditor)


typeof(x) expects x to be a Type, not an Object.

Use this instead

if(c is Telerik.Web.UI.RadEditor)

A correct use of typeof is

if (c.GetType().Equals(typeof(Telerik.Web.UI.RadEditor))


The typeof(...) operator needs to be given a type name, known at compile time. I think you actually meant:

if (c.GetType().Equals(typeof(Telerik.Web.UI.RadEditor)))

However, it would be better to write:

RadEditor editor = c as Telerik.Web.UI.RadEditor;
if (editor != null)
{
    rad.CssClass = "MyStyle";
    label1.Visible = true;
    label1.Text = "dhchk";
}

This would then also cope with the situation where c is an instance of a subclass of RadEditor, which I assume should go down the same path.

By using as once instead of is then as or a cast, you only have to do the dynamic type checking once - it's generally neater.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜