【问题标题】:c# value not being stored into accessorc#值没有被存储到访问器中
【发布时间】:2015-11-13 20:32:08
【问题描述】:

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

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

        }
    }
}

用户输入类:

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);
        }
    }
}

【问题讨论】:

  • LoadingFile() 方法使用FileHandling.TxtFile,但您的用户输入在UserInput 中。
  • @mac10688 好吧,不久前我发了一个帖子,人们说我没有发布足够的代码 xD,现在我把它全部发布了,它还不够好!不能在这个网站上获胜让我发疯。让我在 UserInput 类中更详细地解释当他们输入文件名时我希望它Location.LoadFile = Location.DesktopPath + "\\" + TxtFileLoc; 来存储路径和输入的文件名,但是当 LoadingFile() 执行时,值不再存储在 Location.LoadFile 中跨度>
  • 很抱歉。但我认为递归解决了你的问题。
  • @recursive 啊好吧,所以我需要重新配置它并将用户输入放入 Driver.cs ?还是在UserInput 中调用该方法?
  • @JakeGroves 从巡回演出中不清楚(我假设您已经阅读过)。您需要足够的代码来展示您的问题。尝试删除任何不直接相关的内容。 (这意味着您必须做更多的工作来隔离问题。)

标签: c# accessor


【解决方案1】:

试试:

    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();
    }

注意:将工作方法放入类构造中是很奇怪的。像这样称呼他们可能会更好:

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

【讨论】:

  • 感谢您的回复,我明白您的意思,它进行了部分修复,但它无法识别现在输入的文件名,只有一些错误 TextFile 应该是 TxtFile(请注意,如果人们看到稍后)仅此而已,当我选择它作为桌面位置时,它仍然是当前目录位置。
  • @JakeGroves 由于您使用两个类,因此您需要负责将数据从一个类移动到另一个类。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-04
  • 2016-04-02
  • 2021-04-07
  • 1970-01-01
相关资源
最近更新 更多