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. Debugging a getter

Debugging a getter

Scheduled Pinned Locked Moved C#
debuggingtestingbeta-testingquestion
4 Posts 3 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.
  • H Offline
    H Offline
    Hans Ruck
    wrote on last edited by
    #1

    Hi, I am using the following code for testing purposes:

    public class VariousTests
    {
    	public static void Main(string\[\] args)
    	{
    		AClass c = new AClass();
    		int y = c.DemandSupplyMatrix\[0, 0\]; // breakpoint 2
    	}
    }
    
    class AClass
    {
    	private int\[,\] \_aMatrix = null;
    	public int\[,\] DemandSupplyMatrix
    	{
    		get
    		{
    			if (\_aMatrix == null)
    				\_aMatrix = new int\[9, 4\];
    
    			return \_aMatrix; // breakpoint 1
    		}
    	}
    }
    

    I run it until I hit breakpoint 2 and I expect to see DemandSupplyMatrix being null in the debugger. Instead, it appears to have been already instantiated, and I have never hit breakpoint 1... I really need to debug the getter but it seems that I have no chance. Is this normal or am I missing something?

    Bogdan.

    L 1 Reply Last reply
    0
    • H Hans Ruck

      Hi, I am using the following code for testing purposes:

      public class VariousTests
      {
      	public static void Main(string\[\] args)
      	{
      		AClass c = new AClass();
      		int y = c.DemandSupplyMatrix\[0, 0\]; // breakpoint 2
      	}
      }
      
      class AClass
      {
      	private int\[,\] \_aMatrix = null;
      	public int\[,\] DemandSupplyMatrix
      	{
      		get
      		{
      			if (\_aMatrix == null)
      				\_aMatrix = new int\[9, 4\];
      
      			return \_aMatrix; // breakpoint 1
      		}
      	}
      }
      

      I run it until I hit breakpoint 2 and I expect to see DemandSupplyMatrix being null in the debugger. Instead, it appears to have been already instantiated, and I have never hit breakpoint 1... I really need to debug the getter but it seems that I have no chance. Is this normal or am I missing something?

      Bogdan.

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      Hi, I tried this on VS 2008 C# Express, and you're right, breakpoints inside getters never get hit. If you turn the getter into a normal method, it works fine. Probably is a bug in VS, properties get translated in methods with a slightly different name... :)

      Luc Pattyn


      I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


      Local announcement (Antwerp region): Lange Wapper? Neen!


      L 1 Reply Last reply
      0
      • L Luc Pattyn

        Hi, I tried this on VS 2008 C# Express, and you're right, breakpoints inside getters never get hit. If you turn the getter into a normal method, it works fine. Probably is a bug in VS, properties get translated in methods with a slightly different name... :)

        Luc Pattyn


        I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


        Local announcement (Antwerp region): Lange Wapper? Neen!


        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        It is also the suggestion of MS FxCop. Never use an array directly in a property. The suggestion is always change it to a method.

        L 1 Reply Last reply
        0
        • L Lost User

          It is also the suggestion of MS FxCop. Never use an array directly in a property. The suggestion is always change it to a method.

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          I was very surprised to see breakpoints work fine when the getter properties return a simple variable or a collection such as a List<int>, and they don't when the return type is an array. That doesn't make much sense to me. :omg:

          Luc Pattyn


          I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


          Local announcement (Antwerp region): Lange Wapper? Neen!


          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