c#值未存储到访问器中

c#值未存储到访问器中,c#,accessor,C#,Accessor,我有4个类文件驱动程序类(包含main方法) UserInput类(包含GenerateLines和TxtLoadFile方法) 类(包含LoadFile和LoadingFile方法) 加密类(目前为空类) 我的问题是我试图让用户从三个目录中选择一个文件。选择后,我希望他们输入文件名(.txt),完成后,我希望存储该值并将其带到FileHandling类,LoadFile访问器将该值存储到“TxtFile”变量中。然后我想在LoadingFile方法中使用“TxtFile”来加载文件。 问题是该

我有4个类文件驱动程序类(包含main方法) UserInput类(包含GenerateLines和TxtLoadFile方法) 类(包含LoadFile和LoadingFile方法) 加密类(目前为空类)

我的问题是我试图让用户从三个目录中选择一个文件。选择后,我希望他们输入文件名(.txt),完成后,我希望存储该值并将其带到FileHandling类,LoadFile访问器将该值存储到“TxtFile”变量中。然后我想在LoadingFile方法中使用“TxtFile”来加载文件。 问题是该值存储在UserInput类中,但当我在驱动程序类中调用: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();

        }
    }
}
用户输入类:

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;
        }
    }
}
文件处理类

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);
        }
    }
}
尝试:

注意:将辅助方法放入类构造中是非常奇怪的。最好这样称呼他们:

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

LoadingFile()
方法使用
FileHandling.TxtFile
,但是您的用户输入是在
UserInput
@mac10688中。不久前,我发了一条帖子,人们说我没有发布足够的代码xD,现在我发布了所有代码,这还不够好!在这个网站上赢不了让我发疯。让我在UserInput类中更详细地解释一下,当他们输入我想要的文件名时,
Location.LoadFile=Location.DesktopPath+“\\”+TxtFileLoc
用于存储输入的路径和文件名,但在执行LoadingFile()时,该值不再存储在Location.LoadFileSorry中。但我认为递归解决了您的问题。@recursive啊,好的,所以我需要重新配置它,并将用户输入放入
Driver.cs
?还是在
UserInput
中调用该方法?@JakeGroves从教程中看不清楚(我想您已经读过了)。您需要足够的代码来展示您的问题。尝试删除任何不直接相关的内容。(这意味着你必须做更多的腿部工作来隔离问题。)感谢你的回答,我明白你的意思,它做了部分修复,但它无法识别现在输入的文件名,只有一些错误TextFile应该是TxtFile(请注意,以防人们稍后看到)这就是全部,而且当我选择它作为桌面位置时,它会一直作为当前目录位置。@JakeGroves因为您使用的是两个类,所以您需要负责将数据从一个类移动到另一个类。
        UserInput UI = new UserInput();
        string textFile = UI.TxtLoadFile();
        //... later on...
        FH.LoadFile = textFile;