【问题标题】:How to return multiple variables in propertys如何在属性中返回多个变量
【发布时间】:2019-09-04 09:46:53
【问题描述】:

我想在一个属性中返回多个变量

public class Cars
{
    public object value { get; set; }
    public bool worked { get; set; }
}

public Cars GetCar(object value, Cars c)
{
   c.value = value;
   return c;
}

public void Main(string[] args)
{
   Cars c = new Cars();
   string mycar = "ABC-12";
   int mycar2 = 123;

   if (GetCar(mycar, c).value == "ABC") // working
       GetCar(mycar, c).worked = false;

   if (GetCar(mycar, c).value == 1) // error
       GetCar(mycar, c).worked = false;
}

所以如果对象是一个可以工作的字符串,但如果我使用那个 int 我会得到那个异常

== 运算符不能应用于“object”和“int”操作数

注意:我不想每次都使用 Convert.to

【问题讨论】:

  • 您已将 value 声明为 object,而不是 int。声明为int
  • 值可以是 int、string 或 ulong 如果我把它变成 int 我不能再使用 string
  • 所以如果它是一个字符串,并且它的值是“Fred”,你希望如何将它与整数值1进行比较?
  • 我想你不明白如果我使用示例GetCar(string, c) 那么值是一个字符串
  • 这看起来像an X-Y problem。为什么将value 声明为对象,而不是使用强类型?

标签: c# asp.net


【解决方案1】:

您可以使用object.Equals 方法,这会将两个操作数首先转换为object

        if (object.Equals(GetCar(mycar, c).value,"ABC")) // working
            GetCar(mycar, c).worked = false;

        if (object.Equals(GetCar(mycar2, c).value,1)) // compiles, and does not throw exception
            GetCar(mycar2, c).worked = false;

【讨论】:

  • OP完全没有线索,这就是为什么他没有接受你的答案,这是迄今为止唯一正确的答案。好笑
  • @BionicCode 他在 1 小时前发布了该消息,但是是的,这是更好的方法。谢谢
【解决方案2】:

mycar 的类型为 object。当您设置string mycar = "ABC-12" 时,您明确地将其设置为string 类型。 因此,一旦将其与 int 值进行比较,就会得到异常。

这也应该有帮助:if (<object> == <int>)

【讨论】:

    【解决方案3】:

    例如,如果你想同时使用 string 和 int,你可以创建两个值。

    public class Cars
    {
        public int intValue { get; set; }
        public string stringValue { get; set; }
        public bool worked { get; set; }
    }
    public Cars GetCar(int value, Cars c)
    {
        c.intValue = value;
        return c;
    }
    public Cars GetCar(string value, Cars c)
    {
        c.stringValue = value;
        return c;
    }
    
    public void Main(string[] args)
    {
        Cars c = new Cars();
        string mycar = "ABC-12";
        int mycar2 = 123;
    
        if (GetCar(mycar, c).stringValue == "ABC")
            GetCar(mycar, c).worked = false;
    
        if (GetCar(mycar2, c).intValue == 1)
            GetCar(mycar2, c).worked = false;
    }
    

    这会起作用,但这不是一个好的解决方案。我需要有关该项目的更多信息才能创建一个好的设计。

    【讨论】:

      【解决方案4】:
      • 工作,因为 Class Cars 是引用类型,即对象,String 也是引用类型,即对象
      • 不工作,因为 Class Cars 是引用类型,即对象,而 Int值类型,即对象

      这就是为什么

          string mycar = "ABC-12";
          int mycar2 = 123;
      
          if (GetCar(mycar).value == "ABC") // working because reference type==reference type
              GetCar(mycar).worked = false;
      
          if (GetCar(mycar).value == 1) // error because reference type=value type
              GetCar(mycar).worked = false;
      

      我认为第二个 if 条件应该是这样的

          if (GetCar(mycar2).value == 1) 
           GetCar(mycar2).worked = false;
      

      【讨论】:

        【解决方案5】:

        我猜你只是缺少一个演员表。另一方面,我必须质疑您的代码-您要实现什么目标?你所做的事情对我来说意义不大。

            public class Cars
            {
                public object value { get; set; }
                public bool worked { get; set; }
            }
        
            public Cars GetCar(object value)
            {
                Cars c = new Cars();
                c.value = value;
                return c;
            }
        
            public void Main(string[] args)
            {
                string mycar = "ABC-12";
                int mycar2 = 123;
        
                if ((string)GetCar(mycar).value == "ABC")
                    GetCar(mycar).worked = false;
        
                if ((int)GetCar(mycar2).value == 1) // changed mycar to mycar2 and 
                                                    // added a cast to int
                    GetCar(mycar2).worked = false;
            }
        

        【讨论】:

        • 如果value是一个字符串会抛出异常。
        • 本例中的值为 int。
        • 如果不是 int 怎么办?如果它总是一个int,为什么要声明为object
        • 我最好的猜测是,这是由于对 OP 的主题缺乏了解。由于问题的编写和格式都不好,我试图以第一种方式对 OP 有所帮助。我完全明白你的意思,这当然是正确的,但它对 OP 也有用吗?
        • 无论如何@MatějŠtáglthanxs 我会以这种方式使用它
        【解决方案6】:

        您可以尝试像这样编辑测试:

        object tmp = GetCar(mycar2,2).value;
        if ((tmp as int?) != null && (int)tmp == 1)
                    GetCar(mycar2,2).worked = false;
        

        现在,只有当您的项目实际上是整数时,您才进行强制转换。

        如果你喜欢在一行代码中完成:

        int? temp2 = 0;
        if ((temp2 = (GetCar(mycar2,2).value as int?)).HasValue && temp2 == 1)
            GetCar(mycar2,2).worked = false;
        

        L-

        【讨论】:

        • 代码太多了,我只想用GetCar(mycar, c).value == (string) or (int) or (bool)
        猜你喜欢
        • 2022-01-06
        • 2022-11-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多