user1477388
user1477388

Reputation: 21440

VB.NET Equivalent to Var_dump($myVar); Die();

In PHP, one can immediately stop the execution of the script and output a variable. For instance if you want to know what's in $myVar, you could do:

var_dump($myVar); die();

Is there anyway to immediately get the value of a variable and then kill it in VB.NET?

I'm not asking for a function to include necessarily; just if there is a native function that will basically output a var immediately, without me having to add it to the controller, then the view, then publish and test. Ideally, it would just output the variable in a debugger or messagebox or something...

I am also using ASP.NET MVC 3.

Thanks.

Upvotes: 1

Views: 5806

Answers (1)

Jesse Hallam
Jesse Hallam

Reputation: 6964

You can use the System.Diagnostics.Trace and System.Diagnostics.Debug classes to print out information.

Stopping the execution typically occurs by either returning a relevant System.Web.Mvc.ActionResult, such as System.Web.Mvc.HttpNotFoundResult or by throwing a System.Web.HttpException with the HTTP status code which best describes the problem.

Here's a naive example. Excuse my poor VB.

Public Function UpdateUser(id as Integer, userName as String, password as Password) as ActionResult
    Dim user as User = DataServices.GetUserById(id)
    If user Is Nothing Then
        System.Diagnostics.Debug.WriteLine("Aborting because user did not exist.")
        throw new HttpException(404, "Page not found.")
    End
    ' Do more stuff
end

Or in C#

public ActionResult UpdateUser(int id, string userName, string password) {
    var user = DataServices.Users.Where(u => u.id == id).SingleOrDefault();

    if( user == null ) {
        Debug.WriteLine("Aborting because user did not exist.");
        return HttpNotFound(); // Helper method in Controller Class.
    }
    else if( myRoles() < thisUsersRoles(user) ) {
        Debug.Writeline("Aborting because insufficient access.");
        throw new HttpException(401, "Unauthorized");
    }

    // update user here
    return View();
}

Note that these examples are contrived only to display the use of Debug and the return of http status codes.

Upvotes: 1

Related Questions