View the value of a variable in .NET C#?
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 th开发者_StackOverflowePath = 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.
Grab a copy of Visual Studio Express and use the built-in debugger. Your life will be much easier.
For a Web application, do Response.Write("text to write");
.
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>
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.
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)
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.
System.Debug.WriteLine(someData);
精彩评论