如何使用从form1到新类文件的变量和方法?

时间:2017-02-01 17:31:32

标签: c# .net winforms

我在form1顶部:

private Dictionary<string, string> codeToFullNameMap;
private string mainPath = "Countries";

然后在form1构造函数

ManageDirectories();

然后是ManageDirectories

private void ManageDirectories()
        {
            string savedImagesPath = Path.GetDirectoryName(Application.LocalUserAppDataPath);
            mainPath = Path.Combine(savedImagesPath, mainPath);
            string[] lines = File.ReadAllLines("CountriesNames.txt");
            foreach (string path in lines)
            {
                string countryPath = Path.Combine(mainPath, path);
                if (!Directory.Exists(countryPath))
                {
                    Directory.CreateDirectory(countryPath);
                }
                countries.Add(path);
            }

            string[] countriesCodes = File.ReadAllLines("CountriesCodes.txt");
            foreach (string code in countriesCodes)
            {
                codes.Add(code);
            }

            codeToFullNameMap = codes
                .Select((code, index) => index)
                .ToDictionary(
                              keySelector: index => codes[index],
                              elementSelector: index => countries[index]);
        }

现在在课程文件

var startIndex = file.Name.IndexOf("region=") + "region=".Length;
var length = file.Name.IndexOf("&", startIndex) - startIndex;
var code = file.Name.Substring(startIndex, length);
var countryName = codeToFullNameMap[code];

string countryPath = Path.Combine(mainPath, countryName);

但是在我创建的类文件中,codeToFullNameMap和mainPath都不存在。

如果代码在新类中的form1中的ManageDirectories中执行任何操作而不创建form1的新实例,我如何使用它们?或者在课堂上使用它们的最佳方法是什么?我也可以将它们公之于众。

0 个答案:

没有答案