Reputation: 21
How can I let me show the result of a condition in visual studio while debugging through? Let's say my code looks like:
If a > b andalso c > b andalso d < y then
If i step through I cannot see which of the three conditions are false. Is there a way to be able to?
Upvotes: 2
Views: 1229
Reputation: 1
Try This :
if(a>b)
{
if(c>b)
{
if(d<y)
{
/* Your code here */
}
}
}
Upvotes: 0
Reputation:
you may use System.Diagnostics
and use Debugger.Break()
to stop debugger at this line and see the Debugger Output,
Like this sample code:
in C#:
using System;
using System.Diagnostics;
class Test
{
static volatile int a = a, b = 2, c = 3, d = 4, y = 5;
static void Main(string[] args)
{
Debugger.Break();
Debug.WriteLine("a > b:{0}", a > b);
Debug.WriteLine("c > b:{0}", c > b);
Debug.WriteLine("d < y:{0}", d < y);
Debug.WriteLine("a > b && c > b && d < y:{0}", a > b && c > b && d < y);
if (a > b && c > b && d < y)
{
Console.WriteLine("...");
}
}
}
or in VB:
Imports System
Imports System.Diagnostics
Module Module1
Sub Main()
Dim a As Integer = 1, b As Integer = 2, c As Integer = 3, d As Integer = 4, y As Integer = 5
Debugger.Break()
Debug.WriteLine("a > b:{0}", a > b)
Debug.WriteLine("c > b:{0}", c > b)
Debug.WriteLine("d < y:{0}", d < y)
Debug.WriteLine("a > b && c > b && d < y:{0}", a > b AndAlso c > b AndAlso d < y)
If a > b AndAlso c > b AndAlso d < y Then
Console.WriteLine("...")
End If
End Sub
End Module
i hope this helps.
Upvotes: 0
Reputation: 11773
If I were having an issue with the code I would rewrite it like this so that it was clear and easy to debug.
Dim aGTb As Boolean = a > b
Dim cGTb As Boolean = c > b
Dim dLTy As Boolean = d < y
If aGTb AndAlso cGTb AndAlso dLTy Then
End If
Upvotes: 0
Reputation: 785
You can use the immediate window and copy paste single conditions or anything else you like. This way you don't need to change your code.
Upvotes: 1
Reputation: 11389
You could use the debugconsole to write the results like this:
bool ab = a > b;
bool cb = c > b;
bool dy = d < y;
System.Diagnostics.Debug.WriteLine("a > b = " + ab + ", "c > b = " cb + ", d < y = " + dy");
if (ab && cd && dy)
{
//Your code here
}
The 3 results are shown on the debugconsole like
a > b = true, c > b = false, d < y = true
Optional you could add a fourth boolean like
bool result = ab && cd && dy;
and print it also on the console.
Upvotes: 1
Reputation: 809
If you want to write a code in away which is the best for debugging it will horrible way of coding, e.g. the best way to rewrite the above for debugging as follows:
bool isValid = false;
isValid = isValid && a > b
isValid = isValid && c > b
isValid = isValid && d < y
Unless there is a certain logic in your program to find which part is failing... this way is nonsense, it just better if you add each part to the watch and validate it, overall debugging is not the purpose of code writing.
Upvotes: 0