I had a great job interview for a great company on tuesday just gone, they are a Microsoft Gold Certified Partner so that suits me down to the ground since I am a Microsoft fanboy! I have a second stage interview with them that will be technical, so it would probably involve a test and lots of questions, so I have decided to put in some more study into my profession and brush up on stuff, starting with C#.
I think myself as a fairly good C# programmer, not the best, but fairly good, but, there are some things that I have never needed to learn, in this post I will look at the checked keyword. The checked keyword is used to control the overflow-checking context of integral-type arithmetic operations and conversions - cited from the msdn docs.
At first glance, since some things are not always clear to me, my thoughts are - what does this mean?
Well, if we take a byte, we know that a byte can only have maximum value of 255, so any greater value than 255 would result in an overflow.
So what checked does is - if you perform an arithmetic operation that results in an overflow, it will throw an OverflowException, the following example shows this. class Program
{
static void Main()
{
byte a = 200;
byte b = 200;
byte c = checked((byte)(a + b));
Console.WriteLine(c);
}
}
Without the checked, the calculation would wrap giving c a value of 144.
I have never needed to use this yet, but I guess if an important calculation was being performed then I would consider it, for instance if it was going to lose a lot of money!
I had a second interview today for a job that I am very keen on getting, after the interview it struck me that somehow I must have done something wrong, infact it was during the question 'Do you have any questions?' I realised that I had somehow missed an opportunity or somehow screwed up. Even further it was after something said to me along the lines of 'well, you are just one of a number and we will have some feedback for you as soon as possible', followed again by 'Do you have any questions?'.
This sounded a lot like 'This is your last chance to ask us some deal breaking questions right now?' but I am hoping that this is just the way that I percieved it. Still I had no questions. Well, saying that I did have one question but that was asking wether they had used LINQ to NHibernate and then following up on that by saying how great LINQ is, which ended up sounding like an instruction (according to one of the interviewers, I am not sure if it was meant literaly however it made me feel uncomfortable, like I just gave an instruction to use LINQ to NHibernate, but this is not what I was indicating).
Although this was the only question I did ask, I don't think it was the right question, I failed to ask about the job, about what I might be doing, what my responsibilities would be and where I would fit into the team, it stands to reason that if your going to a job and the job details do not specifically answer these questions then maybe they should be asked.
The thing that I think stopped me from asking them is that I just assumed that, since I am going for a C# Developer role that I would pretty much be involved in writing C# Code in answer to some business idea, plan or problem, which is infact the case since I was already told that the position involves writing B2B enterprise applications.
Reflecting on myself after this interview, I think I may have babbled to much, when asked a direct questions, I seem to have gave an entire story rather than a direct answer, I am not sure why I did this, I think it may have been that I felt I had to prove something, which, I guess I did since thats what your supposed to do in job interviews? right?
I still feel, that getting this job will be the next step in my career, even though it ended on an anti-climax, assuming that the interview was a climax or a superficial climax for me and an anti-climax for them with me babbling on that it made me seem as if I am some big head who knows it all.
Anyway, I hope I get this job!
Recently at a job interview during a technical test I had to answer a question:
What is the difference between a delegate and a multicast delegate?
Unfortunately I did not have the answer, I have used delegates plenty but I have never obviously used them enough to know this difference, and the dissapointing thing is that the answer was glaringly obvious.
A multicast delegate is multiple instances of the same delegate chained together using the + operator, Events in .NET make use of this to attach multiple event handlers to an event.
An example: public delegate void SaySomething(string something);
class Program
{
static void Main(string[] args)
{
// At first, something is just a delegate
SaySomething something = new SaySomething((s) => Console.WriteLine(s));
// By adding together more instances of the SaySomething delegate.
// we get a multicast delegate
something += new SaySomething((s) => Console.WriteLine(s.ToUpper()));
something += new SaySomething((s) => Console.WriteLine(s.ToLower()));
something("Hello");
}
}
The output would give:
Hello HELLO hello
It pains me to think I have now been at this for 6 years and there are still aspects to C# that I have not yet come across, yet I do not think of myself as a bad developer, I am sure there are plenty of things that I am aware of that the interviewer has no idea about, and it makes me wonder if the whole point is to see areas of where I could improve my knowledge, or the purpose is to scrutinise my knowledge in a way that an incorrect answer would make me out to be some kind of fraud.
I guess at least now if this question comes up again I will be able to answer it.
At work I was implementing a search form that required a 3 tier drop down, so I thought the CascadingDropDown would be perfect for this.
It turns out that the CascadingDropDown was not quite right for the requirements, the last drop down in the 3 tiers had to be enabled and items had to be selectable but also had to filter the other 2 upper tiers.
After some thought, the only way to achieve this was having the ability to programatically add my own values to be passed to the knownCategoryValues argument of the web service method for a CascadingDropDown behavior.
To do this, I had to redefine a method on the CascadingDropDown behavior, I could then invoke this method to force the ajax callback for any CascadingDropDown of my choice. Here is that method. AjaxControlToolkit.CascadingDropDownBehavior.prototype._onParentChange2 = function(evt, inInit, moreKnownCatValues) {
/// <summary>
/// Handler for the parent drop down's change event
/// </summary>
/// <param name="evt" type="Object">
/// Set by the browser when called as an event handler (unused here)
/// </param>
/// <param name="inInit" type="Boolean">
/// Whether this is being called from the initialize method
/// </param>
/// <returns />
var e = this.get_element();
// Create the known category/value pairs string for sending to the helper web service
// Follow parent pointers so that the complete state can be sent
// Format: 'name1:value1;name2:value2;...'
var knownCategoryValues = '';
var parentControlID = this._parentControlID;
while (parentControlID) {
var parentElement = $get(parentControlID);
if (parentElement && (-1 != parentElement.selectedIndex)) {
var selectedValue = parentElement.options[parentElement.selectedIndex].value;
if (selectedValue && selectedValue != "") {
knownCategoryValues = parentElement.CascadingDropDownCategory + ':' + selectedValue + ';' + knownCategoryValues;
parentControlID = parentElement.CascadingDropDownParentControlID;
continue;
}
}
break;
}
if (knownCategoryValues != '' && this._lastParentValues == knownCategoryValues) {
return;
}
this._lastParentValues = knownCategoryValues;
// we have a parent but it doesn't have a valid value
//
if (knownCategoryValues == '' && this._parentControlID) {
this._setOptions(null, inInit);
return;
}
// Show the loading text (if any)
this._setOptions(null, inInit, true);
if (this._servicePath && this._serviceMethod) {
// Raise the populating event and optionally cancel the web service invocation
var eventArgs = new Sys.CancelEventArgs();
this.raisePopulating(eventArgs);
if (eventArgs.get_cancel()) {
return;
}
if (moreKnownCatValues) {
knownCategoryValues += ";" + moreKnownCatValues;
}
// Create the service parameters and optionally add the context parameter
// (thereby determining which method signature we're expecting...)
var params = { knownCategoryValues: knownCategoryValues, category: this._category };
if (this._useContextKey) {
params.contextKey = this._contextKey;
}
// Call the helper web service
Sys.Net.WebServiceProxy.invoke(this._servicePath, this._serviceMethod, false, params,
Function.createDelegate(this, this._onMethodComplete), Function.createDelegate(this, this._onMethodError));
$common.updateFormToRefreshATDeviceBuffer();
}
}
The method above is just a copy of _onParentChange method of the CascadingDropDown behavior, I called it _onParentChange2 and call this one when I want to invoke an ajax callback, the only difference is the extra argument moreKnownCatValues, the value of this argument will be appended to the knownCategoryValues which will get passed to the callback method. The changes to the original method have been bolded.
Now its possible to invoke a callback on any CascadingDropDown with the code below. $find('CDD1')._onParentChange2(null, false, "CountryId:25");
The line above will get a reference to a CascadingDropDown behaviour that I have given a BehaviorID of CDD1, it will then invoke the new _onParentChange2 method passing through my extra value of CountryId:25.
Its a shame that the CascadingDropDown does not have a more elegant approach for adding to the knownCategoryValues programatically, but at least this workaround is not to hacky.
I am quite impressed with IE8 beta 2, annoying bugs aside. One feature of IE8 that is very useful is accelerators, I decided to give it a shot today to see if I could get one working and its surprisingly easy.
You can create an accelerator by writing an xml document in the OpenService Format Specification for Accelerators and making the file accessible from a website, then creating a button with an onclick handler that calls window.external.AddService(url_to_xml_file).
As a quick test I created an xml file to describe an accelerator for my blog similar to the following. <?xml version="1.0" encoding="UTF-8"?> <os:openServiceDescription xmlns:os="http://www.microsoft.com/schemas/openservicedescription/1.0"> <os:homepageUrl>http://www.yoursite.com</os:homepageUrl> <os:display> <os:name>Blog with your site</os:name> <os:icon>http://www.yoursite.com/favicon.ico</os:icon> <os:description>Blog on your site easily</os:description> </os:display> <os:activity category="Blog"> <os:activityAction context="selection"> <os:execute action="http://www.yoursite.com/blog/add_entry.aspx" method="get"> <os:parameter name="title" value="{selection}" type="text" /> </os:execute> </os:activityAction> </os:activity> </os:openServiceDescription>
The most notable tags are described below:-
- <os:activityAction context="selection">
The value 'selection' of the context attribute of this tag specifies that this accelerator will be available when the user selects a block of text, there are two other possible options:-
'document' - makes the accelerator available for the whole document. 'link' - makes the accelerator available when the user selects a link.
- <os:execute action="http://www.yoursite.com/blog/add_entry.aspx" method="get">
The action of the execute tag does the business of redirecting to, or in this case, accelerating to the add entry page of the blog service, we can do this via a http GET or POST as described by the method attribute.
- <os:parameter name="title" value="{selection}" type="text" />
You can add a collection of these parameters that will be either sent as GET or POST vars, depending what you set for the method of the execute tag above, {selection} is a placeholder for information that is retrieved from the page that we are accelerating from, there are many possible options here which I will not list as they are available on msdn accelerator docs.
Once this xml document is ready and uploaded all that is needed is a link to it from a html document like the one below. <html> <head><title>My Blog Accelerator</title></head> <body> <button id="installButton" onclick="window.external.AddService('my_accelerator.xml');">Add My Blog Accelerator</button> </body> </html>
The example above would require the html document to be in the same path as my_accelerator.xml.
And thats all there is to it, the documentation for developing accelerators can be found on msdn.
I just released my latest updates to the Ajax Snippets project, the following notes summarise whats new.
ADDED
- SnippetCallback now has an action list item, 'Javascript function stubs to clipboard'
- SnippetCallback now has a designer action list option to configure the callback through a wizard
- Ability to override default value extractors and add new ones
- The Map parameters wizard step of the SnippetCallback wizard now best guesses the mappings by matching the name of the parameter to a control id
- Snippets can now be used as ordinary web user controls where they can be dropped straight on a page with the advantage of SnippetCallbacks
FIXED
- Javascript function stubs to clipboard feature was creating incorrect javascript
- SnippetCallback wizard was not saving WebServiceVirtualPath property
- Snippets now throw the correct exception if errors occur whilst retrieving them from a web service
- SnippetManager was not managing css references correctly
I almost wrote off the idea that this was even remotely possible until I looked into it more on msdn and found an example. /// <reference name "Ajax.js" assembly="System.Web.Extensions, ..." />
After doctoring it for my own script and assembly it was not working after trying several different renditions and wondering if it would ever work with the missing = in the name attribute and what the hell the ... was, I finally came to something that worked. /// <reference name="AjaxSnippets.SnippetManager.js" assembly="AjaxSnippets" />
SnippetManager.js is the file in my AjaxSnippets.dll.
The only snag is that I have not been able to get it to work in a script block inside an aspx page, but it does work in a js file.
I just checked out the 1541 Ultimate website to see the status of the second batch and was surprised to find a nice video of the batch in production.
I am lucky enough to be receiving one of these cards that will bring sd cards and ethernet to my C64.
In an SnippetCallback, when defining mapping between a web service method parameter and a control, there is a client-side function for each control type that deals with extracting the value from the control and passing it as the web service methods parameter.
By default most of the simple web controls (TextBox, Checkbox, RadioButtonList, etc) that come out-of-the-box with ASP.NET already have a corresponding client-side value extractor function defined, but obviously this is not good enough if you want to use custom controls.
To override these or add new ones there are two things that need to be done, first off a new extractor function would have to be defined in configuration. <configSections> <section name="ajaxSnippets" type="AjaxSnippets.Configuration.AjaxSnippetsConfigurationSection, AjaxSnippets"/> </configSections> <ajaxSnippets> <extractors> <add controlType="System.Web.UI.WebControls.TextBox" functionName="myCustomTextBoxExtractor" /> <add controlType="My.Custom.Control" functionName="fromMyCustomControl" /> </extractors> </ajaxSnippets>
In this example, I am overriding the textbox extractor with my own function myCustomTextBoxExtractor, secondly I am defining a new value extractor fromMyCustomControl for My.Custom.Control.
Now the next thing that needs to be done is to write the client functions, this can be done by creating a new javascript file, which would need to be included in the page that Ajax Snippets are being used. AjaxSnippets.ValueExtractors.prototype.myCustomTextBoxExtractor = function(id){
var el = $get(id);
// Extract value from el and return it here
}
AjaxSnippets.ValueExtractors.prototype.fromMyCustomControl = function(id){
var el = $get(id);
// Extract value from el and return it here
}
Ajax Snippets has a ValueExtractors class, and its simply just a job of adding functions to its prototype.
To give a better example, the ValueExtractors default prototype is below in full, in its current state from Ajax Snippets 0.3a release. AjaxSnippets.ValueExtractors.prototype = {
fromAny: function(id) {
return id;
},
fromTextBox: function(id) {
return $get(id).value;
},
fromDropDown: function(id) {
var el = $get(id);
return el.options[el.selectedIndex].value;
},
fromRadioList: function(id) {
var i, els = $get(id).getElementsByTagName('input');
for (i = 0; i < els.length; i++) {
if (els[i].checked) { return els[i].value; }
}
return null;
},
fromCheckList: function(id) {
var i, els = $get(id).getElementsByTagName('input'), vals = new Array();
for (i = 0; i < els.length; i++) {
vals.push(els[i].checked);
}
return vals;
},
fromListBox: function(id) {
var i, el = $get(id), vals = new Array();
for (i = 0; i < el.options.length; i++) {
if (el.options[i].selected) { vals.push(el.options[i].value); }
}
return vals;
},
fromCheckBox: function(id) {
return $get(id).checked;
}
};
|