MAGIC: modifying a class memeber affects another member, HOW?
-
Here is the code:
public Document(string title, string source,string summery, string [] filesList, int noOfFiles)
{
this.title = title;
this.source = source;
this.summery = summery;
this.noOfFiles = noOfFiles;
this.filesList = filesList;
this.pathsList = filesList;string \[\] temp; for(int i=0; i<noOfFiles; i++) { temp = pathsList\[i\].Split('\\\\'); this.filesList\[i\] = temp\[temp.Length-1\]; } }
the array
pathsList
holds the full path of the files the arrayfilesList
hold the abstract names of the files As you see in the code, in the loop I update only thefilesList
The MAGIC is, thepathsList
is updated too Can any why tell me how this happens?Mohammed Gouda foreach(Minute m in MyLife) myExperience++;
-
Here is the code:
public Document(string title, string source,string summery, string [] filesList, int noOfFiles)
{
this.title = title;
this.source = source;
this.summery = summery;
this.noOfFiles = noOfFiles;
this.filesList = filesList;
this.pathsList = filesList;string \[\] temp; for(int i=0; i<noOfFiles; i++) { temp = pathsList\[i\].Split('\\\\'); this.filesList\[i\] = temp\[temp.Length-1\]; } }
the array
pathsList
holds the full path of the files the arrayfilesList
hold the abstract names of the files As you see in the code, in the loop I update only thefilesList
The MAGIC is, thepathsList
is updated too Can any why tell me how this happens?Mohammed Gouda foreach(Minute m in MyLife) myExperience++;
-
Here is the code:
public Document(string title, string source,string summery, string [] filesList, int noOfFiles)
{
this.title = title;
this.source = source;
this.summery = summery;
this.noOfFiles = noOfFiles;
this.filesList = filesList;
this.pathsList = filesList;string \[\] temp; for(int i=0; i<noOfFiles; i++) { temp = pathsList\[i\].Split('\\\\'); this.filesList\[i\] = temp\[temp.Length-1\]; } }
the array
pathsList
holds the full path of the files the arrayfilesList
hold the abstract names of the files As you see in the code, in the loop I update only thefilesList
The MAGIC is, thepathsList
is updated too Can any why tell me how this happens?Mohammed Gouda foreach(Minute m in MyLife) myExperience++;
-
they both hold a reference to the same string array, so when one alters the array it will be reflected in the other as they are still looking at the same reference. The solution is cloning - which is a whole new rabbit hole!
Dave
Thanks I gussed fixed size array is passed by value not be ref
Mohammed Gouda foreach(Minute m in MyLife) myExperience++;
-
Here is the code:
public Document(string title, string source,string summery, string [] filesList, int noOfFiles)
{
this.title = title;
this.source = source;
this.summery = summery;
this.noOfFiles = noOfFiles;
this.filesList = filesList;
this.pathsList = filesList;string \[\] temp; for(int i=0; i<noOfFiles; i++) { temp = pathsList\[i\].Split('\\\\'); this.filesList\[i\] = temp\[temp.Length-1\]; } }
the array
pathsList
holds the full path of the files the arrayfilesList
hold the abstract names of the files As you see in the code, in the loop I update only thefilesList
The MAGIC is, thepathsList
is updated too Can any why tell me how this happens?Mohammed Gouda foreach(Minute m in MyLife) myExperience++;
Yeah, what they said. Personally, I'd do someting more like this:
this.filesList = new string [ filesList.Length ] ;
this.pathsList = new string [ filesList.Length ] ;System.IO.FileInfo temp ;
for(int i=0; i<noOfFiles; i++)
{
temp = new System.IO.FileInfo ( filesList[i] ) ;this.pathsList\[i\] = temp.FullName ; this.filesList\[i\] = temp.Name ;
}
And more likely I'd use
List<string>
s rather than arrays. I also pass file specifications through System.Environment.ExpandEnvironmentVariables before using them, but that's just me.