C# 将用户输入作为新类实例的名称

C# 将用户输入作为新类实例的名称,c#,class,oop,object,C#,Class,Oop,Object,我试图在C#中创建一个名为customer的类,包含3个变量:名称、初始存款和每月存款金额 这是一个控制台程序,它接受用户对这三个变量的输入,并不断请求更多用户,直到用户键入nothing并按enter键 但是, customer userInputName=新客户(userInputName、userInputInitial、userInputMonthly) 他给了我错误。第一个userInputName带有下划线,表示“名为‘userInputName’的本地或参数不能在此作用域中删除,因

我试图在C#中创建一个名为customer的类,包含3个变量:名称、初始存款和每月存款金额

这是一个控制台程序,它接受用户对这三个变量的输入,并不断请求更多用户,直到用户键入nothing并按enter键

但是,
customer userInputName=新客户(userInputName、userInputInitial、userInputMonthly)
他给了我错误。第一个userInputName带有下划线,表示“名为‘userInputName’的本地或参数不能在此作用域中删除,因为该名称在封闭的本地作用域中用于定义本地或参数”。第二个“userInputName”表示“参数1:无法从'lab4.Program.customer'转换为'string'”

解决这个问题的唯一方法是将第一个“userInputName”更改为customer1,但如果这样做,如果用户继续键入名称,我就无法继续创建新客户

理想情况下,我希望能够键入customer.Bob.initialDeposit之类的内容,并让程序能够告诉我Bob的初始存款是什么,等等

我怎样才能做到这一点,或者我做错了什么

using System;

namespace lab4
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("How many months will the customer keep the money in the account?");
            string monthsString = Console.ReadLine();
            int months = Int32.Parse(monthsString);

            bool run = true;
            while (run)
            {
                Console.WriteLine("Enter new customer name: ");

                string userInputName = Console.ReadLine();
                if (userInputName == "")
                {
                    run = false;
                }
                else
                {
                    Console.WriteLine("Enter initial deposit amount: ");
                    string stringInitDeposit = Console.ReadLine();
                    int userInputInitial = Int32.Parse(stringInitDeposit);

                    Console.WriteLine("Enter montly deposit amount: ");
                    string stringMonthDeposit = Console.ReadLine();
                    int userInputMonthly = Int32.Parse(stringMonthDeposit);

                    customer userInputName = new customer(userInputName, userInputInitial, userInputMonthly);
                }

            }
        }


            public class customer
            {
                public string name;
                public int initialDeposit;
                public int monthlyDeposit;

                public customer(string name, int initialDeposit, int monthlyDeposit)
                {
                    this.name = name;
                    this.initialDeposit = initialDeposit;
                    this.monthlyDeposit = monthlyDeposit;
                }
            }
    }
}
你有

string userInputName=Console.ReadLine()

customer userInputName=新客户(userInputName、userInputInitial、userInputMonthly)

您正在尝试重用相同的变量名。为客户选择一个新的变量(因为该名称无论如何都没有意义),并更新引用以使用该新变量名

如果要创建多个客户,请将新客户添加到阵列中。变量仍然可以在while循环中重用

例如:

// add this line outside (above) the while loop: (you will need to import the proper namespace for this: `using System.Collections.Generic;`)
List<Customer> newCustomers = new List<Customer>();


// I renamed this variable. add the line below to put the new customer into the list
customer newCustomer = new customer(userInputName, userInputInitial, userInputMonthly);
newCustomers.Add(newCustomer);

// now you have a list of new customers you can reference outside the while loop.
//在while循环的外部(上方)添加此行:(需要为此导入正确的命名空间:`using System.Collections.Generic;`)
List newCustomers=newlist();
//我重命名了这个变量。添加以下行以将新客户放入列表中
customer newCustomer=新客户(userInputName、userInputInitial、userInputMonthly);
newCustomers.Add(newCustomer);
//现在,您有了一个新客户列表,可以在while循环之外引用。

要获得Bob的押金,您可以选择以下方式:

using System;
using System.Collections.Generic;
using System.Linq;

(...)

var customers = new List<Customer>();

//Your logic to create customers here
var c1 = new Customer("Bob", 100, 10);
customers.Add(c1);
var c2 = new Customer("Bob", 200, 20);
customers.Add(c2);
var c3 = new Customer("Alice", 100, 10);
customers.Add(c3);


//Find all Bobs
var bobs = customers.Where(c => c.Name == "Bob");

foreach (var bob in bobs )
{
    Console.WriteLine($"Bob's initial deposit is {bob.InitialDeposit}");
}

“解决这个问题的唯一方法是将第一个‘userInputName’更改为类似customer1的内容”——不,一点也不。您还可以通过将第二个更改为
customer11
,或者更改为
userInputName
以外的任何内容来解决此问题。但是你的问题不可能给出一个好的答案,因为解决这个问题的第三种方法是完全省略第二个
userInputName
变量。你的代码从来不会对这个变量做任何事情,所以不清楚你到底期望在这里发生什么。请改进您的问题,以便清楚您真正需要什么。我编写了我想用代码执行的操作。理想情况下,我希望能够键入customer.Bob.initialDeposit之类的内容,并让程序能够告诉我Bob的初始存款是什么,等等。我希望每个新客户的名称都是变量的名称,但从下面ps2goat的响应中,我了解我的错误所在。感谢您的回答,这正是我想要的。如果我理解正确,我会将每个新客户及其所有属性(姓名、初始存款和每月存款)添加到列表中,对吗?没错。下一次循环时,会重新创建
newCustomer
变量,因此不会覆盖以前的客户数据。是的,我在阅读完整问题之前就开始回答了。现在更新。
public class Customer
{
    public Customer(string name, int initialDeposit, int monthlyDeposit)
    {
        Name = name;
        InitialDeposit = initialDeposit;
        MonthlyDeposit = monthlyDeposit;
    }

    public string Name { get; }
    public int InitialDeposit { get; }
    public int MonthlyDeposit { get; }
}