Friday, 24 February 2012

How to Convert a Value to a Type when both defined in String?

static void Main(string[] args) {
Object result =
"System.Nullable`1[[System.DateTime, mscorlib, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089]]",
"2012-02-23 10:00:00");

public static Object ConvertValue(string typeInString, string value) {
Type originalType = Type.GetType(typeInString);

var underlyingType = Nullable.GetUnderlyingType(originalType);

// if underlyingType has null value, it means the original type wasn't nullable
object instance = Convert.ChangeType(value, underlyingType ?? originalType);

return instance; } 

Wednesday, 15 February 2012

How to Create QR Code Image Using Google API?

Make a request to this page:

chl query string gets a url then the page generates a code like below:

Then using a QR Code reader (an app on your mobile), you can scan this image and it takes you to this url.

Friday, 3 February 2012

All You'd Need to Know About String Type in C#

  • string is an alias for the String object which means they are equivalent.
  • String object is an Immutable Reference Type because it inherits directly from System.Object.
  • String object is not a Value Type because it doesn't inherit from System.ValueType (although the ValueType itself inherits from System.Object ultimately) but behaves like a Value Type.
  • A String object is internally stored as a Read-Only Collection of Char Objects.
What Does it Mean "String Object is Immutable"?
It simply means that once the object is defined, it can not be changed internally. If you change its value once initialized, then a new String Object will be created and the old one becomes ready for Garbage Collection.

In What Way a String Object Behaves Like a Value Type?
  1. Equality Operators "==", compares the value within the 2 String Objects like Value Types and unlike other Reference Types.
How was it achieved?
Well, you can override any operator that you like in classes including the Equality and Inequality operators. This is how:
public static bool operator ==(
    string a,
    string b
public static bool operator !=(
    string a,
    string b

In What Way a String Object Behaves like a Reference Type?
It has a reference and an actual object.
  1. Parameter Passing: When you pass a String object to a method as an input parameter, its reference is copied and passed while the object stays the same (in the String Intern Pool).
  2. In assignments, It makes a copy of the pointer and points to the same object in the String Intern Pool. String.Clone() also just returns a reference to the same String Object.
static string messageVar = "C#";
public static void Main(string[] args)
    bool isSame = Test(messageVar); //true

    // what about in assignement?
    string messageVar2 = messageVar;
    isSame = Object.ReferenceEquals(messageVar2, messageVar);//also true
public static bool Test(string messageParam)
    // logic
    bool isSame = Object.ReferenceEquals(messageParam, messageVar);
    return isSame;
What is String Intern Pool?
Where String objects are cached to achieve better memory management.


Thursday, 2 February 2012

How to Create and Use A Twitter Application?

On Twitter, everyone knows that you can create User Accounts but not everyone knows that you can also create Applications!

Twitter Applications can be used to send automatic tweets or re-tweets to other user accounts that have authorized this application to post tweets on their wall!

Examples Where Can It Be Used?
  • For example, you have a page on your site and you'd like to allow the user, who has logged on to your system, to send the link to your page on his/her twitter account.
  • Assuming you have a system which publishes articles, posts, videos and other types of contents onto a different website or webservice. Using a Twitter Application, you can automatically post a message unto your Company Twitter Account that such contents have been released. 
  • Imagine Burger King publishes Promotional Offers on their website, using a Twitter Application, they can publish such offers to their Company Twitter Account automatically when such offers become available.
  • Imagine BBC has a Twitter Account e.g. BBC_Business and every news that they publish unto their website, they'd like to be published automatically to this Twitter Account. Using Twitter that would be possible.
  • ...
I think any Company which publishes news/articles/information could potentially benefit from this.

How to Create a Twitter Application?
Each Twitter User can create 1 or more Twitter Applications. To create a new Application for your account go here:

What are Consumer Tokens?
Once your Twitter Application is created, it generates 2 main keys called ConsumerKey and ConsumerSecretKey.

These 2 keys will be used later so that you can authorize this app to post to a user account.

What are the Access Tokens?
Access tokens are 2 tokens using which you allow a Twitter Application to have access or post tweets on another Twitter Account; therefore Each Access Token Pair belongs to only one Twitter User Account and one Twitter Application.

How to Create Access Tokens?
The below page, would generate the the access tokens for a given application.

How to Connect to the Twitter API and Get User Details or Tweet?
Ok, by now you have 4 keys; 2 Consumer Tokens and 2 Access Tokens.

Download Twitterizer, using which you can communicate with the Twitter API.

Once you have those 4 keys and you have the Twitterizer, now you can do what you want. Posting an update to a user account using the newly created Twitter Application:

Another Tutorial:

Wednesday, 1 February 2012

NUnit with TeamCity, Working with IFrames When Writing System Tests

System tests are structured differently than normal unit tests in the sense that we won't have specific Arrange/Act/Verify sections. System tests have to test whether the whole process works correctly and can contain many steps.

public void LoginRegisterPage_WithValidEmailAddress_ShouldSendReminderEmail()

// go to loginregister url

// click the forgotten password
this.ClickElementByLinkText("Forgotten your password?");

// enter your email address
this.TypeInTextBox("PasswordResetRequest1_PasswordResetRequest_Username", TestEmailAddress);
DateTime dateTimeBeforeSubmit = DateTime.Now;

// click submit

// assert Thank You

// wait for 2sec

// Find the email log from database
string urlForResettingPassword = getPasswordReminderUrl(dateTimeBeforeSubmit);

// navigate to the url

string newPassword = this.generateRandomPassword();
// enter a new password

this.TypeInTextBox("PasswordReset1_PasswordReset_Password", newPassword);
this.TypeInTextBox("PasswordReset1_PasswordReset_PasswordConfirmation", newPassword);

// click submit

// Thank you exists
var pageTitle = this.FireFoxWebDriver.FindElement(By.XPath(@"//div/h1"));

Assert.AreEqual("Thank you", pageTitle.Text);

// go to the login page


// login using the new password
this.TypeInTextBox("UserLogin1_UserLogin_Username", TestEmailAddress);
this.TypeInTextBox("UserLogin1_UserLogin_Password", newPassword);

// verify whether the login was successful