tags:

views:

198

answers:

7

I'm trying to see what certain variables' values are in a C# .NET script (it gets an XML file, extracts a bunch of nodes, builds a sorting filter of some type, applies the filter, gets the result, deletes a few things and then iterates over the results outputting them). So I can figure out what is happening where and convert the script to do the same thing in PHP.

In PHP I would just do var_dump($someData); or echo $thePath. How do you do this in .NET (C#)?

In the script there are things like:

XmlDocument someData = new XmlDocument();
string thePath = Server.MapPath( "some.xml" );
someData.Load( thePath );

and

XPathNodeIterator iterator = navigator.Select(stuff);

and so forth but I don't always know what these things are doing (I mean, I know that there is an XML document being loaded and some portion of it is being selected to be iterated over but I want to be able to examine what various variables values are as I move through the script). I do not have access to Visual Studio... just a basic text editor and a browser.

+14  A: 

Grab a copy of Visual Studio Express and use the built-in debugger. Your life will be much easier.

jvenema
Worth mentioning: it's free of charge.
Mark Byers
There are also different editions, one for WinForms/WPF, and another for Web
ChrisF
+2  A: 

If you are going the 'debug by stdout' route you can use the Debug Class in .NET. The Console Class would work as well and might be a bit simpler.

Kris
+2  A: 

if you're just trying to output the value of a variable try this. It should behave like an alert, that way you don't have to try and convert it to a command line app. Should work as long as you have the standard Windows libraries.

MessageBox.show(thePath)
jon
I stuck that right in the midst of the page that has this particular script, uploaded the page to the server and it did nothing. I don’t know anything about .NET so this is no surprise, I guess.
rg88
Actually, I didn't notice you were working on a web env. I was thinking windows apps, sorry. BUT, @Aric TenEyck 's should work for ASP.NET
jon
+2  A: 

jvenema is right - you do have access to Visual Studio, you just don't know it (unless you don't have the rights to install software on your machine) - the express version is free.

However, if you don't want to wait for that, and you don't mind modifying your page to show variable values, you can put this in anywhere for guerilla debugging (since it sounds like you're using ASP.NET):

Response.Write(variable);

Depending on where in your code you do this, it probably won't land inside your viewable HTML, and it might even produce broken HTML output. However, if you view the page source in your browser, you'll be able to view it.

A better approach might be to declare a page-level string variable to which you can append your debug data:

// 'protected' so you can access it from your aspx page template.
protected string debugValues = string.Empty;

// Inside your page's code, do this with any variable you want to see:
this.debugValues += "variable=" + variable;

Then, in your aspx page template, just pick a good spot, perhaps at the bottom of the page, but within the viewable HTML and output it using the <%= %> syntax (which is a shortcut :

<body>
    // ... The rest of your page here ...
    <%= this.debugValues %>
</body>
Jeff Sternal
Sticking this in the page did nothing. I tried Response.Write("test"); as well and that also did nothing.
rg88
Updated with some additional info about that.
Jeff Sternal
Thanks Jeff (and everyone else). I downloaded a copy of Visual Studio Express as per jvenema's suggestion and then I was able to use Response.Write as you and others suggested (viewing it in source works fine for my purposes). For some reason it isn't working on the remote server but locally, using VS Express, it works perfectly. Thanks.
rg88
+2  A: 

If you don't want to use the free Visual Studio express or Mono Develop, I suggest getting a copy of DebugView and using the Trace.Write and Debug.Write.

kenny
+3  A: 

For a Web application, do Response.Write("text to write");.

Aric TenEyck
I stuck that in the script and it did nothing. It did cause the script to fail to return the data that it normally would have but it did not actually write anything.
rg88
+1  A: 
System.Debug.WriteLine(someData);
rein