-2

I'm not sure of the wording of what I'm looking for so I apologize if this has been answered since I'm new to C#.

What I'm trying to do is create multiple dynamically-named Lists based off of "i".

A code snippet would like this:

List<string> infoForUserSessions = new List<string>();

// Code that adds data to infoForUserSessions

for (int i = 0; i < infoForUserSessions.Count; i++){

// I want to initialize multiple List variables based off of how many users were found in my "infoForUserSessions" List.

  List<string> user[i];
}

I was hoping it would create new Lists named:

user1

user2

user3

etc.

Update Sorry all for being so confusing. You guys swarmed with answers! Let me be more specific. I'm practicing string output from a Console application such as using "PsExec \localhost qwinsta". The output would look like this:

 SESSIONNAME       USERNAME                 ID  STATE   TYPE        DEVICE
 services                                    0  Disc
>console           mariob                    1  Active
 rdp-tcp                                 65536  Listen

Each line is stored in the List "infoForUserSessions" so the data looks like:

infoForUserSessions[0] = services                                    0  Disc
infoForUserSessions[1] = >console           mariob                    1  Active
infoForUserSessions[2] = rdp-tcp                                 65536  Listen

I then have code to pick out the important text out of each array index:

string[] tempStringArray;
List<string> allUsersAndIDs = new List<string>();
char[] delimiters = new char[] { ' ' };

foreach (string line in infoForUserSessions)
{
    tempStringArray = line.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);

    for (int i = 0; i < tempStringArray.Length; i++)
    {
      // This is where I was thinking of some logic to create a new array for each user so I could store the separate parts of a string into this new array

      // Something like (which would be from the Lists above in my original message--this is based off of how many users were stored in the original infoForUserSessions List:
      // user1.Add(i);
    }
}

I'm still working out the logic but I figured I wanted the output to be something dynamic based off of two factors:

  1. How many "users" (strings) were stored in the List infoForUserSessions
  2. Individual user arrays/Lists that have their own index values of:

.

user1[0] = "services";
user1[1] = "0";
user1[2] = "Disc";

user2[0] = ">console";
user2[1] = "mariob";
user2[2] = "1";
user2[2] = "Active";
Mario
  • 23
  • 2
  • 5
  • 2
    What are you really trying to achieve? you can have a `List>` but i'd imagine theres a bigger question here.. – Sayse Mar 31 '14 at 18:05
  • 1
    It sort of seems like you just want a two-dimensional list, in which case, you would just use a List>. – neminem Mar 31 '14 at 18:05
  • If you know what number user you are trying to access, what would having explicit individual variables achieve that `users[i]` wouldn't? – selkathguy Mar 31 '14 at 18:05
  • This question comes up about every other day... But no one so far explained what you try to do with these " dynamically named variables". Could you try to be the first one and show code how you plan to *use* these variables? (otherwise it is dup of something like http://stackoverflow.com/questions/5033675/how-do-i-name-variables-dynamically-in-c?rq=1) – Alexei Levenkov Mar 31 '14 at 18:05
  • 1
    Or, maybe, a `Map>`. Or better yet, a `Map` or similar domain object, mapping a username to some domain model – PatrikAkerstrand Mar 31 '14 at 18:05
  • @AlexeiLevenkov, I updated my original question to fill in some blanks as to why I'm doing this. – Mario Mar 31 '14 at 18:50
  • 1
    Indeed after your edit this look like duplicate of many "for some reason I want to have uniquely named variables when I should use an array" question. You have good answers here and in duplicates. Side note: please for your own sanity use classes that describe data of each row instead of arrays/dictionaries where particular index mean some particular value. – Alexei Levenkov Mar 31 '14 at 18:58
  • @AlexeiLevenkov, I'm not sure I full understand what you mean by that. If it's simple to provide a code snippet I would love to learn :) – Mario Mar 31 '14 at 19:00
  • `class ServiceStatus { public string SessionName{get;set;} ...}` and than have list/dictionary of them `List...` – Alexei Levenkov Mar 31 '14 at 19:09

2 Answers2

5

Don't try to use dynamically named variables. That's simply not how variables work in C#.

Use an array of lists:

List<string>[] user = new List<string>[infoForUserSessions.Count];

for (int i = 0; i < infoForUserSessions.Count; i++) {
  user[i] = new List<string>();
}

If the number of sessions can change, you would use a List<List<string>> instead, so that you can add lists to it when you add items to the infoForUserSessions list.

Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • if you are not convenient with array you can use : List> – Anthony Raymond Mar 31 '14 at 18:58
  • Although I don't get a "dynamically named" variable (and that's probably best after reading Guffa's comment), I am able to create a new List based off of the length of infoForUserSessions.Count. This is perfect because then I can call on each List (user[0], user[1]) to get its values. I didn't know you could do List inception :) – Mario Mar 31 '14 at 20:27
1

You can use Dictionary<String, List<string>>

Dictionary associate a key to a value, like an physical Dictionary book.

Dictionary<String, List<string>> myDict = new Dictionary<String, List<string>>();

for (int i = 0; i < infoForUserSessions.Count; ++i){
    myDict.add("user" + i, new List<string>());
}

Here is an exemple how to use Dictionary :

Dictionary<String, String> myDict = new Dictionary<String, String>();
//Line below will add both KEY and a VALUE to the dictionary, BOTH are linked one to eachother
myDict.add("apple", "Apple is a brand");

//This above line return "Apple is a brand"
myDict["apple"];
Anthony Raymond
  • 7,434
  • 6
  • 42
  • 59
  • Unless you REALLY need this 'dynamically named variable' you might not use this principe, @guffa 's solutions is way better. – Anthony Raymond Mar 31 '14 at 18:57
  • 1
    Thanks for the answer Anthony. I tried using this and am a little unfamiliar with Dictionaries but could see how this would work. Ultimately I went with Guffa's code as it made sense to my newbie coding skills at the moment and with what I was looking to achieve. – Mario Mar 31 '14 at 20:29
  • I want to chime back in here and say that I did end up using a Dictionary as well. Took me a bit to figure it out but I understand it and it's pretty cool, so, thank you Anthony :) I'd up-vote it but I don't have 15 rep :( – Mario Apr 01 '14 at 17:55
  • Come back latter then ^^, i'll add an explanation to Dictionary in my post – Anthony Raymond Apr 01 '14 at 17:57