Click here to Skip to main content
15,884,176 members
Please Sign up or sign in to vote.
3.33/5 (3 votes)
I have a VB.Net application in VS2010 that has a lot of collections (mostly List(Of something))

What I want to be able to do is look at a specific property on all of the items in the collection at once.

An example:

I have a List (Of Customer)

I am searching this list to find the one matching a Customer passed to me

VB
Dim found = From c in customers where c Is searchCustomer Select c


well, nothing is being found and I want to check what's going on. Is it that I have two instances of the same customer, or is the customer really not in the collection?

My debugging strategy would be to look at searchCustomer.CustomerId and check each of the customer object in the customers collection for their CustomerId property to see if there is a match or not.

The only ways I have been able to find to do this is

put a watch on customers(0).CustomerId..customers(99).CustomerId

which just takes too long to type

Or put a watch on the collection, and expand nodes looking at the property values - which takes too long and is just annoying

Apparently I can;t use loops in the immediate window - or I could do 'for i = 0 to 99 ... ? customer(0).Customerid ... Next' or something

I feel there must me a way of showing customers(x).CustomerId and have the debugger show every occurrence

I mean, the debugger knows how many instances there are in the collection ...

So, plz hlp! Urgntz
Posted

Add the DebuggerDisplayAttribute to your class. Then when you expand the list, you will see what you specified to display in the debugger.

Using DebuggerDisplay Attribute
http://msdn.microsoft.com/en-us/library/x810d419.aspx[^]

You might also want to turn on the debugging option "Show raw structure of objects in variable windows".

In addition, instead of using the DebuggerDisplayAttribute, you can override the ToString method, since there is a debugging option to "Call string-conversion function on objects in variables windows", but using DebuggerDisplayAttribute is better.

For example:

VB
<DebuggerDisplay("{CustomerId}: {CustomerName}")>
Public Class Customer
   ...
End Class
 
Share this answer
 
v3
Comments
_Maxxx_ 1-Sep-11 5:31am    
Thanks - I just had a quick play - wasn't aware of that option, it could be useful
What I'd really like would be to be able to effectively apply that attribute to a class at run-time from within the debugger, so when I come across situations like this I don't have to stop the app, change code, and run again.
Andy Missico 1-Sep-11 6:02am    
Create a global method to dump a list to the debug window. When execution is paused, call the method from the immediate window and pass in your list. I have about ten standard "dump" methods in my code library.
Andy Missico 1-Sep-11 6:06am    
It is not so bad changing the display attribute. After a few modifications, you have a debugger display string that stays with and becomes part of the class.
Simon_Whale 1-Sep-11 5:52am    
This is also a good site that I got more information about that tag

http://ittecture.wordpress.com/2009/02/23/tip-of-the-day-132/

but otherwise my +5 as that will help me in my current project :)
_Maxxx_ 1-Sep-11 18:31pm    
I like the Dump solution - I wonder if I could write a generic one where I could pass a collection and a property name... Hmm if I had more time I'd feel an article coming on!
Changing the display attribute isn't too bad, I admit, but I think I kinda felt there must be a built-in debugger tool, like a Generic Dump method. I guess attributing all objects would become habit, and defaulting to using their key values as the attribute fields would solve the issue 9 times out of 10
Oh - and thanks for actually answering the question, and not telling my why my code was wrong / could be improved!! :)
It's not a debugging trick, but I just wonder on the use of Is. I think if you want to check for equality, go for Equals. This may solve the problem.
http://msdn.microsoft.com/en-us/library/system.object.equals(v=VS.90).aspx[^]
VB
Dim found = From c in customers where searchCustomer.equals(c) Select c

The Is operator determines if two object references refer to the same object. However, it does not perform value comparisons.
http://msdn.microsoft.com/en-us/library/kb136x1y.aspx#Y300[^]
 
Share this answer
 
v2
Comments
_Maxxx_ 1-Sep-11 2:44am    
Yep - the reason for using that as an example was because the Is could be a problem - and so I want to check whether or not the object I am expecting to be in the collection really is in the collection, by looking at (for example) the customer ids in the collection in the debugger.
But it is just an example.
Equally as valid would have been if I had had where c.CustomerId = 27. If that isn't returning any found objects, then I want to look at my collection and determine if a) the object is genuinely not there, b) it is there, but the CustomerId isn't actually 27 or c) it is there, the CustomerId is 27 and there is something horribly wrong.
Prerak Patel 1-Sep-11 4:37am    
You can place If False Then For Each obj In objCollection : Debug.Print(obj.Name) : Next at the place you want to check. It will be skipped all the time. When you want to check value, set while as next statement and then you can check in output screen.
for finding a single record have you not thought about using lambda?

i.e.

VB
dim Customer as CustomerClass = Customers.find(function(p) p.CustomerID = 27)


have a read of this MSDN list(of T).find[^]
 
Share this answer
 
Comments
_Maxxx_ 1-Sep-11 18:34pm    
I don't see what the difference is, really. In fact I want to find the instance in the collection that is the same instance as my searchCustomer.
And my question isn't about the code, it is about how to debug effectively with collections
Cheerrs
The way your sample code looks is that you're seeing if c is of type searchCustomer. If you want to see if c is the same as searchcustomer, you need to do this:

C#
from c in customers where c.Equals(seachCustomer) select c
 
Share this answer
 
Comments
_Maxxx_ 1-Sep-11 18:27pm    
Cheers John, but the code isn't the issue - I know it may be flawed - my point is I want a debugging strategy when things like this happen, so I can easily see what instances each of the members of my collection are. Maybe when I'd seen that, I would have found what was wrong.
Oh, and you're wrong - Is is telling me that both instances are the same instance (which, in the code I was looking at should have been the case) NOT that c is of type searchCustomer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900