如何在C#中使用变量值作为类的名称?

huangapple go评论109阅读模式
英文:

How to use a variable value as the name of a class in c#?

问题

我尝试创建一种测试控制台应用程序,其中用户可以无限制地创建具有唯一名称的员工。由于这个要求,我希望正在创建的类的名称与员工的名称相同。我有一个变量,其中收集了员工的名称,但我无法使类的名称等于变量的值。我明白这是非常粗糙的代码,但我只是想在这个问题上得到帮助。

我尝试使用变量名称,但显然会引发错误。我也查看了互联网,但找不到任何可以帮助的内容。以下是到目前为止我的代码:

using System;

public class Employee
{
  public string firstName;
  public string lastName;
  public string emailAddress;
  public int age;

  public Employee(string name1, string name2, string email, int age1)
  {
    firstName = name1;
    lastName = name2;
    emailAddress = email;
    age = age1;
  }

  public void ListEmployeeDetails()
  {
    Console.WriteLine("--员工详细信息--");
    Console.WriteLine("~~~~~~~~~~~~~~~~~~~~");
    Console.WriteLine($"姓名: {firstName} {lastName}");
    Console.WriteLine($"电子邮件: {emailAddress}");
    Console.WriteLine($"年龄: {age}");
  }
}

public class Program
{
  public static void Main(string[] args)
  {
    // 设置
    string decision;
    string firstName;
    string lastName;
    string emailAddress;
    int age;
    
    bool next = true;
    Random rnd = new Random();
    int num = rnd.Next(1, 201);

    // 说明
    Console.WriteLine("欢迎使用员工控制台模拟器。\n您想创建新员工、删除员工还是查看员工的工作时间?\n按 'a' 创建新员工,按 'b' 删除员工,按 'c' 查看员工的工作时间。\n按 'e' 退出。");

    while (next == true)
    {
      decision = Console.ReadLine().ToUpper();
      switch (decision)
      {
      case "A":
        // 创建新员工
        Console.WriteLine("输入新员工的名字:");
        firstName = Console.ReadLine();
        Console.WriteLine("输入新员工的姓氏:");
        lastName = Console.ReadLine();
        Console.WriteLine("输入新员工的电子邮件地址:");
        emailAddress = Console.ReadLine();
        Console.WriteLine("输入新员工的年龄:");
        age = Convert.ToInt32(Console.ReadLine());

        Employee **{firstName + lastName}** = new Employee(firstName, lastName, emailAddress, age);
        break;

      case "B":
        break;

      case "C":
        break;

      case "E":
        break;

      default:
        Console.WriteLine("这不是一个有效的命令。请重新输入。");
        break;
      }
    }
  }
}

在上述代码中,我突出了需要更改的部分,以使类的名称等于员工的名称。

英文:

I am trying to create some sort of test console application where I will be able to apply at least some of the code into a real application. I want the user to be able to create employees indefinitely with unique names. Because of this requirement, I want the name of the class that I am creating to be the name of the employee. I have a variable where I have collected the name of the employee but I can't make it so that the name of the class is equal to the value of the variable. I understand it is very crude code but I just want help with this issue.

I tried using the variable name but it obviously threw an error. I checked the internet as well but I could not find anything that could help. Here is my code so far:

using System;
public class Employee
{
public string firstName;
public string lastName;
public string emailAddress;
public int age;
public Employee(string name1, string name2, string email, int age1)
{
firstName = name1;
lastName = name2;
emailAddress = email;
age = age1;
}
public void ListEmployeeDetails()
{
Console.WriteLine("--Employee details--");
Console.WriteLine("~~~~~~~~~~~~~~~~~~~~");
Console.WriteLine($"Name: {firstName} {lastName}");
Console.WriteLine($"Email: {emailAddress}");
Console.WriteLine($"Age: {age}");
}
}
public class Program
{
public static void Main(string[] args)
{
// Setup
string decision;
string firstName;
string lastName;
string emailAddress;
int age;
bool next = true;
Random rnd = new Random();
int num = rnd.Next(1, 201);
// Explanation
Console.WriteLine("Welcome to Employee console simulator. \nWould you like to create a new employee, remove an employee, or check an employee's work hours?\nPress 'a' to create new employee, 'b' to remove an employee, and 'c' to check an employee's work hours.\nPress 'e' to exit.");
while (next == true)
{
decision = Console.ReadLine().ToUpper();
switch (decision)
{
case "A":
// Create new employee
Console.WriteLine("Enter new employee's first name:");
firstName = Console.ReadLine();
Console.WriteLine("Enter new employee's last name:");
lastName = Console.ReadLine();
Console.WriteLine("Enter new employee's email address:");
emailAddress = Console.ReadLine();
Console.WriteLine("Enter new employee's age:");
age = Convert.ToInt32(Console.ReadLine());
Employee **{firstName + lastName}** = new Employee(firstName, lastName, emailAddress, age);
break;
case "B":
break;
case "C":
break;
case "E":
break;
default:
Console.WriteLine("That is not a valid command. Please enter again.");
break;
}
}
}
}

答案1

得分: 4

看起来你需要熟悉一下 .NET 中的集合。在这种情况下,字典 (Dictionary<string, Employee>) 似乎是一个很好的选择:

var employees = new Dictionary<string, Employee>();
while (next == true)
{
    decision = Console.ReadLine().ToUpper();
    switch (decision)
    {
        case "A":
            var fullName = firstName + lastName;
            if (employees.ContainsKey(fullName)) // 检查用户是否已存在
            {
                // 待办事项
            }
            else
            {
                // 如果不存在 - 添加新用户
                employees.Add(fullName, new Employee ..);
            }
            
            break;
        // ...
    }
}        
英文:

It seems that you need to get acquainted with collections in .NET. In this case dictionary (Dictionary<string, Employee>) seems to be a good candidate:

var employees = new Dictionary<string, Employee>();
while (next == true)
{
decision = Console.ReadLine().ToUpper();
switch (decision)
{
case "A":
var fullName = firstName + lastName;
if (employees.ContainsKey(fullName)) // check if user already is present
{
// TODO
}
else
{
// if not - add new one
employees.Add(fullName, new Employee ..);
}
break;
// ...
}
}        

huangapple
  • 本文由 发表于 2023年8月10日 23:58:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76877451.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定