【问题标题】:Need help on a method在方法上需要帮助
【发布时间】:2013-07-25 19:11:49
【问题描述】:

我刚开始学习 c#,我正在尝试制作一个控制台应用程序,它将读取一个文本文件并在命令提示符下显示它。我也在尝试制作在单独的 dll 中读取文本文件的方法,因为我计划稍后扩展我的程序并尝试制作一种基于文本的游戏引擎。无论如何,这是我的 dll 中的代码:

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

namespace EngineFeatures
{
    public class txtedit
    {

        public string Write_txt(string textin, out String output)
        {
            try
            {
                 using (StreamReader sr = new StreamReader(textin))
                {


                    String line = sr.ReadToEnd();
                    output = line;
                    return output;
                }

            }
             catch (Exception e)
            {
                Console.WriteLine("The file could not be read:");
                Console.WriteLine(e.Message);
            }

        }
    }
}

就像我是初学者一样,我实际上是 3 天前才开始的。无论如何,我想做的是能够调用函数 EngineFeatures.txtedit.Write_txt("TXT/test.txt");在应用程序本身并让它返回一个字符串,但我仍然有点困惑,我也收到一条错误消息“EngineFeatures.txtedit.Write_txt(string, out string)':并非所有代码路径都返回一个值。” 我做错了什么?

【问题讨论】:

  • 错误信息应该清楚;如果您的 try 块中发生异常,则它不会返回任何内容,因为您在其他任何地方都没有另一个 return 语句。
  • 您的问题标题并没有真正解释您的问题是什么。您应该给出一个更具描述性的标题。

标签: c# string methods input output


【解决方案1】:

如果出现异常,您的方法不会返回任何内容。添加一些默认值以向调用者返回或抛出(另一个)异常:

catch (Exception e)
{
    Console.WriteLine("The file could not be read:");
    Console.WriteLine(e.Message);
    return null; 
    // or: return String.Empty
    // or: throw new GameLoadException("Cannot read game file", e);
}

【讨论】:

    【解决方案2】:

    您的代码中有几件事,首先您使用out 关键字传递一个变量,然后返回相同的变量。您可以去掉参数列表中的out,并在try 块中简单地返回output,但如果出现异常,您还应该返回一些值,可能是null,例如:

    编辑:您可以完全摆脱output 参数,只需返回该行。 (感谢@Jim)

    public string Write_txt(string textin)
    {
        try
        {
            using (StreamReader sr = new StreamReader(textin))
            {
                String line = sr.ReadToEnd();
                return line;
            }
        }
        catch (Exception e)
        {
            Console.WriteLine("The file could not be read:");
            Console.WriteLine(e.Message);
            return null;
        }
    }
    

    【讨论】:

    • 好答案,但在您的示例中,您可以走得更远,只需去掉第二个参数“输出”,然后返回 sr.ReadToEnd();
    • @jim,是的,但我实际上想传达的是应该从 try 和 catch 块返回一些东西。但你说得对,我要修改它。
    • 我经常声明对象在try块之外返回,而不是从try块和catch块中返回,并在catch结束后返回。所以我有 string output = string.Empty;就在尝试之前。然后我设置 output = sr.ReadToEnd();从尝试中,然后在捕获之后我会返回输出;在 catch 中,您可以将输出设置为 null,或者将其设置为 string.empty,或者抛出一个新异常,等等。但是,从 try/catch 之外返回意味着您只需要一个 return 语句,并且您不需要为每个 catch 单独一个。
    【解决方案3】:
    public class txtedit
    {
    
        public string Write_txt(string textin, out String output)
        {
            output = "";
    
            try
            {
                using (StreamReader sr = new StreamReader(textin))
                {
    
    
                    String line = sr.ReadToEnd();
                    output = line;
                    return output;
                }
    
    
            }
            catch (Exception e)
            {
                Console.WriteLine("The file could not be read:");
                Console.WriteLine(e.Message);
            }
            return output;
    
        }
    

    【讨论】:

      猜你喜欢
      • 2013-09-02
      • 2013-09-10
      • 2011-07-10
      • 2011-06-23
      • 2014-11-04
      • 1970-01-01
      • 2022-08-21
      • 1970-01-01
      相关资源
      最近更新 更多