Thursday, June 17, 2010

C# vs VB - The debate continues...

Just getting my feet wet with C#, after being a VB developer for awhile, realizing that C# is getting all of the jobs. With that being said, have run into a few difficulties in converting over, and I'll list a couple here, to be added to as I go along. Consider this a newbie's version of headaches you may face when switching to C#...




  1. In VB, if you want to locate an element inside of a control, you would use:

    Dim control as CONTROLTYPE = CType(CONTAINER.FindControl("ControlID"), CONTROLTYPE)


    In C#, you would write the same expression as:

    CONTROLTYPE control = (CONTROLTYPE)CONTAINER.FindControl("ControlID");

  2. In VB, if you want to place an IIf statement inside of a Label Control, you're free to do so. Simply use

    <%#IIf(Convert.ToString(EXPRESSION)=Something, True, False)%>

    However, when you attempt to use this in C#, you will get an error, as IIf isn't recognized.

    Here's the fix:

    <%# (Convert.ToString(EXPRESSION) == Something ? True : False)%>.

  3. Of Course, & is not recognized, it is replaced by +.

  4. I know when I was creating a VB Side HTML Message, or if I was creating a very long string, I would use '& vbCrLf _'. However, in C#, this changes a bit. Declare your String, and append it just as you would in JS. Example?


    string A = "";

    A += "This is a string ";
    A += " and the second line of the string...";



I will modify this as I go, but if you are like me, and have done VB this whole time, I would recommend taking any current sites you have done, and redo them from scratch in C#. Get Hung Up? Use THIS as a last resort, it's a pretty good tool.


Good Luck!

Wednesday, June 9, 2010

Thank You Ryan Grove - Finally a Script Loader that works painlessly!

Finally was able to make my homepage not CRAWL due to a lazy script loader that I found. Prior to this, either security was too tight from GoDaddy or it was just made for Image Files. Anyways, check this out...


http://wonko.com/post/painless_javascript_lazy_loading_with_lazyload

Tuesday, June 8, 2010

Use Google Calendar as Datasource for Holiday Notifications

I wanted to see how difficult it would be to do a simple notification on my site of any basic holidays that occurred on any given day. Sure, it would be simple enough to simply create an array to hold the data in js, and display if the date matched, but I figured I would use a JSON Feed to display the data instead. Turns out Google Calendar has just the thing in their Holiday Calendar Feed.

http://www.google.com/calendar/feeds/en.usa%23holiday%40group.v.calendar.google.com/public/basic

If you go to this feed, you'll notice there is a lot of information that needs to be parsed down to use it, so after some fiddling, I came up with the following YQL Feed:

select entry.title.content, entry.summary.content from xml where url='http://www.google.com/calendar/feeds/en.usa%23holiday%40group.v.calendar.google.com/public/basic' AND entry.content.content LIKE '% + year + %'

with year being a new Date().getYear() value.

Anyways, here's the script. It simply displays a message in a Div if today falls on one of the holidays.

$(function () {
var d = new Date();
var year = d.getYear();
var shortDay = (d.toDateString()).slice(4, 10);
var query = "select entry.title.content, entry.summary.content from xml where url='http://www.google.com/calendar/feeds/en.usa%23holiday%40group.v.calendar.google.com/public/basic' AND entry.content.content LIKE '%" + year + "%'";
$.getJSON(
"http://query.yahooapis.com/v1/public/yql?q=" + encodeURIComponent(query) + "&format=json&callback=?",
function (data) {
$.each(data.query.results,
function (index, item) {
for (i = 0; i < item.length; i++) {
var each = item[i];
var date = ((each.entry.summary).slice(9, 16).replace(',', ''));
if (jQuery.trim(date) == jQuery.trim(shortDay)) {
$('<h6></h6>').html('Today is ' + each.entry.title + '!').appendTo('#holidays');
}
}
});
});
});

You can feel free to download this script if you would like to use it.

http://www.joshuablackstone.com/Scripts/HappyHolidays.js.

Friday, June 4, 2010

Using YQL and Google Translate to Translate Text

Had a little time to play, so I wanted to see what I could do to internationalize (I think that's a word :) ) my site. I noticed in YQL there was a GoogleTranslate Table, so after some trial and error, found out that with:

select * from json where url='http://www.google.com/uds/Gtranslate?context=22&langpair=%7C" + lang + "&v=1.0&q=" + encodeURIComponent(str) + "'

with the variables being, of course, lang for language symbol (i.e. en for english, de for dutch, etc.) and str for Text String to be translated. Plug that into a YQL Json function and you're set, well, almost...

Why not also scrape the items from the Google Site to use for a Combo Box to select a language from? If you go to: http://translate.google.com, you will see a combo box next to TranslateInto. To Get the data from that, simply use this query as your JSON String:

http://query.yahooapis.com/v1/public/yql?q=select%20option%20from%20html(0)%20where%20url%3D%22http%3A%2F%2Ftranslate.google.com%22%20and%0D%0A%20%20%20%20%20%20xpath%3D'%2F%2Fselect%5B%40class%3D%22tllangdropdown%22%5D'&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=?

Plug that into the YQL Console, and you will find that you can get the value and Content for items to plug into a combo box of your own.

Finally, wire up the dropdown that was created with an event similar to:

$("#DROPDOWNNAME").change(function () {
translateText($(str).html(), this.value);
});

And you're all done.

I realize this was a very brief explaination, but for the full script simply go to:

http://www.joshuablackstone.com/Scripts/GTranslate.js.