Brilliant conversion work on an Imperial Reaver Titan
I was listening through Dan Abnett’s reviews on working for the Black Library, his work on the Horus Heresy books and Gaunt’s Ghosts series when I came across this beauty, an awesome conversion which I would love to attempt in the future.
Ordering and Removing Duplicates from a C# IList using System.Linq
I have recently been delving into dealing with ILists Containing Objects, and have a need to order them without doing another database query with an Order By statement. Previously I would have dealt with this situation by looping through the elements one at a time and if they matched a set of rules put them in and if not ignore, in other words a lot of effort for the same gain. What I should have been doing was using the wonderfulness of System.Linq to solve my duplication filtering and sorting woes, as below.
using System.Linq;
...
public IList<StatusType> returnListMinusDuplicates(IList<StatusType> statusTypes) {
if (statusTypes== null) { return new List<StatusType>(); }
// remove the items with the same Name
IList<StatusType> filteredList = statusTypes
.GroupBy(x => x.Name.ToLower())
.Select(x => x.First())
.ToList<StatusType>();
// sort and return the List
return filteredList.OrderBy(x => x.Name);
}
The CIA Social Network Project Codename: Facebook
This is a brilliant satirical video from the Onion Network about the CIA’s information gathering project codename: Project Facebook. See it for yourself
The best quote “400 billion tweets and not one useful bit of data was ever transmitted…”
CSS display: table-cell, table-row, table in IE7
The CSS property display is a very handy attribute which can easily allow you to structure content which would not logically be table content in a table structure. The reason for doing this is simple, under the w3c guidelines for the Semantic Web, a table should contain tabular data, not be used for structuring a page. Which is fine, because the CSS framework has
<style>
.table { display: table; }
.tablerow { display: table-row; }
.tablecell { display: table-cell; }
</style>
Which allows you to visibly structure in a table structure without using tables, brilliant. There is however one caveat, IE7 and below does not have full support for the display property, it only understand the types inline, block, and none. Which means that if you were to use display:table-cell; IE7 would revert to display:block; and potentially ruin your well laid plans.
But wait! jQuery to the rescue!!
$(document).ready(function(){
if ($.browser.msie && $.browser.version <= 7)
{
$(".tablecell").wrap("<td />");
$(".tablerow").wrap("<tr />");
$(".table").wrapInner("<table class='newparenttable' />");
}
});
This simple piece of code will, if the browser is IE7, grab the elements and wrap them in actually table elements which is a wonderfully simple solution, to achieve a visual layout without having to re-write all of your code to suit the situation.
Re-triggering JavaScript methods after partial page updates without using ASP.Net Ajax Control Toolkit “UpdatePanelAnimationExtender”
ASP.Net Ajax Control Toolkit has a cool gadget called the UpdatePanelAnimationExtender control which sits at the bottom of the page and allows for specific events to occur, while an update panel updates and when it is finished. For the example I had a JavaScript function called loadonhoverEvent(), see below which caused every <td> element in a tablerow’s background to darken when the user hover over the element. Seems simple enough.
<script type="text/javascript">
function loadonhoverEvent() {
$(".ListGridView tr").hover(
function () {
$(this).children("td").css("background-color", "#EBEBEB");
}, function () {
$(this).children("td").css("background-color", "");
});
};
$(document).ready(function () { loadonhoverEvent(); });
</script>
However there was a minor problem, one of the controls only did a partial page postback to toggle the status of one of the fields. No Problem, Use UpdatePanelAnimationExtender, however this is a bit overkill for what I was intending.
All I needed was
protected void Page_Load(object sender, EventArgs e)
{
ScriptManager.RegisterStartupScript(this,this.GetType(),"thescript","loadonhoverEvent();",true);
}
The only problem I was having was I initially has the code as a self-executing function, styled after the jQuery source code which does the same. That would mean I would not need the $(document).ready() section as below:
(function loadonhoverEvent() {
$(".ListGridView tr").hover(
...
});
})();
But this would not work as it cannot call the function anymore, so I had to break it out of the self-executing block.
PHD Comics – Dark Matters Video
Check out the cool video Jorge Cham from PHDcomics created in conjunction with Physicists Daniel Whiteson and Jonathan Feng to talk about Dark Matter and how Cern’s collider is helping answer the question: What is it?
Dark Matters from PHD Comics on Vimeo.
MSQL Getting the Integer Value from a Hex Value that looks like a Decimal
Background
Just to give you come context I have included a little background as to why I’m trying to do this:
I have a table ‘cp’ which has an [id] field which is the identifier for each registration I have come realize that this is to be moved from a deployed solution to a cloud based as a result multiple accounts will be accessing the same objects and it would be best if they all start at 1 so rather than use the id field I have created an int field called label which will be used in conjunction with the account id to find the unique record per account.
When the registrations are printed out on the screen I want them to be displayed as a 5 digit HEX number and I can simply do the following in the c# code
value.ToString(“X5″)
However the problem existed with the existing entries in the database before I added the new label field. I have found several questions on StackOverflow with a similar topic but did not answer my question
- Converting a paragraph to hex notatation, then back to string
- Converting a String to HEX in SQL
- SQL Query convert integer to hex and hex to integer
The Problem
i have the fields with ids (1,2,3,—-,11,12,13,—etc) values 1-9 can simply be copied over as the value.ToString(“X”) will simply do nothing with those values however if I simply copied value 11 would return “B” which correctly is the HEX value of 11 however I want it to return “11″ so I need an SQL script which will convert 11 => 17 (which is the hex value of 0×11) so that when the application reads it it will output the value 11 to the screen.
This is only to occur once to convert the existing ids, which is why I’m wanting an SQL script to do it all in one batch rather than build it into the application.
What I would Like would be something like below so that it assumes the value in would be a hex value and convert it back to an Int.
UPDATE [cp] SET label_id = CONVERT(INT, ’0x’ + id);
I found that I could get the correct effect in C# by doing the following
int.Parse(value.ToString(),System.Globalization.NumberStyles.HexNumber).ToString()
The Solution
I asked this question on StackOverflow But as it turned out I managed to find a solution before anyone answered the question.
It was not the prettiest answer but could be used for multiple use-cases and I liked the simplicity of it.
CREATE FUNCTION ConvertFromBase
(
@value AS VARCHAR(MAX),
@base AS BIGINT
) RETURNS BIGINT AS BEGIN
-- just some variables
DECLARE @characters CHAR(36),
@result BIGINT,
@index SMALLINT;
-- initialize our charater set, our result, and the index
SELECT @characters = '0123456789abcdefghijklmnopqrstuvwxyz',
@result = 0,
@index = 0;
-- make sure we can make the base conversion. there can't
-- be a base 1, but you could support greater than base 36
-- if you add characters to the @charater string
IF @base < 2 OR @base > 36 RETURN NULL;
-- while we have characters to convert, convert them and
-- prepend them to the result. we start on the far right
-- and move to the left until we run out of digits. the
-- conversion is the standard (base ^ index) * digit
WHILE @index < LEN(@value)
SELECT @result = @result + POWER(@base, @index) *
(CHARINDEX
(SUBSTRING(@value, LEN(@value) - @index, 1)
, @characters) - 1
),
@index = @index + 1;
-- return the result
RETURN @result;
END
Then it can simply be called using
UPDATE [cp] SET label = dbo.ConvertFromBase(id,16);
I would like to thank the original author of this code Converting Hexadecimal or Binary to Decimal on SQL Server 2008 and 2005, though I am not using it exactly at the writer intended it is brilliantly simple.
Stop a Zombie Apocalypse ruining your holiday.
This is pure genius
No New Years Celebrations in Belfast?!
Looks like there is no public celebrations in Belfast this year, looking around there appears to be nothing going on. Yes there will be a few events in bars around Belfast such as Club Mono which is having a new year’s eve ball tonight. But looking at the Belfast city council websites, searching Google, I cannot find anything else going onto celebrate, even the Odyssey which hosted the MTV EMA and the Haloween party doesn’t seem to have anything announced which seems a bit sad.
Unfortunately Twitter seems to be having issues at the moment due to load, I would imagine as the rest of the world’s festivities kick off, so I cant check there, some people are saying that there will be something at the Odyssey but we’ll have to see. Come on Belfast, get it into gear and celebrate New Year’s properly!
Update: from the BBC News site:
“In the absence of an official city council-run event, revellers in Belfast are being advised by the council to attend events in local clubs and pubs.”
HTML 5 Canvas Signature
Whilst searching for what I would do to implement an electronic signature into a web application I came across this brilliant jQuery plugin by Thomas J Bradley which you should seriously look into if you are going to attempt this. It uses a HTML5 Canvas element which allows a user to draw a signature on the screen, submit it and return the result to be displayed to the user. It’s great.