1
p1.Contains(listname[i, 0]);

p2.Contains(listname[i, 0]);

is there a way to replace p1 and p2 and use an variable instead. I want to do something like

"p" + variableName.Contains(listname[i, 0]);

This doesn't work.

Can someone tell me how I can make this work?

elnigno
  • 1,751
  • 14
  • 37
  • Give us the bigger picture: what are you trying to achieve? – dcastro Feb 13 '14 at 12:00
  • 5
    It is far from obvious what you are trying to achieve here, but if you want to reference local variables by a string representation of their "name", you are probably better off storing your lists in a `Dictionary>`. – Klaus Byskov Pedersen Feb 13 '14 at 12:00
  • @klaus it is obvious, it is the umpteenth question about variable variables as they exist in PHP: `$foo = 'p'; $bar = 1; $myVar = ${$foo$bar}` (or something like that), gaining the result of the variable named `$p1`. As with all previous instances of this question, the answer is "use an array, dictionary or other collection that suits your needs". – CodeCaster Feb 13 '14 at 12:03
  • Read: [*evaluating-string-variable-name*](http://bytes.com/topic/c-sharp/answers/279029-evaluating-string-variable-name) – Ravinder Reddy Feb 13 '14 at 12:06
  • 1
    @CodeCaster how come no one is giving the *real* answer to the question then: **Use reflection!** :-) – Klaus Byskov Pedersen Feb 13 '14 at 12:07

5 Answers5

3

Use an array or a hash table.

var p = new ICollection[2];
p[0] = p0;
p[1] = p1;
...
p[your_variable].Contains(listname[i,0]);

You cannot access variables by their names, as their names do not persist in runtime, e.g. the CLR has no chance to find the value of a variable "p1".

Georg
  • 5,626
  • 1
  • 23
  • 44
1

You can make array of p instead of declaring multiple variable. Assuming p1 and p2 are strings.

P []p = new P[10];

for(int i = 0; i < p.Length; i++)
     p[i].Contains(listname[i, 0]

It would be better if you can use List

List<P> p = new List<P>();

for(int i = 0; i < p.Length; i++)
   p[i].Contains(listname[i, 0]

Edit base on comments, p1 and p2 are collections

 var p = new ICollection[2];
 List<P> p1 = new List<P>();
 List<P> p2 = new List<P>();
 p[0] = p1;
 p[1] = p2;

p[variableContainZeroOrOne].Contains(listname[i, 0]     
Adil
  • 146,340
  • 25
  • 209
  • 204
0

Create a function that takes the list as a parameter.

public bool doSomething (List list, int i)
{
    bool test = list.Contains(listname[i, 0]);
    return test;
}
ohlmar
  • 958
  • 8
  • 17
  • You should explain that he has to create a new List, and add his `p1`, `p2`, `p3` to that new list. – nos Feb 13 '14 at 12:04
0

Simply create a variable of type List and assign the correct list to it. Use this variable in the rest of your code...

Shabi_669
  • 190
  • 4
0

If you have variables p1, p2 and you want to reference them by using number, then:

Solution 1, create helper method

string PX(int number)
{
   switch(number)
   {
       case 1: return p1;
       case 2: return p2;
   }

   // or
   if(number == 1) return p1;
   if(number == 2) return p2;
}

// usage
PX(x).Contains(blablabla);

Solution 2, use indexes

// helper array
string[] px = new string[] {p1, p2};
// usage
px[i - 1].Contains(blablabla);

or

// helper array
string[] px = new string[] {null, p1, p2};
// usage
px[i].Contains(blablabla);

Solution 3, use key-value collections.

Solution 4, use reflection.

Solutions 3 and 4 are slower and with unnecessary overhead. Indexes are fast, having helper method or helper array is not too much, consider following when constructing properties

private string[] _px = string[2];
public string[] PX { get { return px; } }
public string P1
{
    get { return _px[0]; }
    set { _px[0] = value; }
}
public string P2
{
    get { return _px[1]; }
    set { _px[1] = value; }
}

This way data are initially saved in array = you don't waste memory, while having possibility to access values by index PX[index] or by name P1/P2.

Sinatr
  • 20,892
  • 15
  • 90
  • 319
  • Is there a way to do this with Lists? – user3305851 Feb 13 '14 at 12:36
  • replace `string` with `List` (i took `string`, because I know `string.Contains` method) – Sinatr Feb 13 '14 at 12:52
  • I have two differents lists one for p1 and other for p2 p1.add(12) and p2.add(11) what i want to do is p[variable].add(value) the variable should tell to what list to add. – user3305851 Feb 13 '14 at 12:53
  • If variable >= 0 and <= array length - 1, then you are fine to just have array of lists `List[] px = new List[] {p1, p2}`, so `px[0] == p1` and `px[1] == p2`. – Sinatr Feb 13 '14 at 13:00
  • Does that make two seperate lists where values get stored depending on the variable? – user3305851 Feb 13 '14 at 13:27
  • If you want to *reference* N different `List`s, then the best is to initially have them in N-sized array and use index. Instead of `p1` you will have to use `px[0]` anywhere in your code. If `p1` and `p2` are different lists, then `px = new List[] {p1, p2}` is an array of 2 lists indeed. `px` is not a **copy** of your array, it's just an array. To make it simple, `px` will take something like 8 bytes of memory *disregards* how big `p1` or `p2` are. – Sinatr Feb 13 '14 at 13:31
  • I have one more question is it possible to make the list public/global? – user3305851 Feb 13 '14 at 13:46
  • Try it. Or better read a book about `C#` first! This way you will have hundreds of future questions answered! And if you are going to ask `which book?`, then go google and type there something like `c# best book`! – Sinatr Feb 13 '14 at 13:49