Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

My project has a class ‘mybutton’.

namespace Project.WebUI.HtmlHelpers
{
    public class myButton 
    {
        public myButton()
        {
            Clickable = false;
            Selected = false;
            Url = "";
        }

        public myButton(string displayText, string url) : this()
        {
            DisplayText = displayText;
            Url = url;
        }

        public bool Clickable { get; set; }
        public bool Selected { get; set; }
        public string DisplayText { get; set; }
        public string Url { get; set; }

    }
}

When I try to create a new instance of myButton from another class I get the error “A field initializer cannot reference the non-static field, method...” (red squiggle over url).

public class PageElements
{
    url =”url goes here”;
    public myButton CancelButton = new myButton("Cancel Order", url);
    …
    …
}

What am I doing wrong?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
209 views
Welcome To Ask or Share your Answers For Others

1 Answer

It says to move the initialization of CancelButton into PageElements constructor. Ii order to work what you tried to do the url should have been either static or constant


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...