c#值未存储到存取器中

时间:2015-11-13 20:32:08

标签: c# accessor

我有4个类文件Driver类(包含main方法) UserInput类(包含GenerateLines& TxtLoadFile方法) FileHandling.class(包含LoadFile和LoadingFile方法) 加密课程(目前是空课)

我的问题是我试图让用户选择三个目录中的哪个目录来选择文件。一旦选择,我希望他们输入文件名(.txt),完成后我希望存储该值并将其带到FileHandling类,LoadFile访问器将值存储到' TxtFile'变量。然后我想使用' TxtFile'在LoadingFile方法中加载文件。 问题是在UserInput类中存储的值,但是当我调用:Driver类中的LoadingFile方法时,它会删除该值。

请记住,我是学生仍在学习C#,所以可能不是最好的课程,因为我只是在练习作业。

编辑:我忘了提到我通过调试器检查了这个,但我无法解决如何修复它

驱动程序类:

namespace UniAssignVigereneCipher
{
    class Driver
    {
        static void Main(string[] args)
        {
            UserInput UI = new UserInput();
            Crypting CR = new Crypting();
            FileHandling FH = new FileHandling();

            Console.WriteLine(UI.test);
            Console.WriteLine(UI.test2);
            FH.LoadingFile();

        }
    }
}

UserInput类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace UniAssignVigereneCipher
{
    class UserInput
    {
        public string test = GenerateLines();
        public string test2 = TxtLoadFile();

        public static string GenerateLines()
        {
            string input;
            Console.WriteLine("Would you like to encrypt/decrypt a .txt(t) file  or a piece text string(s)?\r\nPlease type (t) or (s)");
            input = Console.ReadLine();

            switch (input)
            {
                case "t":
                case "T":
                    Console.WriteLine("You have selected a txt file.");
                    break;
                case "s":
                case "S":
                    Console.WriteLine("You have selected to input your own text string.");
                    break;
                default:
                    break;
            }

            return input;

        }
        public static string TxtLoadFile()
        {
            FileHandling Location = new FileHandling();
            int n;
            string FileInput;
            string TxtFileLoc;
            Console.WriteLine("Please choose the location of the .txt file you would like to load from.\r\n1 (Current Location): {0} \r\n2 (Desktop): {1} \r\n3 (My Documents): {2}",  Location.CurrentDir, Location.DesktopPath,Location.DocumentsPath);
            FileInput = Console.ReadLine();
            bool test = int.TryParse(FileInput, out n);

            switch (n)
            {
                case 1:
                    Console.WriteLine("File Location: {0} \r\nName of file to load: ", Location.CurrentDir);
                    TxtFileLoc = Console.ReadLine();
                    Location.LoadFile = Location.DesktopPath + "\\" + TxtFileLoc;
                    break;
                case 2:
                    Console.WriteLine("File Location: {0} \r\nName of file to load: ", Location.DesktopPath);
                    TxtFileLoc = Console.ReadLine();
                    Location.LoadFile = Location.DesktopPath + "\\" + TxtFileLoc;
                    break;
                case 3:
                    Console.WriteLine("File Location: {0} \r\nPName of file to load: ", Location.DocumentsPath);
                    TxtFileLoc = Console.ReadLine();
                    Location.LoadFile = Location.DocumentsPath + "\\" + TxtFileLoc;
                    break;
                default:
                    break;
            }         
            return FileInput;
        }
    }
}

FileHandling类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace UniAssignVigereneCipher
{
    class FileHandling
    {
        public string DesktopPath = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
        public string DocumentsPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
        public string CurrentDir = Environment.CurrentDirectory;



        string TxtFile;

        public string LoadFile
        {
            get
            {
                return TxtFile;
            }
            set
            {
                TxtFile = value;
            }
        }

        public void LoadingFile()
        {
            Console.WriteLine("name:" + TxtFile);
            StreamReader LF = new StreamReader(TxtFile);
            string FileContent = LF.ReadLine();
            Console.WriteLine(FileContent);
        }
    }
}

1 个答案:

答案 0 :(得分:2)

尝试:

    static void Main(string[] args)
    {
        UserInput UI = new UserInput();
        Crypting CR = new Crypting();
        FileHandling FH = new FileHandling();

        Console.WriteLine(UI.test);
        Console.WriteLine(UI.test2);
        FH.LoadFile = UI.test2;
        FH.LoadingFile();
    }

注意:将您的worker方法放入类构造中是很奇怪的。将它们称为这样可能会更好:

        UserInput UI = new UserInput();
        string textFile = UI.TxtLoadFile();
        //... later on...
        FH.LoadFile = textFile;