- Home /
C# ArrayList match to string?
Hi, I've been looking around now to see how I can or even If I can convert a ArrayList to a string and match it with another?
By making an output on DataStore[5] I get the number 1 or 0 as defined
(This is not the complete script but the important part.)
ArrayList DataStore = new ArrayList();
DataStore.AddRange(Regex.Split(info.text, "->"));
if(DataStore[5] == "1") // Error is here.
{
DisplayStrings = "Yes, its 1.";
}
else
{
DisplayStrings = "No, it's not 1.";
}
Possible unintended reference comparison. Consider casting the left side of the expression to `string' to compare the values
How do I make this work?
It's propably really simple but right now I'm really tired and need a fresh pair of eyes.
Thanks in andvance!
I wonder if the problem isn't that you're trying to do ArrayList.AddRange() on a string[] ...Not sure if string[] inherits from ICollection
Answer by stevethorne · Mar 12, 2014 at 08:31 PM
The error tells you what you need to do. Did you try casting DataStore[5] to a string? I try to avoid ArrayLists and use Lists instead so that it knows what types are coming in.
if ( ( ( string ) DataStore[5] ) == "1" )
Thanks, It worked like a sharm! Would I be able to change ArrayLists to Lists or would there be more to change?
Sure! Just create it like this.
List<string> DataStore = new List<string>();
Continue using everything else the way you have it, and this way you wont need to cast each entry to a string because it already knows what it is.
Huh, Look at that. Seems like I started from the wrong end. I will look in to this tomorrow, right now I really need some sleep. Thanks for the fast answer. $$anonymous$$y script is now working as it should!