如何将变量从类传递给新类?

时间:2013-03-21 09:56:12

标签: c#

我有这个类我将在Form1中为它创建一个实例,它将变量从Form1传递给类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GatherLinks
{
    class WebcrawlerConfiguration
    {

        public string url;
        public int levels;
        public string mainurl;
        public Dictionary<string,List<string>> localy;
        public bool removeext;
        public bool downloadcontent;
        public int failedUrls;
        public bool toCancel;
        public System.Threading.ManualResetEvent busy;

        public WebcrawlerConfiguration()
        {

        }
    }
}

如何让这个类从Form1获取变量? 当我为类创建实例时,类中的每个变量都应该在Form1中获取变量。

然后我有这个班:

class BackgroundWebCrawling
    {
        int counter = 0;
        List<string> WebSitesToCrawl;
        BackgroundWorker mainBackGroundWorker;
        BackgroundWorker secondryBackGroundWorker;
        WebCrawler webcrawler1;
        WebcrawlerConfiguration webcrawlerCFG;
        public BackgroundWebCrawling()
        {
            mainBackGroundWorker = new BackgroundWorker();
            mainBackGroundWorker.DoWork += mainBackGroundWorker_DoWork;
        }

        private void mainBackGroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            for (int i = 0; i < WebSitesToCrawl.Count; i++)
            {
                if (counter >= 3)
                {
                    Thread.Sleep(10);
                }
                else
                {
                    webcrawler1 = new WebCrawler(webcrawlerCFG);
                    counter++;
                    secondryBackGroundWorker = new BackgroundWorker();
                    secondryBackGroundWorker.DoWork += secondryBackGroundWorker_DoWork;
                }


            }            
        }

        private void secondryBackGroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {

                if (offlineOnline == true)
                {
                    offlinecrawling(f, levelsToCrawl, e);
                }
                else
                {
                    webcrawler1.webCrawler(
                }
                counter--;
        }

在底部我在做:webcrawler1.webCrawler( 函数webCralwer应该从第一个类WebcrawlerConfiguration获取所有变量。

这就是WebCrawler函数的样子:

public List<string> offlinecrawling(string url, int levels, string mainurl, Dictionary<string, List<string>> localy, bool removeext, bool downloadcontent, int failedUrls, bool toCancel, System.Threading.ManualResetEvent busy)

所以现在我正在做webcrawler1.webCrawler( 它要求所有变量......但我想以某种方式从WebcrawlerConfiguration传递它们。

我该怎么做?

0 个答案:

没有答案