【问题标题】:Returning two strings in a function in C# [duplicate]在C#中的函数中返回两个字符串[重复]
【发布时间】:2014-06-27 08:07:30
【问题描述】:

考虑:

protected string Active_Frozen(string text, string color)
{
    connection();
    string query = "SELECT CustomerInfo FROM ActiveSubscription WHERE UserName=@UserName";

    SqlCommand cmd = new SqlCommand(query, conn);

    if(query=="true")
    {
        text = "Active";
        color = "Green";
    }
    else
    {
        text = "Frozen";
        color= "Red";
    }

    return (text, color);
}

我想返回两个字符串:文本和颜色,但我不确定问题出在哪里。

错误@return 语句:

(参数)?文字/颜色

无法将 lambda 表达式转换为类型“字符串”,因为它不是委托类型

【问题讨论】:

  • 将属性TextColor封装在一个类中,并返回该类的一个实例。您描述的语法在 C# 中不可用。
  • 您可以返回一个Tuple<string, string> 或(最好)只滚动一个包含两个字符串作为属性的简单类并返回它。编辑:或使用outref 参数,但使用起来可能有点痛苦。

标签: c# return


【解决方案1】:

有多种方法可以实现:

  1. 数组

    protected string[] Active_Frozen(string text, string color)
    {
        string [] returnVal=new string[2];
        returnVal[0] = text;
        returnVal[1] = color;
        return returnVal;
    }
    
  2. 结构体(类似于类对象)

    struct myReturnValues
    {
        public string text;
        public string color;
    }
    
    protected myReturnValues Active_Frozen(string text, string color)
    {
        myReturnValues returnVal = new myReturnValues();
        returnVal.text = text;
        returnVal.color = color;
        return returnVal;
    }
    
  3. 输出参数

    protected myReturnValues Active_Frozen(out string text, out string color)
    {
        text="new valuess";
        color= "new color";
    }
    

【讨论】:

    【解决方案2】:

    我认为你可以使用一个字符串列表,你可以返回所有你想要的值:

    protected list<string> Active_Frozen(string text, string color)
    {
        connection();
        string query = "SELECT CustomerInfo FROM ActiveSubscription WHERE UserName=@UserName";
    
        SqlCommand cmd = new SqlCommand(query, conn);
    
        if(query=="true")
        {
            text = "Active";
            color = "Green";
        }
        else
        {
            text = "Frozen";
            color= "Red";
        }
        list<string> liststring = new list<string> {text, color};
        return liststring;
    

    }

    【讨论】:

      【解决方案3】:

      你必须返回一个数组、一个 Tuple 甚至是一个 List(谁知道呢,有一天你可能需要返回更多的项目)。

      protected string[] Active_Frozen(string text, string color)
      {
          // Code code
          return new string[] {text, color};
      }
      
      protected Tuple<string, string> Active_Frozen(string text, string color)
      {
          // Code code
          return new Tuple<string, string>(text, color);
      }
      
      protected List<string> Active_Frozen(string text, string color)
      {
          List toReturn = new List<string>();
      
          // Code code
      
          toReturn.Add(text);
          toReturn.Add(color);
          return toReturn;
      }
      

      你可以把热量调高并返回一个 Hashtable,或者使用 out 参数。

      protected Hashtable Active_Frozen(string text, string color)
      {
          // Code code
          Hashtable values = new Hashtable();
      
          if(query=="true")
          {
             values.Add("text", "Active");
             values.Add("color", "Green";
          }
          else
          {
              // etc.
          }
      
          return values;
      }
      
      protected void Active_Frozen(out string text, out string color)
      {
          // This way, whenever you modify text or color, you will modify the actual instances you passed to Active_Frozen instead of copies
          // and code code
      }
      

      【讨论】:

        【解决方案4】:

        创建一个类并从方法中返回一个类对象:

        public class Container
        {
            public string text {get;set;}
            public string color{get;set;}
        }
        

        方法:

        protected Container Active_Frozen(string text, string color)
        {
            connection();
        
            string query = "SELECT CustomerInfo FROM ActiveSubscription WHERE UserName=@UserName";
        
            SqlCommand cmd = new SqlCommand(query, conn);
        
        
            if(query=="true")
            {
                Container c = new Container{text = "Frozen", color= "Red"};
            }
        
            else
            {
                Container c = new Container{text = "Frozen", color= "Red"};
            }
        
            return c;
        }
        

        【讨论】:

          【解决方案5】:

          当你返回 two 东西时,你需要将你的函数声明为返回 two 东西。但是,您的函数被声明为返回 one 字符串。

          修复它的一种方法是使用Tuple&lt;T1,T2&gt;

          Tuple<string,string> Active_Frozen(string text, string color) {
              ...
              return Tuple.Create(text, color);
          }
          

          请注意,返回颜色名称而不是颜色对象本身可能并不理想,具体取决于您设计中返回值的用途。如果您希望返回颜色的对象表示而不是字符串,请更改 Tuple 的第二个类型参数,或创建您自己的表示文本及其颜色的类。

          【讨论】:

          • "你需要将你的函数声明为声明两件事" mah bwains hutz dood D:
          【解决方案6】:

          可以使用out参数:

          protected string Active_Frozen(out string text, out string color)
          {
              connection();
          
              string query = "SELECT CustomerInfo FROM ActiveSubscription WHERE UserName=@UserName";
          
              SqlCommand cmd = new SqlCommand(query, conn);
          
          
              if(query=="true")
              {
                 text = "Active";
                 color = "Green";
              }
          
              else
              {
                 text = "Frozen";
                 color= "Red";
              }
          }
          

          这样称呼它:

          string text;
          string color;
          
          Active_Frozen(out text, out color);
          

          【讨论】:

            猜你喜欢
            • 2013-05-06
            • 2016-07-13
            • 2023-03-17
            • 2017-05-30
            • 2011-04-17
            • 2021-10-12
            • 1970-01-01
            • 2013-02-28
            • 2020-07-05
            相关资源
            最近更新 更多