简体   繁体   English

如何在类中创建 form1 的实例并在 form1 中创建类的实例?

[英]How can I make instance of form1 in a class and make instance of the class in form1?

In the form1 constructor :在 form1 构造函数中:

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.我想将类 Satellite 和 Radar 中的一些东西传递给 Form1,所以我为 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 :但在 Form1 顶部我也有这个列表,我想将此列表从 Form1 传递给类 Satellite 和 Radar :

public List<string> folders;

In each class I make an instance for Form1 :在每个类中,我都为 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.我想在两个类中都使用 List 文件夹。

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.问题是一段时间后我在类中遇到异常,因为它正在执行“乒乓”它在类卫星的form1中创建实例然后在卫星中创建form1的实例然后它返回到form1再次创建类的实例到 form1 的类实例等在实例循环中。

How can I use this List folders from Form1 in both classes ?如何在两个类中使用 Form1 中的这个 List 文件夹?

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.如果您在 Form1 中关心的只是名为文件夹的列表,那么只需在其他两个类中的每个类中创建一个类似的容器变量,然后通过在实例化期间从 Form1 类中传递这些变量来设置这些变量的值类。 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:然后要创建这些类,请在 Form1 实例化中执行此操作:

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?这回答了你的问题了吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM