4

I'm debugging an application and in some moment, I have a list with amount of items, which prevent me watch each element step by step. I wanted to check what value is inside property Layer of element with PropertyName = "XXX". Is there any simple way to do that?

Screenshot

Outer piece of code:

var metadata = FieldsConfigurationProvider.GetAllFieldsConfiguration();
RequestDocumentTypeModel model = new RequestDocumentTypeModel()
{
    Requester = CurrentUser.Login,
    MetadataItems = metadata.Where(f => !f.IsSystemGenerated).Select(f => new RequestDocumentMetadataItem(f.DocumentModelPropertyName, f.DisplayName, false, f.Layer)).ToList()
};

// BREAKPOINT HERE
// # MORE CODE #

Of course I can't use Immediate Window and LINQ, because LINQ isn't allowed. I'm using Visual Studio 2010, but as far as I know, other versions have the same "problem".

Fka
  • 6,044
  • 5
  • 42
  • 60
  • 1
    Could you write a function that returns what you want from the input `PropertyName`, and then run it in the quickwatch while debugging? – VoidStar May 22 '15 at 07:43
  • What is it you are looking out for? Tim's suggestion of using conditional breaks is probably right (+1), but I suspect you should be using it to break when you get a value that is erroneous for whatever reason – Sayse May 22 '15 at 07:47

6 Answers6

3

If you need to check the whole list at once, try DebuggerDisplayAttribute on the RequestDocumentMetadataItem in MetadataItems

[DebuggerDisplay("DisplayName = {DisplayName} PropertyName = {PropertyName}")]

Eric
  • 5,675
  • 16
  • 24
2

I wanted to check what value is inside property Layer of element with PropertyName = "XXX". Is there any simple way to do that?

Yes, you can specify a breakpoint condition. Then it stops only when the condition is met.

In your case:

PropertyName == "XXX"

How to: Specify a Breakpoint Condition

Remember that you never specify a condition like PropertyName = "XXX" since that changes the variable value silently.

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • Yes, indeed, but problem is, I don't want to write loop every time I'd like to check some property inside list. This list of item's isn't processed by my code. – Fka May 22 '15 at 07:45
  • 3
    @fka: you don't need to change the code, you can specify the breakpoint condition during debugging. You don't need to compile the code. It stops at that point only when the condition is met. – Tim Schmelter May 22 '15 at 07:47
  • I edited my question. Maybe I don't understand your answer well, but when I put breakpoint in specified place (of course this line isn't commented in real) and put contition: **model.MetadataItems.PropertyName == "XXX"** this should work? I don't think so, because MetadataItems doesn't have property **PropertyName** - items inside list do. – Fka May 22 '15 at 07:56
  • @Fka: so actually you want to stop if any of the items in the list have `.PropertyName == "XXX"`? Then it's indeed difficult without changing the code since you cant use LINQ in debugger. I would add a local variable `bool containsPropName=model.MetadataItems.Any(i=>i.PropertyName == "XXX");`. Then you can specify a breakpoint condition that uses `containsPropName`. – Tim Schmelter May 22 '15 at 07:59
  • No. As I mentioned in my question, I'd like only check what is value of `Layer` property of item with `PropertyName = "XXX"` in a simple way. That's all. – Fka May 22 '15 at 08:02
  • What means that you want to "check"? You can always check it's value once the debugger has stopped. You just need to specify the desired condition. – Tim Schmelter May 22 '15 at 08:03
  • I put breakpoint in specified line and opened Locals window in debugger. Then I recieved a list with 95 elements inside and **somewhere** in this list is my item (my item means `PropeortyName` == "XXX"). As you can see, I'm not iterating this list, I'm not transforming it in any way. This is just a property of my model of type `List – Fka May 22 '15 at 08:07
  • @Fka: yes, i understand now your problem. But i don't know how to solve it without changing the code. Interesting question. – Tim Schmelter May 22 '15 at 08:08
1

Two work arounds I can suggest:

  1. Follow the advice in this answer to use System.Linq.Dynamic expressions: https://stackoverflow.com/a/2771843/245452
  2. Wait for Visual Studio 2015 (http://blogs.msdn.com/b/visualstudioalm/archive/2014/11/12/support-for-debugging-lambda-expressions-with-visual-studio-2015.aspx)
Community
  • 1
  • 1
ytoledano
  • 3,003
  • 2
  • 24
  • 39
1

You could try writing a small wrapper around the List of your object type, and giving it a string based [] overloaded accessor. Like this:

public class ComplexType
{
    public string PropertyName { get; set; }
    public string Layer { get; set; }
    public string DisplayName { get; set; }
}

public class DebuggableList : List<ComplexType>
{
    public ComplexType this[string key]
    {
        get
        {
            return this.FirstOrDefault(i => i.PropertyName == key);
        }
    }
}

class Program
{
    static void Main(string[] args)
    {
        var myList= new DebuggableList();

        myList.Add(new ComplexType { DisplayName = "XXX", Layer = "YYY", PropertyName = "ZZZ" });
        myList.Add(new ComplexType { DisplayName = "AAA", Layer = "BBB", PropertyName = "CCC" });
        myList.Add(new ComplexType { DisplayName = "DDD", Layer = "EEE", PropertyName = "FFF" });
    }
}

In the watch window, you can then access your desired object using myList["XXX"], and the object with PropertyName=="XXX" will be displayed.

Baldrick
  • 11,712
  • 2
  • 31
  • 35
1

Perhaps right click on the line in the code, select Breakpoint -> Insert Tracepoint, and specify explicitly the property name with a comment e.g.:

enter image description here

You can then inspect this in the output window. The beauty of it is that you don't need to compile any additional code, and you can change it at any point at run-time.

Jeb
  • 3,689
  • 5
  • 28
  • 45
0

You could write a static debug class with helper functions for debugging. There are extensions which help debugging in VS itself, but I think none are free.

MarioDS
  • 12,895
  • 15
  • 65
  • 121