Home > Enterprise >  WPF accessing variables outside of Mainwindow class
WPF accessing variables outside of Mainwindow class

Time:09-21

I have got my code that gets the users input (email and password) and I have transferred it to my main WPF form and am now trying to access those variables from another class and am struggling to do so any help much appreciated.

This is where the variables are:

public partial class MainWindow : Window
{
    
    public MainWindow(string clientEmail, string clientPass)
    {
        InitializeComponent();

        string email = clientEmail;
        string password = clientPass;
        
        
    }

and this is where i want to be able to access the code

public class emailSender
{
    
    
    

    virtual public bool sendEmail(string recEmail, string usrSubject, string usrBody)
    {
        
        try
        {
            var smtpClient = new SmtpClient("smtp.gmail.com")
            {
                Port = 587,
                Credentials = new NetworkCredential(email, password),
                EnableSsl = true,

            };

            smtpClient.Send(email, recEmail, usrSubject, usrBody);
            MessageBox.Show("Email Successfully sent");
            
            return true;
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
            return false;
        }




    }
}

CodePudding user response:

One of the ways you could do this is to use application settings to store global variables.

Step 1: In Visual Studio go to "Project\[app name] Properties".

enter image description here

Step 2: Create 2 settings and name them clientEmail and clientPass. Make sure they are set to user scope and are of string type.

enter image description here

Step 3: To write to your newly created settings:

public partial class MainWindow : Window
{
    string UserEmail = "[email protected]";
    string UserPassword = "12345";

    public MainWindow()
    {
        InitializeComponent();
        Properties.Settings.Default.clientEmail = UserEmail;
        Properties.Settings.Default.clientPass = UserPassword;
    }

}   

Step 4: To read from your newly created properties:

 string email = Properties.Settings.Default.clientEmail;
 string password = Properties.Settings.Default.clientPass; 

Step 5: If you wish to save your settings:

Properties.Settings.Default.Save();   

CodePudding user response:

First of all, you should consider if transfering your variables to the MainWindow is something you need to do. It looks like you don't need them there. The next thing is to see where you are calling sendEmail from. Do you have access to the variables there? you could add them to the method call of sendEmail:

virtual public bool sendEmail(string recEmail, string usrSubject, string usrBody, string clientEmail, string clientPass)
{
    // email sending code
}

If you really want to have them in the MainWindow you have to store them as a public variable:

public partial class MainWindow : Window
{
    public string email;
    public string password;
    
    public MainWindow(string clientEmail, string clientPass)
    {
        InitializeComponent();

        email = clientEmail;
        password = clientPass;
        
        
    }
...
}

now you could do:

public class emailSender
{
    // we need a reference to the main window
    private MainWindow mainWindow;

    public emailSender(MainWindow mainWindow)
    {
        // add mainWindow reference while constructing the email sender
        this.mainWindow = mainWindow;
    }
    
    virtual public bool sendEmail(string recEmail, string usrSubject, string usrBody)
    {
        var smtpClient = new SmtpClient("smtp.gmail.com")
        {
            Port = 587,
            // now we can access the variables of mainWindow
            Credentials = new NetworkCredential(mainWindow.email, mainWindow.password),
            EnableSsl = true,

        };
        // do the email sending code
        
    }
}

there still are many better ways to do this. To use them you have to consider where the variables should be stored and which classes need access to them.

  • Related