开发者

C# insert a variable [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, i开发者_运维技巧ncomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

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?


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)) { } 


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");
    }


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜