|
Hello guys. I am copying one ArrayList into another ArrayList . However, I am calling ToArray() on my source ArrayList. Later when I try to retrieve values from my target ArrayList, it only gives my System.String[] , instead of string values. Here is what I am trying
public void CopySetting(ArrayList mySrcArrayList)
{
myTargetArrayList.Clear();
myTargetArrayList.Add(mySrcArrayList.ToArray(typeof(string)));
}
public GetValues()
{
foreach (Object obj in myTargetArrayList)
{
string str = (string)obj.ToString();
}
}
So what could be wrong here? Thanks for any help.
This world is going to explode due to international politics, SOON.
modified 4-Feb-13 3:21am.
|
|
|
|
|
what is the data type of 'm_objDigitList' variable? the sample code doesnt say anything about that.
Jibesh V P
|
|
|
|
|
ahhh. It is Target ArrayList. I think I could not modify my original code properly.
This world is going to explode due to international politics, SOON.
|
|
|
|
|
that's the main problem when you work with ArrayList, Add method simply accepts any object you pass. here you are adding the string[] returned by the source.ToArray operation. to Add the array range you should use array.AddRange.
My personal recommendation is to avoid using ArrayList in your application and use Generic lists.
public void CopySetting(ArrayList mySrcArrayList)
{
myTargetArrayList.Clear();
myTargetArrayList.AddRange(mySrcArrayList.ToArray(typeof(string)));
}
Jibesh V P
|
|
|
|
|
Yeah don't use ArrayList unless there is some good reason you don't want type safety. Even if the initial list is a heterogenous one in this case, so you want an untyped list, you should definitely be using List<String> as the target, since you're only adding strings to it.
|
|
|
|