user11824860
user11824860

Reputation:

use variables from a class in other classes

I create a class that I define some variables with their properties.

also I have two class "Form1" and "Form2".

I assign values to this variables in "Form1" but when I want to use the values in "Form2" after I assigned them and show them through MessageBox.Show(), I find out the variables are empty.

class Property
{
    private string a_username;
    private string a_email;


    public string username
    {
        get { return a_username; }
        set { a_username = value; }
    }
    public string email
    {
        get { return a_email; }
        set { a_email = value; }
    }

    public string password { get; set; } = "88306540";
}

the assignment: (this function is in "Form1")

Property pro = new Property();
private void CreateUserInform()
    {
        userid = File.ReadLines(filePath).Skip(idx).Take(1).First();
       // MessageBox.Show(userid);
        HtmlElementCollection elemcol = webBrowser2.Document.GetElementsByTagName("option");
        int i = 0;
        string[] mailservices = new string[elemcol.Count];
        foreach (HtmlElement elem in elemcol)
        {
            mailservices[i] = elem.InnerText;
            i += 1;
        }
        pro.username = userid;
        Random rand = new Random();
        mailservice = mailservices[rand.Next(10)];
        pro.email = pro.username + mailservice;
        wb2func_create_mail();
    }

call function: (this function is in "Form2" and it called after previous function.)

Property pro = new Property();
public void signup_fill()
    { 
        HtmlElementCollection elemcol = site.Document.GetElementsByTagName("input");
        foreach (HtmlElement elem in elemcol)
        {
            if (elem.Name == "login")
                elem.SetAttribute("value", pro.username);
            if (elem.Name == "remail")
                elem.SetAttribute("value", pro.email);
            if (elem.Name == "password")
                elem.SetAttribute("value", pro.password);
            if (elem.Name == "password2")
                elem.SetAttribute("value", pro.password);
        }
        MessageBox.Show(pro.username);
    }

I should mention that the "password" variable was shown pretty good but the others were shown empty.

also when I call them in "Form1" that I used to define them, it works just fine and shows the correct assignment.

the completely Form2 codes:

namespace Bypassing
{
public partial class Form2 : Form
{

    string referal_link;

    Property pro = new Property();

    public Form2(Property form1Property)
    {
        InitializeComponent();
        pro = form1Property;
    }



    public void signup_fill()
    { 
        HtmlElementCollection elemcol = site.Document.GetElementsByTagName("input");
        foreach (HtmlElement elem in elemcol)
        {
            if (elem.Name == "login")
                elem.SetAttribute("value", pro.username);
            if (elem.Name == "remail")
                elem.SetAttribute("value", pro.email);
            if (elem.Name == "password")
                elem.SetAttribute("value", pro.password);
            if (elem.Name == "password2")
                elem.SetAttribute("value", pro.password);
        }
        MessageBox.Show(pro.username);
    }

    private void btn_fill_Click(object sender, EventArgs e)
    {
        signup_fill();
    }

    private void btn_logout_Click(object sender, EventArgs e)
    {           
        HtmlElementCollection elemcol = site.Document.GetElementsByTagName("a");
        foreach (HtmlElement elem in elemcol)
        {
            if (elem.InnerText == " Log out ")
                elem.InvokeMember("click");                
        }
    }

    private void btn_next_link_Click(object sender, EventArgs e)
    {

    }

    public bool btn_fill_enabled
    {
        get { return btn_fill.Enabled; }
        set { btn_fill.Enabled = value; }
    }

    public bool btn_logout_enabled
    {
        get { return btn_logout.Enabled; }
        set { btn_logout.Enabled = value; }
    }

    public bool btn_next_link_enabled
    {
        get { return btn_next_link.Enabled; }
        set { btn_next_link.Enabled = value; }
    }

    private void avelon_site_Completed(object sender, WebBrowserNavigatedEventArgs e)
    {
        Form1 main_win = new Form1();
        main_win.text_edit();
    }
}

}

Form1 codes:

namespace BitcoinCloudMiningBypassApp
{

public partial class Form1 : Form
{

    private string mailservice;
    private string userid;
    int idx = 29;

    bool wb1flag = true;
    bool wb2flag = true;
    bool wb1ready = false;
    bool wb2ready = false;
    bool workflag = false;
    bool start = false;
    string filePath;        
    //StreamWriter file2 = new StreamWriter("avelon_users_email.txt", true);
    //StreamWriter file = new StreamWriter("avelon_referal_links.txt", true);        
    Property pro = new Property();

    private void OpenFileDialogForImportingUserId()
    {
        //var fileContent = string.Empty;

        using (OpenFileDialog openFileDialog = new OpenFileDialog())
        {
            openFileDialog.InitialDirectory = "C:\\";
            openFileDialog.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
            openFileDialog.FilterIndex = 1;
            openFileDialog.RestoreDirectory = true;

            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                //Get the path of specified file
                filePath = openFileDialog.FileName;
                btn_start.Enabled = true;

                ////Read the contents of the file into a stream
                //var fileStream = openFileDialog.OpenFile();

                //using (StreamReader reader = new StreamReader(fileStream))
                //{
                //    fileContent = reader.ReadToEnd();
                //}
            }
        }

    }

    private void CreateUserInform()
    {
        userid = File.ReadLines(filePath).Skip(idx).Take(1).First();
       // MessageBox.Show(userid);
        HtmlElementCollection elemcol = webBrowser2.Document.GetElementsByTagName("option");
        int i = 0;
        string[] mailservices = new string[elemcol.Count];
        foreach (HtmlElement elem in elemcol)
        {
            mailservices[i] = elem.InnerText;
            i += 1;
        }
        pro.username = userid;
        Random rand = new Random();
        mailservice = mailservices[rand.Next(10)];
        pro.email = pro.username + mailservice;
        //MessageBox.Show(avelon_email);
        wb2func_create_mail();
    }

    public Form1()
    {
        InitializeComponent();
        webBrowser2.Navigate("https://temp-mail.org/en/option/change/");

    }


    private void btn_start_Click(object sender, EventArgs e)
    {
        Form2 avelon = new Form2(pro);
        avelon.Show();
        //VPN vpn = new VPN();
        // MessageBox.Show(vpn.ConnectVPN(vpn.SetServer(0)).ToString());
        CreateAvelonUserInform();
        start = true;
        btn_load.Enabled = false;
        btn_start.Enabled = false;            
        avelon.btn_logout_enabled = true;
        btn_refresh.Enabled = false;
    }


    private void Form1_Load(object sender, EventArgs e)
    {

        btn_load.Enabled = false;
        btn_start.Enabled = false;
        btn_next.Enabled = false;
        //Process.Start("cmd.exe", "taskkill / F / im notepad.exe");

    }

    private void webBrowser2_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
        if (wb2flag)
        {
            wb2flag = false;
            wb2ready = true;
            if (!start)
                btn_load.Enabled = wb2ready;
        }
        else
        {              
            text_edit();
        }
    }

    private void wb2func_create_mail()
    {
        HtmlElementCollection elemcol = webBrowser2.Document.GetElementsByTagName("option");
        foreach (HtmlElement elem in elemcol)
        {
            if (elem.InnerText == mailservice)
                elem.SetAttribute("selected", "selected");
        }
        HtmlElementCollection elemcol2 = webBrowser2.Document.GetElementsByTagName("input");
        foreach (HtmlElement elem in elemcol2)
            if (elem.GetAttribute("name") == "mail")
                elem.SetAttribute("value", pro.username);
        //wb2flag = true;
        //workflag = true;
        webBrowser2.Document.GetElementById("postbut").InvokeMember("click");            
    }


    private void btn_load_Click(object sender, EventArgs e)
    {
        //OpenFileDialogForImportingUserId();
        VPN vpn = new VPN();
        MessageBox.Show(vpn.ConnectVPN(vpn.SetServer(0)).ToString());
        //Process.Start("C:\\Users\\Hossein\\Desktop\\output.bat");
        //MessageBox.Show(Environment.GetEnvironmentVariable("a", EnvironmentVariableTarget.Process));
        //MessageBox.Show(Environment.GetEnvironmentVariable("a", EnvironmentVariableTarget.User));
        //MessageBox.Show(Environment.GetEnvironmentVariable("a", EnvironmentVariableTarget.Machine));


    }


    public void text_edit()
    {
        txt_username.TextChanged += new EventHandler(text_change);
        txt_username.Text = pro.username;
        txt_email.Text = pro.email;

    }

    private void btn_next_Click(object sender, EventArgs e)
    {


    }

    private void btn_refresh_Click(object sender, EventArgs e)
    {            
        webBrowser2.Navigate("https://temp-mail.org/en/option/change/");
    }

    private void text_change(object s , EventArgs e)
    {
        Form2 avelon = new Form2(pro);
        avelon.btn_fill_enabled = true;
    }



}

}

Upvotes: 0

Views: 100

Answers (1)

Jorge C.M
Jorge C.M

Reputation: 393

Seems that password is showing fine as it's defined at the Property class itself so, each time you create a new object of that class, it will be setted by default untill you change it.

The reason you don't see the data from Form 1 at Form 2 seems to be that you aren't passing the Property object from Form 1 to Form 2, so you have a Property object filled with data at Form 1 but at Form 2 you remain with a newly created Property object. Just modify the Form 2 constructor so it accepts a Property parameter and pass it from Form 1.

Example (this goes on your Form2's code):

public Form2 (Property form1Property){
   InitializeComponent();
   pro = form1Property;
}

That creates a code that executes each time you create a new Form2 and requires you to pass a Property object to create it (new Form2(pro); in Form1) so it assigns Form2's Property object to the one you passed when creating it's object on Form1.

Also make your Property class public, so you can use it as a parameter at Form2's constructor.

More info about constructors here

Hope this helps you!

P.S: Looking at your code I see that you're creating a global Form2 object. You should create it at btn_start_Click before you are showing it, so data is filled up correctly (when you show it your Form1's Property object is filled, now when you create it it's not filled)

Upvotes: 2

Related Questions