Weird behaviour: Immediate Window starting app instead of evaluating expression - visual-studio

I don't know that the following piece of code is really relevant, but for the sake of full disclosure here is the code I was trying to call from the Immediate Window:
abstract class Test
{
public int x;
public Test()
{
x = 5;
}
}
class TestImp : Test
{
public int GetX()
{
return this.x;
}
}
It was just a test to see whether the default base constructor gets called automatically or if I have to call it specifically because I couldn't remember.
Okay, on to the problem. I typed this into the Immediate Window to see the result:
new Mercury_Reports.TestImp().GetX();
And rather than evaluating the expression it just started my application. I closed the app and tried again two more times and got the same result. The next time, I went and put a breakpoint in my Program.cs file. Then instead of starting the app like it did the last three times and then hitting the breakpoint, it decided to actually just evaluate my expression.
I've seen some weird things in the Visual Studio IDE before, but I think this is one of the weirdest. Anyone have a clue as to what was going on there? :)

When evaluating an expression in the immediate window when you're not debugging the following process takes place
Loads your project / application binaries into the hosting process
Silently attaches the debugger to the hosting process
Evaluating the expression against that debugger session
Most of the time this is done in a way that makes it hard to detect that the application is actually being run. But occasionally a side effect of the application shows through and reveals what's really going on under the hood.
EDIT
In general it shouldn't be displaying the UI though. I can think of some obscure corner cases where this would happen though and not be a Visual Studio bug. The basically all come down to the same scenario though
The evaluated string causes an unintended side effect to happen at a time where it wouldn't happen during normal program flow.
This is actually more common than you'd expect in the immediate window because it's essentially executing your code out of order. Normally you'd never get to new Mercury_Reports before executing Program.Main but in the immediate window this is exactly what happens. This can have nasty effects like re-ordering static type constructors
Here are some unintended consequences which can surface via an immediate window expression
Cause types to be loaded and hence cause their static initializers to run
Change the order in which static initializers are run
The return type of the expression has a ToString method the debugger executes
The return type of the expression has a DebuggerDisplay value which the debugger executes
In the past I have seen the static constructor case cause UI to show. Essentially a static type constructor was evaluating MainForm.Instance (a lazy creation property). During normal program flow it was called from Program.Main was run and from then on was simply available. In the immediate window though Program.Main didn't run. But the expression being executed inadventently loaded that type and hence displayed the UI for what was a trivial property getter.
This is a pretty obscure corner case though. I'd say the most likely cause here is a bug in Visual Studio. Debugging is nasty business, especially when executing live code, and this is probably a symptom of that.

Related

Visual Studio - Is there any way to search a subset of code that can be executed?

Not sure if this is possible but given the way we can easily navigate to implementations of method calls (or at least a choice of possible implementations) and can even syntax highlight code coverage - is there any way to perform a 'search' or have an overview of all the code that CAN be run in a given highlighted section?
I.E if I highlight code
CallThirdParty(); // this function calls five other functions from classes X Y and Z
WriteToDatabase(); // no child function calls
PerformReconciliation(); // this function calls fourteen other functions from class A
Could I run a search on code that would be in classes X Y Z and A? Or at least get a view of all the code that would / could be run for that snippet?
Forgive me if it doesn't make much sense, but I think this would be absolutely awesome, especially when jumping into a project you aren't familiar with!
For Visual Studio for the question purposes but I'd be interested in any IDE / plugin that accomplishes this.
The Code Map might do what you're looking for. As the article says it can help you
Understand the overall architecture of a .NET application.
Analyze dependencies surfaced in that architecture by progressively drilling into the details.
Understand and analyze the impact of a proposed change to the code by building a dependency map from a specific code element.
The Call Hierarchy may help too. As the article says the
Call Hierarchy enables you to navigate through your code by displaying all calls to and from a selected method, property, or constructor. This enables you to better understand how code flows and to evaluate the effects of changes to code. You can examine several levels of code to view complex chains of method calls and additional entry points to the code, which enables you to explore all possible execution paths.
Additionally, you could always debug and step through the code to see what it does under different circumstances and then look at the call stack etc. to follow your calls and variables through.

Can a IDE find logic errors?

Can a good IDE (e.g. Visual Studio) find basic logic errors?
or is there no such thing as a "basic" logic error and all of these errors are undetectable by the IDE?
Yes, some IDEs (like Visual Studio) have continous syntax checks, that can find some logical errors. However, a logical error can only be spotted if there is something odd in the code, there is no AI trying to figure out what the code is actually intended to do.
If you for example write this in a C# method in Visual Studio:
int a = 1;
int b = 2;
Console.WriteLine(a + a);
then the IDE will notice that you never used the variable b, and put a warning in the form of a squiggly line under the variable. Pointing on it will reveal the message The variable 'b' is assigned, but its value is never used.
The IDE can't know if you intended to output a + b rather than a + a, and simply the use of a + a is not odd enough to render a warning, but it can see that you created a variable b and that you probably intended to use that for something.
Not really.
Sometimes it can pick up that a code path may never execute I think.
int x = 9;
if (x != 9)
{
foo();
}
and it maybe able to tell you that you've declared something without using it. It's stuff you can catch yourself. However, the real power is in the debugger where you can use "watch" or locals/autos and follow the code with step-in/out/over at any scope, see when they change, and change the values yourself to see what needs to happen. It's a good way to test logic. In assembly, you can move your code back a few lines and repeat it... it's not guaranteed to work, but you can override anything.
Edit: see https://en.wikipedia.org/wiki/Halting_problem

Syntax error when using Me in VB

Here's an interesting thing, I have used Visual Studio 2010 previously to make basic VB programs, and never had an issue with anything. Now I am just perplexed as to why this is happening.
I haven't even gotten very far into the coding phase of the first form, and already I am getting yelled at for a syntax error when I use "Me", whether it is Me.Close(), or whatever. Instead of posting the code as text here, it would be best to just show the syntax highlighting.
http://kelina-enterprises.com/img/vb-syntax-error-stackoverflow.png
::EDIT:: This image was taken down, it is not a real error.
Here's the funny thing, when I test the program, it runs just fine if I run the last successful build, which executes those "errored" lines.
Is this something I should even concern myself with, or should I just ignore these apparently false syntax errors?
Do keep in mind that if this is something simple that I have just neglected to take care of for some reason (nothing comes to mind), it has been about 3 years since I last used VB, and not even for a year at that.
You cannot write VB statements inside a Class, such as, Me.Close() or MessageBox.Show("??"). For example,
Class Test
MessageBox.Show("test")
End Class
is obviously syntax error
Inside a Class, you can write only declarations or methods.
In your case, you can place the codes in the Load event handler like this ...
Private Sub LoginForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Fullscreen, over everything, maximized
Me.Width = My.Computer.Screen.WorkingArea.Width
Me.Height = Screen.PrimaryScreen.Bounds.Height
Me.CenterToScreen()
Me.TopMost = True
Me.WindowState = FormWindowState.Maximized
End Sub
You don't need to type-in the Private Sub ... End Sub. Go to form design, double click the form and the Load event should be appeared.
It looks like you mean to put that code within the load event of the form, you can't have code that's not inside some sort of routine.
If you double-click on the form in design mode, the load event should be automatically created for you and you can then move the code into it.
There are also other events of the form that you may want to use, e.g. Shown event, these can be created by clicking on the appropriate selection in the drop-down box next to the one that has LoginForm selected.

Can I have VS 2013 ignore certain syntax errors until I build?

For example, I could pretty much always do without the "not all code paths return a value" error squiggly. This generally means I'm in the process of writing the method. I don't need to be told it doesn't return a value. If I forget to add a return value, VS can tell me that when it builds. But I never want that while I'm working; it's annoying and usually tells me something I already know.
For another example, I could do without the errors that show up while I'm in the process of writing a switch statement, such as the one that tells me I don't have a break statement yet, as I'm still working on whatever case.
You can try to disable "Show live semantic errors",
http://www.devcurry.com/2011/01/disable-squiggly-or-wavy-lines-in.html

Is it possible to set a breakpoint in a method chain in VS, and if so how?

Given the code
[Test]
public void Test1()
{
var a = new A();
a
.Method1()
.Method2();
}
is it possible to set a breakpoint so that execution pauses after Method1() has executed, but before Method2 without going to the definition of Method2 and putting a breakpoint there? When I do it, the breakpoint appears at the 'a'.
you can't set a breakpoint there, but you can set your breakpoint on the whole statement, and then use the "Step into Specific >" command on the right-click menu (Debug.StepIntoSpecific) to step into Method2().
you can also do repeated step in/step out to step through the indivdual method calls of the compound statement.
Use Rider instead of Visual Studio. IntelliJ Idea is capable of logical step in when fluent syntax is used. It is 2017 and fluent syntax is everywhere (LINQ). Shame on Visual Studio (even 2017).
No, the debugger's unit of executable code is a statement. There are only two in the method body in your snippet. Post feature requests to connect.microsoft.com. It's going to be a hard sell, it is not technically impossible but a potentially heavy re-engineering effort.

Resources