我有一个事件对象...
public class BeforeEvntDrawArgs
{
/// <summary>
/// Event html.
/// </summary>
internal string InnerHTML
{
get
{
return this.Html;
}
set
{
this.Html = value;
}
}
/// <summary>
/// Event tags, using Data TagFields.
/// </summary>
public Gamex.TagCollection Tag
{
get;
private set;
}
/// <summary>
/// Event text.
/// </summary>
public string Text
{
get;
private set;
}
/// <summary>
/// Event Clue.
/// </summary>
public string HoverOver
{
get;
set;
}
internal BeforeEvntDrawArgs(Event e, string[] fields)
{
this.Id = e.Id;
this.Text = e.Text;
if (e.Tags != null)
{
this.Tag = new Gamex.TagCollection(fields, new List<string>(e.Tags));
}
}
}
尝试使事件冒泡并复制详细信息时,出现错误18使用未分配的局部变量'beforeEntDrawArg'
如果我分配空
BeforeEvntDrawArgs beforeEvntDrawArg = null;
,那么它可以正常编译,因为该应用程序炸毁了null ref
。不知道我在做什么错,您能帮我更正此代码吗?但是,在事件主体中,当我进行复制和初始化时,我做错了什么,为什么在编译时得到未初始化的信息,如果使用null,为什么它在运行时中断?请让我知道如何解决此问题。
private BeforeEvntDrawArgs DoBeforeEvntDraw(Event e)
{
// Issue begins here, and pre-initializing to null compiles,
// however breaks on runtime as a null ref
BeforeEvntDrawArgs beforeEvntDrawArg = new BeforeEvntDrawArgs(e, this.TagFields)
{
HoverOver = Encoder.HtmlEncode(beforeEvntDrawArg.Text),
InnerHtml = Encoder.HtmlEncode(beforeEvntDrawArg.Text)
};
this.OnBeforeEvntDraw(beforeEvntDrawArg);
return beforeEvntDrawArg;
}
错误18使用未分配的局部变量'beforeEvntDrawArg'
最佳答案
您正在尝试从其自身的对象初始化程序中使用beforeEvntDrawArg。不支持此功能。 https://msdn.microsoft.com/en-us/library/bb397680.aspx
尝试这个。只是没有对象初始化语法而已。
BeforeEvntDrawArgs beforeEvntDrawArg = new BeforeEvntDrawArgs(e, this.TagFields)
beforeEvntDrawArg.HoverOver = Encoder.HtmlEncode(beforeEvntDrawArg.Text);
beforeEvntDrawArg.InnerHtml = Encoder.HtmlEncode(beforeEvntDrawArg.Text);