I'm trying to join 3 different values in an app.config so they correlate.
<add key="User" value="User1,User2,User3,Pass4" />
<add key="Pass" value="Pass1,Pass2,Pass3,Pass4" />
<add key="Location" value="Location1,Location2,Location3,Location4" />
var User = ConfigurationManager.AppSettings.Get("User").Split(new[] { ',' });
var Pass = ConfigurationManager.AppSettings.Get("Pass").Split(new[] { ',' });
var Location = ConfigurationManager.AppSettings.Get("Location").Split(new[] { ',' });
I'm having no trouble doing a split on the comma to get each value for each key. What I want to do is have User1 go with Pass1 and Location1. Is this something that I could easily do via a hashtable/dictionary? If so what's the easiest way?
The best way would probably be to define a class to hold them:
public class UserInfo
{
public string User { get; private set; }
public string Pass { get; private set; }
public string Location { get; private set; }
public UserInfo(string user, string pass, string location)
{
this.User = user;
this.Pass = pass;
this.Location = location;
}
}
Then a simple loop to build them:
List<UserInfo> userInfos = new List<UserInfo>();
for(int i = 0; i < User.Length; i++)
{
var newUser = new UserInfo(User[i], Pass[i], Location[i]);
userInfos.Add(newUser);
}
Then if you did want a lookup table or dictionary based on say, User
:
Dictionary<string, UserInfo> userLookup = userInfos.ToDictionary(userInfo => userInfo.User);
EDIT: You may also want to do a quick check to make sure you have the proper amount of corresponding information as well before you build your objects:
if (User.Length != Pass.Length || User.Length != Location.Length)
throw new Exception("Did not enter the same amount of User/Pass/Location data sets!");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.