Home > database >  How can I make instance of form1 in a class and make instance of the class in form1?
How can I make instance of form1 in a class and make instance of the class in form1?

Time:10-27

In the form1 constructor :

public Form1()
        {
            InitializeComponent();

            LoadFile(textBoxRadarPath, "radarpath.txt");
            LoadFile(textBoxSatellitePath, "satellitepath.txt");

            CheckIfImagesExist();

            if (pictureBox1.Image == null || pictureBox2.Image == null)
            {
                trackBar1.Enabled = false;
            }

            tracker = new DownloadProgressTracker(50, TimeSpan.FromMilliseconds(500));
            label1.Location = new Point(progressBar1.Width / 2 - label1.Width / 2, label1.Location.Y);
            lblStatus.Text = "Download Pending";
            lblAmount.Text = "";
            lblSpeed.Text = "";
            
            sat = new Satellite();
            rad = new Radar();

I want to pass some stuff from the classes Satellite and Radar to Form1 so I make instances for the classes in Form1.

but in Form1 top I have also this List that I want to pass this List from Form1 to the classes Satellite and Radar :

public List<string> folders;

In each class I make an instance for Form1 :

namespace Extract
{
    class Satellite
    {
        private List<string> satelliteUrls = new List<string>();
        private string mainUrl = "https://de.sat24.com/de/eu/infraPolair/";
        private string[] statements;
        Form1 f1 = new Form1();

I want to use the List folders in both classes.

The problem is I'm getting exception in the classes after some time because it's doing "ping pong" it's creating instance in form1 of the class satellite then in satellite create instance of form1 then it's back to form1 make instance of the classes again and back to the classes instance of form1 and so on in loop of instances.

How can I use this List folders from Form1 in both classes ?

CodePudding user response:

If ALL that you care about in Form1 is the List called folders, then just create a similar container variable in each of the other two classes, and then set the values for those variables by passing them in from the Form1 class during the instantiation of your classes. Here's something like what the classes will look like:

class Satellite
{
    private List<string> satelliteUrls = new List<string>();
    private string mainUrl = "https://de.sat24.com/de/eu/infraPolair/";
    private string[] statements;
    public List<string> folders = null;

    Satellite (List<string>inputFolders)
    {
        folders = inputFolders;
    }
}

class Radar
{
    public List<string> folders = null;

    Radar(List<string> inputFolders)
    {
        folders = inputFolders;
    }
}

And then to create these classes, you do this in your Form1 instantiation:

public Form1()
{
    InitializeComponent();

    LoadFile(textBoxRadarPath, "radarpath.txt");
    LoadFile(textBoxSatellitePath, "satellitepath.txt");

    CheckIfImagesExist();

    if (pictureBox1.Image == null || pictureBox2.Image == null)
    {
        trackBar1.Enabled = false;
    }

    tracker = new DownloadProgressTracker(50, TimeSpan.FromMilliseconds(500));
    label1.Location = new Point(progressBar1.Width / 2 - label1.Width / 2, label1.Location.Y);
    lblStatus.Text = "Download Pending";
    lblAmount.Text = "";
    lblSpeed.Text = "";

    sat = new Satellite(folders);
    rad = new Radar(folders);
}

Thus when the form is instantiated, you're passing along that list of folders into the instantiation of the other two objects, and they will then have access to them. Does this answer your question?

  • Related