tonelm54
asked on
array not updating
Im trying to write a simple function to remove a string from each element from an array, so what I have soo far is:-
My problem is that I call this function by:-
Where allBranch is an array of files starting 'c:\Windows\System32', and branchFolder is 'c:\Windows\System32', so the function should return an array of files where each element doesnt start with 'c:\Windows\System32', however it seems to run and return an the same array. Im assuming its because the for each loop passes the array as not a reference so it doesnt update
Any ideas how I can change my foreach loop to change the array as a reference.
public class RemoteFolder
{
public string[] RemoveFolderStings(string[] arrFiles, string folderName)
{
foreach (var fil in arrFiles)
{
fil.ToString().Replace(folderName, "");
}
return arrFiles;
}
}
My problem is that I call this function by:-
RemoteFolder r1 = new RemoteFolder();
allBranch = r1.RemoveFolderStings(allBranch, branchFolder);
Where allBranch is an array of files starting 'c:\Windows\System32', and branchFolder is 'c:\Windows\System32', so the function should return an array of files where each element doesnt start with 'c:\Windows\System32', however it seems to run and return an the same array. Im assuming its because the for each loop passes the array as not a reference so it doesnt update
Any ideas how I can change my foreach loop to change the array as a reference.
replace returns a string. it would probably be better to use a for loop and write to the index with what is returned
yourarry[index] = yourarray[index].ToString( ).Replace( folderName , "");
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Try this.
public string[] RemoveFolderStings(string[] arrFiles, string folderName)
{
for(int index=0;index<=arrFiles.Length-1; index++)
{
arrFiles[index] = arrFiles[index].ToString().Replace(folderName, "");
}
return arrFiles;
}
While I agree with with it_saige's explanation, if you are going to keep the method as a mutator of the array that is passed in, then you should clearly denote that that is what it is doing in the XML comments for your method. You don't want your fellow programmers (or even yourself some time down the road) to not know how your methods work internally.
In a purely mathematical sense, functions don't modify the arguments they are passed. You expect parameters in and a result out. There are no "side effects" to adding 1 and 2 together.
The fact that .NET lets you manipulate parameters being passed in should (ab)used judiciously.
In a purely mathematical sense, functions don't modify the arguments they are passed. You expect parameters in and a result out. There are no "side effects" to adding 1 and 2 together.
The fact that .NET lets you manipulate parameters being passed in should (ab)used judiciously.
I disagree. You did not provide the answer, you provided a part of the answer. I don't care about the points, if you want to stay in the *spirit* of EE, then you would want a more complete solution to be accepted. Why? Because the more complete solution is what visitors will see when searching for answers. The solution does not have to be a comprehensive solution with examples (like mine), but at least recognize that Norie's solution is a more rounded solution than the one you proposed.
-saige-
-saige-