locked
scope misunderstanding RRS feed

  • Question

  • Hi,

    I made this mistake in a asp.net codebehind page.

    public partial class EditOfferedObject : AdminPage
    	{
    		private int _PadId;
    
    		protected void Page_Load(object sender, EventArgs e)
    		{
    			int _PadId = Convert.ToInt32(Request.QueryString["id"]);
    			
    			if (!Page.IsPostBack)
    			{
    				BindUserPad();
    			}
    		}
    }
    
    

     

    The strange thing is that the private variable _PadId changes when I change the local variable _PadId in Page_Load. But when I call BindUserPad(); the private _PadId is 0 again. Off course it is bad coding, but shouldn't the compiler make an error or shouldn't the Private _PadId ignore the local _PadId (because of different scope)?

    Can anyone explain what happens?

    Thanks,

    Willem,


    Willem de Jong
    www.willemdejong.net
    www.levendFW.codeplex.com
    Tuesday, April 19, 2011 11:19 AM

Answers

  • the private variable _PadId changes when I change the local variable _PadId in Page_Load

    I doubt it. Inside the Page_Load method, the simple name _PadId is resolved as the local variable, not the private field. To access the field, you'd need to qualify it using the "this" keyword:

    this._PadId = ...

    • Proposed as answer by Jack0x539 Tuesday, April 19, 2011 1:09 PM
    • Marked as answer by Larcolais Gong Monday, April 25, 2011 2:49 AM
    Tuesday, April 19, 2011 11:40 AM
  • Hi Willem,

    The compiler doesn't really care about the naming of local variables as long as it finds no repetitions inside one scope (otherwise you get CS0102: The type 'type name' already contains a definition for 'identifier'). Take a look at the produced IL. All you will see is some call like .local init (int32), no sign of _padId whatsoever in the disassembly of Page_Load. By contrast, fields do retain their name in the IL-code, so you shall see something like .field private int32 _PadId when you look at the disassembled app.

    There's nothing wrong with having the same variable name in different scopes from the compiler's point of view, it's only a programming bad practice. As for _PadID getting set to 0 when you move to BindUserPad(), I think the debugger is fooling you. As it fools you when - while in Page_Load - you hover over the _PadId field just to find out that it's suddenly set to 1 (huh?). Just Maya, nothing real. When inside BindUserPad() the local variable goes out of scope, so the debugger now shows you the value of the field.

    Marcel


    • Proposed as answer by Marcel Roma Tuesday, April 19, 2011 2:49 PM
    • Marked as answer by Larcolais Gong Monday, April 25, 2011 2:49 AM
    Tuesday, April 19, 2011 11:57 AM

All replies

  • the private variable _PadId changes when I change the local variable _PadId in Page_Load

    I doubt it. Inside the Page_Load method, the simple name _PadId is resolved as the local variable, not the private field. To access the field, you'd need to qualify it using the "this" keyword:

    this._PadId = ...

    • Proposed as answer by Jack0x539 Tuesday, April 19, 2011 1:09 PM
    • Marked as answer by Larcolais Gong Monday, April 25, 2011 2:49 AM
    Tuesday, April 19, 2011 11:40 AM
  • Hi Willem,

    The compiler doesn't really care about the naming of local variables as long as it finds no repetitions inside one scope (otherwise you get CS0102: The type 'type name' already contains a definition for 'identifier'). Take a look at the produced IL. All you will see is some call like .local init (int32), no sign of _padId whatsoever in the disassembly of Page_Load. By contrast, fields do retain their name in the IL-code, so you shall see something like .field private int32 _PadId when you look at the disassembled app.

    There's nothing wrong with having the same variable name in different scopes from the compiler's point of view, it's only a programming bad practice. As for _PadID getting set to 0 when you move to BindUserPad(), I think the debugger is fooling you. As it fools you when - while in Page_Load - you hover over the _PadId field just to find out that it's suddenly set to 1 (huh?). Just Maya, nothing real. When inside BindUserPad() the local variable goes out of scope, so the debugger now shows you the value of the field.

    Marcel


    • Proposed as answer by Marcel Roma Tuesday, April 19, 2011 2:49 PM
    • Marked as answer by Larcolais Gong Monday, April 25, 2011 2:49 AM
    Tuesday, April 19, 2011 11:57 AM
  • I think that is it: the debugger is fooling me and shows the wrong value.

    Thanks for the replies.


    Willem de Jong
    www.willemdejong.net
    www.levendFW.codeplex.com
    Tuesday, April 19, 2011 12:24 PM
  • Any update? Has your question been solved? Please remember to mark helpful reply as Answered.

    Best Regards,


    Larcolais Gong[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, April 20, 2011 5:32 AM