Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. MAGIC: modifying a class memeber affects another member, HOW?

MAGIC: modifying a class memeber affects another member, HOW?

Scheduled Pinned Locked Moved C#
data-structuresquestionannouncement
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Muhammad Gouda
    wrote on last edited by
    #1

    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 array filesList hold the abstract names of the files As you see in the code, in the loop I update only the filesList The MAGIC is, the pathsList is updated too Can any why tell me how this happens?

    Mohammed Gouda foreach(Minute m in MyLife) myExperience++;

    L D P 3 Replies Last reply
    0
    • M Muhammad Gouda

      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 array filesList hold the abstract names of the files As you see in the code, in the loop I update only the filesList The MAGIC is, the pathsList is updated too Can any why tell me how this happens?

      Mohammed Gouda foreach(Minute m in MyLife) myExperience++;

      L Offline
      L Offline
      leppie
      wrote on last edited by
      #2

      Mohammed Gouda wrote:

      this.filesList = filesList; this.pathsList = filesList;

      Because you are modifying the same instance. Make a copy of pathsList.

      xacc.ide - now with TabsToSpaces support
      IronScheme - 1.0 alpha 4a out now (29 May 2008)

      1 Reply Last reply
      0
      • M Muhammad Gouda

        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 array filesList hold the abstract names of the files As you see in the code, in the loop I update only the filesList The MAGIC is, the pathsList is updated too Can any why tell me how this happens?

        Mohammed Gouda foreach(Minute m in MyLife) myExperience++;

        D Offline
        D Offline
        DaveyM69
        wrote on last edited by
        #3

        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

        M 1 Reply Last reply
        0
        • D DaveyM69

          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

          M Offline
          M Offline
          Muhammad Gouda
          wrote on last edited by
          #4

          Thanks I gussed fixed size array is passed by value not be ref

          Mohammed Gouda foreach(Minute m in MyLife) myExperience++;

          1 Reply Last reply
          0
          • M Muhammad Gouda

            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 array filesList hold the abstract names of the files As you see in the code, in the loop I update only the filesList The MAGIC is, the pathsList is updated too Can any why tell me how this happens?

            Mohammed Gouda foreach(Minute m in MyLife) myExperience++;

            P Offline
            P Offline
            PIEBALDconsult
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups