0

How can i use the value from ip and user (line 3) in line 12?

public partial class MainWindow : Window
{
    public MainWindow(string ip, string user)
    {
        InitializeComponent();
        label1.Content = "Connected to: " + user + " " + "(" + ip + ")";
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        MessageBox.Show(user);
    }
}

The code from the other Form:

public partial class LoginWindow : Window
{
    public LoginWindow()
    {
        InitializeComponent();
    }

    public void Button_Click(object sender, RoutedEventArgs e)
    {
        MainWindow w1 = new MainWindow(txtip.Text,txtuser.Text);
        w1.Show();
        this.Close();
    }
}

I'm new to C #, thanks for your answers!

2
  • 1
    What is line 12? Can you please a comment in the code where you want to use it? Commented Mar 31, 2020 at 22:30
  • Neither ip or user is directly accessibly outside MainWindow without storing it somewhere. Commented Mar 31, 2020 at 22:32

1 Answer 1

3

Is this what you need?

public partial class MainWindow : Window
{
    private string _user;

    public MainWindow(string ip, string user)
    {
        InitializeComponent();
        _user = user;
        label1.Content = $"Connected to: {_user} ({ip})";
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        MessageBox.Show(_user);
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

No idea why you got down-voted. It's not clear from the question exactly what the OP meant (like why he shows two classes where it seems that he's only interested in the first one). But, if you count the lines, your answer is pretty close to what one interpretation of the question might mean. I upvoted you since someone else dinged you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.