In the event definition after init, why am I getting error 18 Using unrecognized local variable 'beforeEvntDrawArg'

I have an event object ...

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));
        }


    }
}

      

When I try to bubble an event and copy the details, I get error 18 Using unrecognized local variable 'beforeEntDrawArg'

If I assign null BeforeEvntDrawArgs beforeEvntDrawArg = null;

then it compiles fine because the app is bombing by saying null ref

. Not sure what I am doing wrong, can you help me fix this code?

However, in the body case, when I copy and initialize, what am I doing wrong, why am I getting uninitialized at compile time, and if I use null, why does it break at runtime? Please let me know how to fix this problem.

  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;
     }

      

Error 18 Using unassigned local variable 'beforeEvntDrawArg'

+3


source to share


2 answers


You are trying to use beforeEvntDrawArg from your own object initializer. This is not supported. https://msdn.microsoft.com/en-us/library/bb397680.aspx

Try it. It's exactly the same without the object initialization syntax.



BeforeEvntDrawArgs beforeEvntDrawArg = new BeforeEvntDrawArgs(e, this.TagFields)
beforeEvntDrawArg.HoverOver = Encoder.HtmlEncode(beforeEvntDrawArg.Text);
beforeEvntDrawArg.InnerHtml = Encoder.HtmlEncode(beforeEvntDrawArg.Text);

      

+1


source


I don't see anywhere in the class you posted (BeforeEvntDrawArgs) a definition for a constructor that takes 2 arguments. This means that the only available constructor will be the standard parameterless constructor. So when you try to execute

HoverOver = Encoder.HtmlEncode(beforeEvntDrawArg.Text)

      



beforeEvntDrawArg.Text hasn't been assigned a value yet.

+1


source







All Articles