1

I am working on a C# WPF application. Here is a sample of my code:

private static List<string> a = new List<string>();
private static List<string> b = new List<string>();
private static List<string> c = new List<string>();

Then I have another list like this:

private static List<List<string>> alphabet = new List<List<string>>() { a, b, c };

I need to access a, b, or c as a string. so I need to get the name of the items inside alphabet list. It seems that list does not have any name property.

Mike Debela
  • 1,930
  • 3
  • 18
  • 32
  • Would you please show the line where you are attempting to access the string values in `a`, `b`, and `c`, and received the error? Also, and this may be due to the limited code snippets, but why a list of a list? – gravity Jun 01 '16 at 14:25
  • 1
    Do you want to combine all the strings from all your lists into one single string? Then `String.Join` is your friend. – MakePeaceGreatAgain Jun 01 '16 at 14:25
  • 3
    `a`, `b` and `c` are _variables_. Variables _contain a reference_ to your `List` instances. But the names of those variables are _no property_ of the instances. Imagine a `var d = a;` Would the "name" of the list now be "a" or "d"? Your mixing something up here... – René Vogt Jun 01 '16 at 14:25
  • 2
    Variable names are just for reading code. If you need to associate a name with your list then either create a custom class or use `Tuple>`. – juharr Jun 01 '16 at 14:26
  • I would recommend using a dictionary for this, that way you can access the variable a but a name, if that is really what you intend to do – theDarse Jun 01 '16 at 14:33
  • I don't think that is a duplicate. I don't think OP is asking about property names. – paparazzo Jun 01 '16 at 14:42
  • But the items in alphabet do not have a name. You just have a List. – paparazzo Jun 01 '16 at 14:49
  • @gravity: sqlSelect = "SELECT * FROM " + a + ";"; –  Jun 01 '16 at 15:14
  • @ HimBromBeere: My question has nothing to do with that. I don't want to access or work with the data inside a,b, or c at this point. I just need to get their names as string. –  Jun 01 '16 at 15:15

1 Answers1

4

What you're asking for is impossible. a, b and c are local variables names that are parsed by the compiler, but are not, in fact, kept throughout the process. The final, compiled DLL or EXE does not contain the names of these variables, just references to memory locations. There's no way to get these names at runtime - they're not part of the data structure, nor are they available as metadata.

It seems that what you're after is being able to access a specific list by name, but a) a List doesn't have a name, and b) a List, the outer list, isn't a good data structure to access objects by name. Unlike languages like PHP who use associative arrays by default, C# is stricter with its data structures - a List<T> is an index-accessible array-backed list, not an associative array.

If your intention is to access an object by name, you'll have to use a data structure that's designed for it - in this case, a Dictionary<string, List<string>>. This will allow you to set a name as a key to access an object:

private List<string> a = new List<string>();
private List<string> b = new List<string>();
private List<string> c = new List<string>();

private Dictionary<string, List<string>> lists = new Dictionary<string, List<string>>();  
lists.Add("a", a);
lists.Add("b", b);
lists.Add("q", c); // note that the key isn't related to the variable name.

You can then access a specific list by name through the Dictionary's indexer property:

var cList = lists["q"];

Alternate interpretation

Since your question isn't entirely clear, it's possible, based on one comment, that you simply want to extend the List<string> class to also carry a name, for some reason - perhaps to store a SQL table name associated with it, even though that's a pretty bad design that couples your code to a specific database structure.

This can be achieved by subclassing List<string> and adding a new property:

public class NamedList<T> : List<T>
{
     public NamedList(string name)
     {
          Name = name;
     }
     public string Name {get;set;}
}

Now, instead of creating a List<string>, you'll create a NamedList:

var a = new NamedList<string>("a");
var q = new NAmedList<string>("c");
Avner Shahar-Kashtan
  • 14,492
  • 3
  • 37
  • 63
  • Thank you Anvar Shahar-Kashtan. I defined a class having a list and a string as its data members. Then for any instance of that class, I have access to both of them. Your both ways are correct though –  Jun 05 '16 at 03:06