Thursday, July 30, 2015

.hasOwnProperty in JavaScript

...does what you'd expect. The following example returns two alerts, the first saying "true" and the second saying "false"

var burningMan = {
   wickerColossusOnFire : "burn baby burn"
};
alert(burningMan.hasOwnProperty("wickerColossusOnFire"));
alert(burningMan.hasOwnProperty("wickerColossusAfterFire"));

 
 

This is another something I learned from Kyle Simpson's book "this & OBJECT PROTOTYPES"

Could not load file or assembly 'Whatever' or one of its dependencies. An attempt was made to load a program with an incorrect format.

This error raised its head for me when I attempted to make a new unit test project and have it test an existing C# class library project in Visual Studio 2015. As it turns out, if you right-click on a project and then go to the "Build" tab there is a place where you may explictly set the project to be x64 in lieu of x86 and the project I was testing had this setting and I had to give it to my unit test project to get around this error. Then when I attempted to test with MSTest by going to: Test > Run > All Tests ...my initial test just would not run. To get it to run I went to: Test > Test Settings > Default Processor Architecture ...and then picked x64.

Deserialize any randomly-shaped JSON object to a dynamic type in C# with JavaScriptSerializer and then pull properties off of it.

JavaScriptSerializer serializer = new JavaScriptSerializer();
var jsonObject = serializer.Deserialize<dynamic>(reader.ReadToEnd());
bool isVerified = jsonObject["success"];

 
 

This is an example directly from here which is some mostly working code for interfacing with Google reCAPTCHA (reCAPTCHA is a spiffier version of CAPTCHA that is harder for bots to beat and there is a C# library for it). If you want to use this code please note that the line for making a new WebRequest is crafted wrong and there is a better example of how to do so here. The whole thing should look like:

  1. String recaptcha = Request.Form["g-recaptcha-response"];
    string x = "secret=" +
    YOURKEY + "&response=" + recaptcha;
    WebRequest webRequest = WebRequest.Create("https://www.google.com/recaptcha/api/siteverify?" + x);
    webRequest.Method = "GET";
    WebResponse webResponse = webRequest.GetResponse();
    Stream stream = webResponse.GetResponseStream();
    StreamReader reader = new StreamReader(stream);

    The YOURKEY above variable is either a public or private key that one gets from Google, and one will get both. I was only exposed to this in helping a friend with code last night and I only see some of what's going on here.
  2. Next comes the three lines of code at the top of the blog posting. The thing you really want is the isVerified true/false value which will be false if the reCAPTCHA isn't undertaken successfully and true if the user is not a robot.
  3. The next three lines of code do a .Close(); on reader, stream, and webResponse. I guess I would have used a using statement if I wrote this, but of course, I didn't. I had to steal code from someone else who was competent. :P

Wednesday, July 29, 2015

"Rename" is TortoiseSVN for Subversion

If you right-click on a file in the Repository Browser you will see this option in the menu that appears. It does what you might think, retaining commit history.

European Union (EU) laws require you to give EU visitors information about cookies used on your blog. In many cases, these laws also require you to obtain consent.

Really? I just got this notice when I logged into my Blogger account! Supposedly some gunk has been automatically added to my blog to accommodate this. Aren't cookies scary?

Copy a file in Subversion without dropping the history.

With TortoiseSVN installed, right-click on a file under source control and pick "Repo-browser" out of the "TortoiseSVN" menu to bring up the Repository Browser. Right-click in the Repository Browser on a file (the one you want to copy) and pick "Copy to..." next. You will need to give a path to where the file should be copied to in source control. You may want to open another repo-browser and copy the path to the destination folder out of the URL line so you don't fat-finger it. The copied file will have all of the commit history as the file copied from! I suppose one could "move" a file by undertaking these same steps and then deleting the old file.

Use safe navigation operator in C# 6.0 to check to see if something is null in advance of calling a method.

Here we check to see if foo is null before calling .GetBarList() and if it's null we just return a new empty list of Bar as suggested by the null coalescing operator.

return foo?.GetBarList() ?? new List<Bar>();