子类不能从父类访问数据

时间:2016-03-06 02:38:54

标签: c# class inheritance

我已经获得了以下代码,其中我定义了要在类及其变量中完成的一些操作:

namespace PPF_Converter_v10
{

    public class ProgramStuff
    {
        protected List<String> OpenedFiles { get; private set; }
        protected List<String> ValidFiles { get; private set; }
        protected List<String> InvalidFiles { get; private set; }
        protected List<String> FileData { get; private set; }
        protected string FileContents { get; private set; }
        public ProgramStuff()
        {
            OpenedFiles = new List<string>();
            ValidFiles = new List<string>();
            InvalidFiles = new List<string>();
            FileData = new List<string>();
            FileContents = string.Empty;
        }


    public void SelectFiles()
        {
            using (var FileSelect = new OpenFileDialog())
            {
                FileSelect.Multiselect = true;
                FileSelect.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyComputer);
                FileSelect.Filter = "PPF Files (*.ppf)|*.ppf|CIP Files (*.cip)|*.cip";
                FileSelect.Title = "Seclect a PPF or CIP File";
                DialogResult dr = FileSelect.ShowDialog();
                if (dr == DialogResult.OK)
                {
                    foreach(var File in FileSelect.FileNames)
                    {
                        OpenedFiles.Add(File);
                    }
                }
            }
        }
        public void ReadFiles()
        {
            foreach(var File in OpenedFiles)
            {
                using (var fs = new FileStream(File, FileMode.Open, FileAccess.ReadWrite, FileShare.None))
                {

                    FileContents = string.Empty;
                    var len = (int)fs.Length;
                    var bits = new byte[len];
                    fs.Read(bits, 0, len);
                    // Dump 1024 bytes per line
                    for (int ix = 0; ix < len; ix += 1024)
                    {
                        //drawTextProgressBar(ix, (int)fs.Length);

                        var cnt = Math.Min(1024, len - ix);
                        var line = new byte[cnt];
                        Array.Copy(bits, ix, line, 0, cnt);

                        // Convert non-ascii characters to .
                        for (int jx = 0; jx < cnt; ++jx)
                            if (line[jx] < 0x20 || line[jx] > 0x7f) line[jx] = (byte)'.';
                        //Creating a big string with output
                        FileContents += Encoding.ASCII.GetString(line);
                    }
                    FileData.Add(FileContents);
                }
            }
        }
        public void FileDefiniton()
        {
            foreach(var File in FileData)
            {

                bool b = File.Contains("/HDMZoneCoverageValue") && File.Contains("/CIP3AdmInkColors");
                if(b)
                {
                    ValidFiles.Add(File);
                }
                else
                {
                    InvalidFiles.Add(File);
                }
            }

        }
        public string XMLOutputFolder()
        {
            string XMLOutput = string.Empty;
            using (var XMLOut = new FolderBrowserDialog())
            {
                XMLOut.ShowNewFolderButton = true;
                XMLOut.RootFolder = Environment.SpecialFolder.MyComputer;
                DialogResult dr = XMLOut.ShowDialog();
                if(dr == DialogResult.OK)
                {
                    XMLOutput = XMLOut.SelectedPath;
                }
                return XMLOutput;
            }
        }
        public void ConvertedPPFFolder(string ConvertedPPF)
        {
            using (var ConvFolder = new FolderBrowserDialog())
            {
                ConvFolder.ShowNewFolderButton = true;
                ConvFolder.RootFolder = Environment.SpecialFolder.MyComputer;
                DialogResult dr = ConvFolder.ShowDialog();
                if (dr == DialogResult.OK)
                {
                    ConvertedPPF = ConvFolder.SelectedPath;
                }
            }
        }


    }//Closing class ProgramStuff
    //Creating a child class called FileManipulation - manipulate files
    public class FileManipulation: ProgramStuff
    {
        protected string PPFColors;
        protected string[] ColorsNames;

        public void ColorExtraction()
        {
            MessageBox.Show(ValidFiles.Count.ToString());

            foreach (var data in ValidFiles)
            {
                Regex ColorNameRegex = new Regex("CIP3AdmSeparationNames(.*)CIP3AdmPSExtent");
                var RegexAux = ColorNameRegex.Match(data);
                PPFColors = RegexAux.Groups[1].ToString();
                PPFColors = PPFColors.Replace("] def./", "").Replace("[", "").Replace(" (", "(").Replace("(", "").Replace(")", "|");
                PPFColors = PPFColors.Remove(PPFColors.Length - 1, 1);
                ColorsNames = PPFColors.Split(new[] { "|" }, StringSplitOptions.RemoveEmptyEntries);

            }
        }
    }


}

然后,我有我的表单声明,我在其中实例化并使用它们:

   public partial class Form1 : Form
    {
        private FileManipulation FileOp;
        private ProgramStuff GetFiles;

        public Form1()
        {
            InitializeComponent();
            FileOp = new FileManipulation();
            GetFiles = new ProgramStuff();
        }       
        private void button1_Click(object sender, EventArgs e)
        {           
            GetFiles.SelectFiles();
            GetFiles.ReadFiles();
            GetFiles.FileDefiniton();
        }

问题是:我可以使用实例化的类ProgramStuff(称为GetFiles)完成我需要的所有操作。但是,就在这里,当我从子类中调用一个方法时:

private void button5_Click(object sender, EventArgs e)
{
    FileOp.ColorExtraction();
}

我无法访问父类中存储的数据。调试时,名为ValidFiles的List有0个元素;并且在父类中添加了元素。我有办法访问这些元素吗?这是我的问题的主要观点。

谢谢!

1 个答案:

答案 0 :(得分:1)

我认为您遇到的问题是您正在实例化Child和Parent Class:             FileOp = new FileManipulation();             GetFiles = new ProgramStuff();

并且您正在尝试使用存储在两个不同对象中的数据。

在我看来,你只需要实例化Child Class:             FileOp = new FileManipulation();

然后,您必须在代码调用child和parents方法上使用FileOp。

我希望它有所帮助。