简体   繁体   English

基于变量的动态类标识符

[英]Dynamic Class identifier based on variable

I have the following function below that I'm trying to make and fill a class called User. 我在下面的函数中尝试创建并填充一个名为User的类。

I want the Class Identifier to be dynamically named for each user there is. 我希望为每个用户动态命名类标识符。

The datatable that is being passed in contains informations like below. 传递的数据表包含如下信息。

USER     | CONTROLNUMBER
"jsmith" | "789842"

I want to make a class named "JSmith" and fill the properties in that class accordingly. 我要创建一个名为“ JSmith”的类,并相应地填充该类中的属性。

I am extremely new to using classes in useful way, so be gentle. 我对以有用的方式使用类非常陌生,所以要保持柔和。

public static void FillUserListClass(DataTable dt, string _userName)
{
User _userName = new User();
_userName.ControlNumber = "789842";
_userName.UserName = "jsmith";
}

Would something like this be right for the dictionary of list idea? 像这样的清单想法字典适合吗?

static Dictionary<string, List<int>> UserList = new Dictionary<string, List<int>>();

public static void FillUserListClass(DataTable dt)
{
    for (int ctr = 0; ctr < dt.Rows.Count; ctr++)
                {
                    var row = dt.Rows[ctr];
                    var userName = row["User"].ToString();

                    if (UserList.ContainsKey(userName))
                    {
                        UserList[userName].Add(Convert.ToInt32(row["ControlNumber"]));
                    }
                    else
                    {
                        UserList.Add(row["User"].ToString(), new List<int>());
                        UserList[userName].Add(Convert.ToInt32(row["ControlNumber"]));
                    }
    }

And if so, how would i print out the dictionary so you would see something similar to 如果是这样,我将如何打印字典,以便您看到类似于

'jsmith' has 4 control numbers.

"234950"
"494602"
"494200"
"585890"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM