Tuesday, January 31, 2012

Bitwise Logic in C#, and HasFlag

Hey, if you're ever using bitwise logic, or you want to know how to use bitwise logic, check out this post:

Bitwise Logic

One of the cool new features is the HasFlag() method in .NET 4.0 that lets you quickly verify if a bit has been flagged or not. The HasFlag is at the bottom of the page. I wish I could write more about this, but NO TIME!! Maybe I'll expand on it later. Ciao!

Line Numbers in Visual Studio

Here's a simple little feature that I never realized I could do. Did you know that you can actually view LINE NUMBERS in Visual Studio? YES! I'm sure all 1 of you who are reading this are like, "DUH!!", but I didn't know how to do it. If you want to see how, read this here:

How to Display Line Numbers in Editor

Oh, and you're welcome, DUH!!

Wednesday, January 18, 2012

Change Browser Plug-in

I posted something a while ago about how to change browsers in Visual Studio, but it can be problematic when using the MVC framework, because VS won't let you browse templates or Views directly in a browser like you can with Web Forms - which lead me on a short search down the interwebs where I found this solution!

For Visual Studio 2010, there's a downloadable plug-in available that will let you change your browser on the fly. Check out:

http://visualstudiogallery.msdn.microsoft.com/bb424812-f742-41ef-974a-cdac607df921

I'm using it now and it's awesome. It puts a little toolbar at the top of the IDE with icons for all the browsers you have installed on your PC so you can switch between them. Browsers available are ie, firefox, chrome, safari, and opera. This makes switching browsers even EASIER, and I've found it to be pretty nifty.

Oh, and according to Microsoft, a similar toolbar will come built in with Visual Studio 2011. Purty cool!

Saturday, January 14, 2012

Creating Custom Exception Types in .NET

There's going to come a time when you need to create a custom Exception type. Of course, there are always people who will go overboard and use a custom exception type instead of actually coding a check to prevent the exception, which is very bad, but that's now what this post is about. What it IS about is how to do it.. so I'll post a link to some guy who wrote it all out for me already.

http://blog.gurock.com/articles/creating-custom-exceptions-in-dotnet/

Me = SOooooo lazy.

Saturday, December 10, 2011

XML Deserializer

XML Deserializing is great! Basically you can define any class structure, use the XML Deserializer to convert any XML to the object you defined, so long as the structures match, of course. This makes it way easy to parse XML. I just used it yesterday for the first time. Here's a great resource below:

http://msdn.microsoft.com/en-us/library/tz8csy73.aspx

In the msdn sample above, it uses an xml file, but if you want to read from an xml string, simply use the following syntax to populate your XmlReader object and the rest is the same:

XmlReader reader = XmlReader.Create(new System.IO.StringReader(xmlSTRING));

Thursday, December 8, 2011

Generic Handlers in .NET and AJAX

If you've ever come across an instance where you don't want to use UpdatePanels but you need some ajax functionality (i.e. you need to do ajax manually), generic handlers are the way to go. I could probably write an entire post on why UpdatePanels are not always a great idea but here is one alternative.

To start with, create a generic handler. This should be easy enough. In the ProcessRequest(HttpContext context) method, you want to grab your parameters from the context. Here's an example:

string url = context.Request.QueryString["url"];

Once you've done that, take your parameters, do whatever functionality you need, and the .ashx should return the value via context.Response.Write().

On the client side, use the following javascript to read the response:

function createXMLHttpRequest() {
try { return new XMLHttpRequest(); } catch(e) {}
try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {}
try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {}
alert("XMLHttpRequest not supported");
return null;
}

var xmlHttpReq= createXMLHttpRequest();
xmlHttpReq.open("GET", "your.ashx?v1=1&v2=2&etc", false);
xmlHttpReq.send(null);
var yourJSString = xmlHttpReq.responseText;

Then, take that response value and use JQuery or javascript to make whatever change you need on the front end. It's that simple! Now you are making AJAX calls from scratch, and you can feel like you know something.