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

Can someone help me how to get the value of the string x and put it to another class? I want to use the value of string x in another class.

namespace WindowsFormsApp6
{
    public partial class Titles : Form
    {
        public Titles(string strToDisplay)
        {
            InitializeComponent();
            string x = strToDisplay;
        }

        private void titleList_SelectedIndexChanged_1(object sender, EventArgs e)
        {
           // USE X HERE
        }
    }
}
See Question&Answers more detail:os

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

1 Answer

You can't access method scoped variable outside that method. You need to declare that variable at class level and reuse it.

private string x;
public Titles(string strToDisplay)
    {
        InitializeComponent();
        x = strToDisplay;
    }


    private void titleList_SelectedIndexChanged(object sender, EventArgs e)
    {
        // Now you can access here
    }

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