c#从用户输入获取类名

本文关键字:获取 输入 用户 | 更新日期: 2023-09-27 18:14:28

在c#中,我如何使用用户输入或流来决定使用哪个类?本例使用Console.Readline(),但是实际的程序将根据在循环中读取的流中的数据来决定使用哪个类。这只是简化了问题的一个例子:

static void Main(string[] args)
        {
            stock aapl = new stock(); //instantiate a class for Apple Stock
            stock fb = new stock();   //instantiate a class for Facebook Stock
            Console.WriteLine("Please enter a symbol for Apple or Facebook");
            string symbol = Console.ReadLine(); //this should get the class to work on
            Console.WriteLine("Please enter yesterdays price for the symbol");
            double yestPrice = Convert.ToDouble(Console.ReadLine());
            Console.WriteLine("Please enter Todays Price for the symbol");
            double currPrice = Convert.ToDouble(Console.ReadLine());
            //Assuming aapl was entered, how do I
            //set values for the appl member using
            //the symbol variable like this:
            symbol.YesterdaysPrice = yestPrice; 
            symbol.CurrentPrice = currPrice; 
        }
        class stock
        {
            private double yesterdayPrice;
            private double currentPrice;
            private double dailyGain;
            public double YesterdaysPrice
            {
                get { return yesterdayPrice; }
                set { yesterdayPrice = value; }
            }
            public double CurrentPrice
            {
                get { return currentPrice; }
                set { currentPrice = value; }
            }
            public double DailyGain
            {
                get { return currentPrice - yesterdayPrice; }
                // No need to ever set directly
            }
        }

c#从用户输入获取类名

下面是一个使用字典解决这个问题的例子:

    static void Main(string[] args)
    {
        Dictionary<string, stock> stocks = new Dictionary<string, stock>(StringComparer.CurrentCultureIgnoreCase);
        //Add the initial stocks here if desired.
        Console.WriteLine("Please enter a symbol");
        string symbol = Console.ReadLine();
        Console.WriteLine("Please enter yesterdays price for the symbol");
        double yestPrice = Convert.ToDouble(Console.ReadLine());
        Console.WriteLine("Please enter Todays Price for the symbol");
        double currPrice = Convert.ToDouble(Console.ReadLine());
        if (stocks.ContainsKey(symbol))     //The dictionary contains the stock
        {
            stocks[symbol].YesterdaysPrice = yestPrice;
            stocks[symbol].CurrentPrice = currPrice;
        }
        else
        {
            //The stock wasn't found, we can either say invalid stock, or add one like this:
            stocks[symbol] = new stock()
            {
                YesterdaysPrice = yestPrice,
                CurrentPrice = currPrice;
            };
        }
    }

字典在顶部声明,将保存所有输入的符号。在用户输入代码和数据后,if语句检查该代码的股票是否已经存在,如果存在,则更新其值。只是一个简短的说明,我创建了一个带有StringComparer.CurrentCultureIgnoreCase参数的字典,这样用户可以输入AAPL或AAPL或AAPL,它将匹配相同的股票,默认情况下它是区分大小写的,并将为这些创建不同的股票。

如果股票不存在,您可以告诉用户这是一个无效的股票,或者您可以直接添加股票,因为您有这样做所需的所有数据。我展示了使用price属性的内联初始化器添加新股票的示例。

Ron建议这样做

        static readonly IDictionary<string, Stock> Stocks = new Dictionary<string, Stock>();
        static void Main(string[] args)
        {
            Console.WriteLine("Please enter a symbol for Apple or Facebook");
            var symbol = Console.ReadLine(); //this should get the class to work on
            Console.WriteLine("Please enter yesterdays price for the symbol");
            var yestPrice = Convert.ToDouble(Console.ReadLine());
            Console.WriteLine("Please enter Todays Price for the symbol");
            var currPrice = Convert.ToDouble(Console.ReadLine());

            Stock stock;
            if (Stocks.ContainsKey(symbol))
                stock = Stocks[symbol];
            else
            {
                stock=new Stock();
                Stocks[symbol] = stock;
            }

            stock.YesterdaysPrice = yestPrice;
            stock.CurrentPrice = currPrice;
        }