Taswar Bhatti
The synonyms of software simplicity
dotnet C#

Here is another C# tip.
Most of the time when you are trying to get a value from dictionary you would try to check if the key exist or not something like.

But one can use TryGetValue

The benefit of using TryGetValue is that the dictionary is synchronized, there is no race condition.

WPF

Here is a hint when using WPF and you may come into a situation where you want to databind an element to its Parent ViewModel.

ViewModel

As an example lets say you have a view that is binding to ViewModel and inside your xaml you have a control that has DataTemplate
The code Below we are using the BusyIndicator from https://wpftoolkit.codeplex.com/

Our ViewModel looks like

We would assume that the property would bind, but actually it doesn’t since the datatemplate is there.

So what we need to do is bind it to its parent viewmodel

What we need to provide is RelativeSource and the AncestorType, below we are using UserControl but it could also be Window or a provided type like AncestorType={x:Type XYZObject}

iis8

If you are running Windows Server 2012 with IIS8 and you got this error HTTP Error 500.19 – Internal Server Error. If you web.config file contains “handler” or “modules” tag.

In order to fix this, launch cmd or powershell and execute these 2 commands inside of C:\Windows\System32\InetSrv

This will unlock the handlers and modules section.

dotnet C#

In C# there is a params keyword which allows a method parameter to take variable number of arguments. MSDN

Some notes on param

  • It must always be the last parameter in the function/method definition
  • There can only be one params keyword in a function/method parameter signature

Example:

But by changing it to params we can change the call and simplify it

There is no performance bottleneck in using params because the compiler is basically changing your code into an array for you, thus it is basically just syntactic sugar for C#.

phantomjs

Thanks to everyone who came out last night at OttawaJS for the phantomjs talk, the slides are located at slid.es.
You can find the code in my github account. https://github.com/taswar/ottawajs-dec2013

A recap of things we covered:

  • Simple screencapture
  • CoffeeScript with phantomjs
  • Scraping hackernews and dzone site for links
  • using webdriver to integrate with phantomjs (python and ruby)
  • Using remote webdriver to integrate phantomjs with jruby
  • Rails app with phantomjs integrated to take screencapture
dotnet C#

In C# there is the out parameter keyword which causes arguments to be passed by reference. It is like the ref keyword, except that ref requires that the variable be initialized before it is passed.

Example:

The out parameter keyword provides a way for a method to output multiple values from a method without using an array or object, since the return statement only allows one output, by using out one can return multiple values.

Last but not least I personally do not recommend using out keyword and also from
The Framework Design Guidelines it also recommend to avoid using out. If you have multiple returns use an object that wraps around the value is a better design.

dotnet C#

In C# when using the ref keyword it causes an argument to be passed by reference, not by value.
In order to use a ref as a parameter, both the method definition and the calling method must explicitly use the ref keyword, and also the variable must be initialized before passing in.

In the example below we have two methods, one using the ref keyword the other not.

But what about objects that are reference types. Here is where the confusion starts that the concept of passing by reference with the concept of reference types, the concepts are not the same. A method parameter can pass in a ref type regardless of its type (value or reference).

Below is an example that shows when a Person object reference type is used.

Last but not least I personally do not recommend using ref keyword and also from
The Framework Design Guidelines it also recommend to avoid using ref.

Framework Design Guideline

dotnet C#

.NET provides in its System.IO namespace the Path class which performs operations on String instances that contain file or directory path information. These operations are performed in a cross-platform manner.

Most of the time we see develeopers writing code like

But by using Path.Combine we can provide a cross platform path

By using the Path and Combine method, one can at least eliminate some of the headache of porting to another platform.

GhostDoc

Recently have been going through some old code to review the comments in them from other developers and what I find out is developers tend to have really bad comments & documentation in their code

Example:

From the above code it is obvious that it is the constructor but does the comment tell me anything? I understand that sometimes it may be hard to document or comment certain method, thus I really strongly suggest at least use GhostDoc

What is GhostDoc?
GhostDoc is a Visual Studio extension that automatically generates XML documentation comments for methods and properties based on their type, parameters, name, and other contextual information.

Here is the result of using GhostDoc on the Person Class

As one can see Ghostdoc does a better job, don’t get me wrong GhostDoc still requires one to go through the comments here is an example where it tries to make sense.

Riches the text selection changed. ????? As you can see it didn’t do one of the best job for this comment, but at least one can modify it and it may become “RichTextBox selection has changed event handler”

Personally I would recommend using the tool but just take it with a grain of salt and always check the documentation that it generates.

linq

Here is a LINQ tip where you may wish to order a collection with an existing ordering of another collection.

Example:

Currently as it stands our data is stored in the order of { 5, 13, 7, 1, 8 } and we wish to order them in terms of { 1, 8, 5, 7, 13 }, we can use LINQ OrderBy to do so.

By doing so we end up with { 1, 8, 5, 7, 13 }, now if we wanted to put elements that dont have matching at the end of the list we can modify the OrderBy like this.

UA-4524639-2