-5

I have this code, where i have an arrayList. I use the method arrayList.Contains.

 if (arrayList2.Contains(5))
 {
     //something
 }

I would like to replace arrayList2 in the code, with a variable (a string for example) that contains the value arrayList2.

string hello = "arrayList2" // create a varible with the value of the name of the arrayList
if (hello.Contains(5)) // insert the variable "hello" instead of "arrayList2"
{
    //something
}

This method doesn't work, any ideas how i can get it to work?

Ingo Karkat
  • 167,457
  • 16
  • 250
  • 324
Blareprefix
  • 850
  • 2
  • 9
  • 18
  • What do you mean 5? Is this index or substring? – red1ynx Jun 13 '11 at 10:22
  • 2
    You haven't clarified the question much over your previously-closed one. *Why do you think this is what you want to do?* – Jon Skeet Jun 13 '11 at 10:23
  • Naming variable `hello` makes me feel that you do not respect us and expect us to guess your puzzles. – Snowbear Jun 13 '11 at 10:25
  • If you want dinamically evaluate some method by the variable's name it's impossible. C# doesn't allows this. – Yuriy Rozhovetskiy Jun 13 '11 at 10:27
  • Completely different answers are pretty good indicator that something is rotten in the state of Denmark – Snowbear Jun 13 '11 at 10:31
  • Actually, I think he's asking how to get variable value when he only has variable name (with reflection). Something like: http://stackoverflow.com/questions/4629/c-eval-equivalent/4637#4637 – veljkoz Jun 13 '11 at 11:13

4 Answers4

7

I'll try to guess this one.

Maybe you want something like this:

Dictionary<string, ArrayList> arrayLists = new Dictionary<string, ArrayList>();
arrayLists.Add("arrayList1", new ArrayList());
arrayLists.Add("arrayList2", new ArrayList());
arrayLists.Add("arrayList3", new ArrayList());

string hello = "arrayList2";
if (arrayLists[hello].Contains(5)) { } 
Snowbear
  • 16,924
  • 3
  • 43
  • 67
0

if i understand ur question corectly maybe this is the answer

    public List<int> arrayList = new List<int>();

    private void test()
    {
        arrayList.Add(5);
        object[] parameters = { 5 };
        var your_arraylist= this.GetType().GetField("arrayList").GetType();
        var your_arraylist_method = your_arraylist.GetMethod("Contains");
    }
Saleh
  • 2,982
  • 5
  • 34
  • 59
0

You're confusing the Contains() method on an ArrayList with the Contains() method of a String - while they have the same name they do different things.

    ArrayList myArrayList = new ArrayList();
    object myObject = new object();
    if(myArrayList.Contains(myObject))
    {
        ..
    }

In this case, Contains() takes in an object and tests whether the ArrayList contains that object.

In your second example you're doing:

    String myString = "Hello my name is Chris";
    if(myString.Contains("my name"))
    {
        ..
    }

In this case, Contains() takes in a string and tests whether myString contains those letters in a sequence.

Your code doesnt compile because if (hello.Contains(5)) is trying to test whether an integer exists in that string. hello.Contains("5") would work - though I'm guessing this still isnt what you're after.

christofr
  • 2,680
  • 1
  • 18
  • 19
0

Looks like you have a php expirience and try to achieve something like this:

private static ArrayList arrayList;

    static void Main(string[] args)
    {
        arrayList = new ArrayList{1,2,3,4,5};

        var hello = "arrayList";
        var fieldInfo = typeof(Program).GetField(hello, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance);

        dynamic dyn = fieldInfo.GetValue(null);
        Console.WriteLine(dyn.Contains(5));
    }

If so, this is a not applicable in C# - even if C# 4.0 allows to do something like above you can't do this.

Yuriy Rozhovetskiy
  • 22,270
  • 4
  • 37
  • 68