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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Strange Behaviour of an Object

Strange Behaviour of an Object

Scheduled Pinned Locked Moved C#
helpdatabasexmlquestion
2 Posts 1 Posters 1 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.
  • 1 Offline
    1 Offline
    1nsp1r3d
    wrote on last edited by
    #1

    Hi all, I've encountered a strange behaviour of one of my objects. After it updates itself in a method(updates some private fields) the changes happen when the method is being executed but when execution returns to the line where the method was called, the object is the same as before. Let me show you the code which does this.

    public enum DConfMode
    {
    Normal,
    NoInit
    }

    public class DConf
    {
    	private String hostname;
    	private String database;
    	private String username;
    	private String password;
    
    	public DConf(DConfMode Mode)
    	{
    		switch (Mode)
    		{
    			case DConfMode.Normal:
    				this.Init();
    				break;
    			case DConfMode.NoInit:
    				break;
    		}
    	}
    
    	public void Init()
    	{
    		XmlTextReader reader = new XmlTextReader(DGlobals.ConfFile);
    
    		while (reader.Read())
    		{
    			switch (reader.Name)
    			{
    				case "hostname":
    					this.hostname = reader.Value;
    					break;
    				case "database":
    					this.database = reader.Value;
    					break;
    				case "user":
    					this.username = reader.Value;
    					break;
    				case "password":
    					this.password = reader.Value;
    					break;
    			}
    		}
    		reader.Close();
    	}
    
    	public String DbHostname
    	{
    		get
    		{
    			return this.hostname;
    		}
    	}
    
    	public String DbDatabase
    	{
    		get
    		{
    			return this.database;
    		}
    	}
    
    	public String DbUsername
    	{
    		get
    		{
    			return this.username;
    		}
    	}
    
    	public String DbPassword
    	{
    		get
    		{
    			return this.password;
    		}
    	}
    }
    

    DConf conf = new DConf(DConfMode.NoInit);
    conf.Init();
    Console.WriteLine(conf.DbHostname);

    So DConf does the problem. In the second code excerpt, echoing conf.DbHostname doesn't do anything, but when I've debugged it, in conf.Init(); the values are clearly populated. So I'm quite confused why this happens. Here's some sample xml which you could use: username samplepass localhost dbname As a sidenote, is it safe calling Init(); in DConf's constructor? I suppose no, since it changes fields and the object hasn't been guaranteed to have been fully set up, am I correct? Any help is greatly appreciated. Regards, gamehack -- modified at 19:29 Saturday 29th October, 2005

    1 1 Reply Last reply
    0
    • 1 1nsp1r3d

      Hi all, I've encountered a strange behaviour of one of my objects. After it updates itself in a method(updates some private fields) the changes happen when the method is being executed but when execution returns to the line where the method was called, the object is the same as before. Let me show you the code which does this.

      public enum DConfMode
      {
      Normal,
      NoInit
      }

      public class DConf
      {
      	private String hostname;
      	private String database;
      	private String username;
      	private String password;
      
      	public DConf(DConfMode Mode)
      	{
      		switch (Mode)
      		{
      			case DConfMode.Normal:
      				this.Init();
      				break;
      			case DConfMode.NoInit:
      				break;
      		}
      	}
      
      	public void Init()
      	{
      		XmlTextReader reader = new XmlTextReader(DGlobals.ConfFile);
      
      		while (reader.Read())
      		{
      			switch (reader.Name)
      			{
      				case "hostname":
      					this.hostname = reader.Value;
      					break;
      				case "database":
      					this.database = reader.Value;
      					break;
      				case "user":
      					this.username = reader.Value;
      					break;
      				case "password":
      					this.password = reader.Value;
      					break;
      			}
      		}
      		reader.Close();
      	}
      
      	public String DbHostname
      	{
      		get
      		{
      			return this.hostname;
      		}
      	}
      
      	public String DbDatabase
      	{
      		get
      		{
      			return this.database;
      		}
      	}
      
      	public String DbUsername
      	{
      		get
      		{
      			return this.username;
      		}
      	}
      
      	public String DbPassword
      	{
      		get
      		{
      			return this.password;
      		}
      	}
      }
      

      DConf conf = new DConf(DConfMode.NoInit);
      conf.Init();
      Console.WriteLine(conf.DbHostname);

      So DConf does the problem. In the second code excerpt, echoing conf.DbHostname doesn't do anything, but when I've debugged it, in conf.Init(); the values are clearly populated. So I'm quite confused why this happens. Here's some sample xml which you could use: username samplepass localhost dbname As a sidenote, is it safe calling Init(); in DConf's constructor? I suppose no, since it changes fields and the object hasn't been guaranteed to have been fully set up, am I correct? Any help is greatly appreciated. Regards, gamehack -- modified at 19:29 Saturday 29th October, 2005

      1 Offline
      1 Offline
      1nsp1r3d
      wrote on last edited by
      #2

      I've found my mistake. It's in my xml handling code. Thanks for reading. Regards

      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