0

I'm new in Visual Studio (2015) and C# and am having trouble with something very basic. What I'm trying to do is to simply test a function by displaying some text, that indicates the function this is executed from is called. I'm making a WPF application.

I have experience with javascript, php and Actionscript 3/Flash, and in all these languages it's easy to simply write a line of text, to see whether a function is called or see the value of a variable; with something like trace(), console.write() etc.

However, after looking for hours to a simple way for do this in C#, I can't find any way to do this without writing whole blocks of code or having this text written to some external file.

To my understanding, Trace.WriteLine() writes its content to an external file and needs a whole bunch of additional lines of code to have it displayed somewhere else. Same for Console.WriteLine(), because you first have to summon the console to be opened first.

As for now, I'm using System.Windows.MessageBox.Show("test"), but this isn't very convenient in my opinion. I cannot imagine there is no other simple way to trace a simple string or var or something.

Please help this rookie out!

paddotk
  • 1,359
  • 17
  • 31

2 Answers2

1

What you are looking for can be achieved with Debug.WriteLine.

It will display in the Output window of Visual Studio.

Szabolcs Dézsi
  • 8,743
  • 21
  • 29
  • Thank you. I didn't realise there was a pane I had to add to the default GUI of Visual Studio. Strange that it isn't there by default. – paddotk Feb 20 '16 at 22:05
  • p.s. is there also a way to do this without seeing a whole lot of 'ProjectName.vshost.exe (..)' lines surrounding this? – paddotk Feb 20 '16 at 22:11
  • You can right click the pane and uncheck everything except `Program Output` to remove those. – Szabolcs Dézsi Feb 20 '16 at 22:18
0

You can use Console.WriteLine() or Debug.WriteLine(). For the later, if you don't see the output, check this post:Where does System.Diagnostics.Debug.Write output appear?.

For further examples on Debug.WriteLine(), see:DotNetPerls-Debug.

Community
  • 1
  • 1
NoChance
  • 5,632
  • 4
  • 31
  • 45
  • Thanks, but like I mentioned, if you use `Console.WriteLine()` then the console doesn't automatically pop up. This method is too devious for a simple test. – paddotk Feb 20 '16 at 22:07
  • Nothing makes messages popup except MessageBox.Show() (or a similar coded implementation). However, The managed debugger does support a "when hit" condition on a breakpoint, allowing you to dump info to the output window. For more on that see:http://stackoverflow.com/questions/2912183/how-can-i-trace-a-variable-at-runtime-in-c – NoChance Feb 20 '16 at 22:09