【问题标题】:How to convert datatype in Method argument from Struct?如何从 Struct 转换 Method 参数中的数据类型?
【发布时间】:2019-07-02 11:22:08
【问题描述】:

问题

我制作了一个结构、一个枚举和一个方法。 枚举在结构中使用。该方法使用结构体。

当我尝试在 main 中接受 来自用户的输入调用方法时,我收到了转换错误。 我无法将结构体转换为双精度体。

在这种情况下如何转换用户的输入?

额外信息

(这应该是转换温度类型,现在我只写了1个温度的转换,但其他的都是一样的(除了公式))

PS:我还没有学习列表和接口,我不能在不同的课程中做到这一点。

public enum MyEnum {C,F,K}

public struct MyStruct
{
    public double foo;
    public MyEnum bar; 
}
class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Make user put in a double followed by a space and one of the Enums");
        string input = Console.ReadLine();
        string[] parts = input.Split(' '); 

        MyStruct temp = new MyStruct (); 
        temp.foo= double.Parse(parts[0]);

        if(parts[1] == "C")
        {

            TheMethodIUSedForC(parts[0]);
            temp.bar= MyEnum.C;
        }
    }
    public MyStruct TheMethodIUSedForC(MyStruct t)
    {
        if (t.bar== MyEnum.F)
        {
            t.bar= MyEnum.F;
            t.foo = (t.foo* 1.8) + 32;
            return t;
        }
        else if (t.bar== MyEnum.K)
        {
            t.bar= MyEnum.K;
            t.foo= t.foo + 273.15;
            return t;
        }
        else
        {
            return t;
        }
    }

【问题讨论】:

    标签: c# methods struct enums type-conversion


    【解决方案1】:

    我不知道我是否正确理解了您的问题,但是您可以构建您的临时结构并将其传递给这样的方法:

    public enum MyEnum {C,F,K}
    
    public struct MyStruct
    {
        public double foo;
        public MyEnum bar;
    }
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Make user put in a double followed by a space and one of the Enums");
            string input = Console.ReadLine();
            string[] parts = input.Split(' ');
    
            MyStruct temp = new MyStruct();
            temp.foo = double.Parse(parts[0]);
            temp.bar = (MyEnum)Enum.Parse(typeof(MyEnum),parts[1]);
            MyMethod(temp);
        }
        public static MyStruct MyMethod(MyStruct t)
        {
            if (t.bar == MyEnum.F)
            {
                t.bar = MyEnum.F;
                t.foo = (t.foo * 1.8) + 32;
                return t;
            }
            else if (t.bar == MyEnum.K)
            {
                t.bar = MyEnum.K;
                t.foo = t.foo + 273.15;
                return t;
            }
            else
            {
                return t;
            }
        }
    }
    

    这样你甚至不需要在“MyMethod”中重新分配“bar”变量,你只需详细说明 foo 变量。

    当然,您需要正确处理提供的字母无法解析为 MyEnum 的情况。为此,您可以使用 Enum.TryParse(...)

    【讨论】:

      【解决方案2】:

      查看您的代码并尝试编译它,我无法得到您遇到的错误。在我的情况下,错误不是关于 double 而是 string 这是正确的,因为 parts[0] 是字符串。 你的方法:

      public MyStruct TheMethodIUSedForC(MyStruct t)
      

      接受 MyStruct 作为参数,因此您不能将 parts[0] 作为参数传递,因为它是字符串类型。如果你通过例如它将编译。 temp 属于 MyStruct 类型。但是它可能无法按预期工作,因为 bar 字段尚未设置。 变化:

      if(parts[1] == "C")
      {
          TheMethodIUSedForC(parts[0]);
          temp.bar= MyEnum.C;
      }
      

      到:

      if(parts[1] == "C")
      {
          temp.bar= MyEnum.C;
          TheMethodIUSedForC(temp);
      }
      

      应该会更好。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-12-24
        • 2011-06-16
        • 2021-11-18
        • 2014-06-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多