Monday, November 15, 2010

String Manipulation in .NET

String is one of the most frequently used data types in programming. There are quite a few string functions out there and all of those functions are implemented differently in different programming languages. I am going to show here how this is done in .NET Framework.
In the example below I am going to demonstrate the use of StringBuilder class. The below example receives a string as a parameter and then reverses it:
private string ReverseString(String str)
{
    if (str != string.Empty)
    {
        StringBuilder sb = new StringBuilder();
        for (int i = str.Length - 1; i >= 0; i--)
        {
            sb.Append(str.Substring(i, 1));
        }
        str = sb.ToString();
    }
    return str;
}



Here is another example that accepts a string, splits it, and then reverses the order of words in that string:
private void ReOrderString()
{

string str = "Token A#$%@&Token B#$%@&Token C#$%@&Token D#$%@&Token E"; 
string[] Split = str.Split(new string[] { "#$%@&" }, StringSplitOptions.None);
    StringBuilder sb = new StringBuilder();
    for (int i = Split.Length - 1; i >= 0; i--)
    {
        sb.Append(Split[i]);
        if (i > 0)
            sb.Append("#$%@&");
    }
    lblOriginalString.Text = "Token A#$%@&Token B#$%@&Token C#$%@&Token D#$%@&Token E";
    lblProcessedString.Text = sb.ToString();
}

Wednesday, July 14, 2010

Technical Interview: Frequently Asked Questions & Answers

Most of the time, when applying for software development jobs, be prepared to answer the following questions:
1. General Programming Questions:
  1. What is the difference between passing parameter by value versus passing it by reference?
  2. What is boxing / unboxing?
  3. What is implicit conversion?
  4. What is explicit conversion?
  5. Late Binding vs. Early Binding in VB.NET
2. OO Questions:
  1. What are the main concepts of Object Oriented development?
  2. What is an Abstract Class?
  3. What is an Interface?
  4. What is Reflection?
  5. What design patterns do you know?
  6. What is the difference between value types vs. reference types?
3. SQL Questions:
  1. How do you join two tables (SQL)?
  2. What are the differences between different types of join?
  3. What is UNION?
4. HTML Questions:
  1. How do you make a table cell that runs across several columns?
  2. How do you make a table cell that spans across multiple rows?
OO Answers:
1. Object-oriented programming (OOP) is a programming paradigm that uses "objects" – data structures consisting of data fields and methods together with their interactions – to design applications and computer programs. Programming techniques may include features such as data abstraction, encapsulation, modularity, polymorphism, and inheritance.

2. Abstract classes are closely related to interfaces. They are classes that cannot be instantiated, and are frequently either partially implemented, or not at all implemented. One key difference between abstract classes and interfaces is that a class may implement an unlimited number of interfaces, but may inherit from only one abstract (or any other kind of) class. A class that is derived from an abstract class may still implement interfaces. Abstract classes are useful when creating components because they allow you specify an invariant level of functionality in some methods, but leave the implementation of other methods until a specific implementation of that class is needed. They also version well, because if additional functionality is needed in derived classes, it can be added to the base class without breaking code.

An abstract class is denoted in Visual Basic by the keyword MustInherit. In C#, the abstract modifier is used. Any methods that are meant to be invariant may be coded into the base class, but any methods that are to be implemented are marked in Visual Basic with the MustOverride modifier. In C#, the methods are marked abstract. The following example shows an abstract class:

' Visual Basic
Public MustInherit Class WashingMachine

Sub New()

' Code to instantiate the class goes here.

End sub

Public MustOverride Sub Wash
Public MustOverride Sub Rinse (loadSize as Integer)
Public MustOverride Function Spin (speed as Integer) as Long

End Class

// C#
abstract class WashingMachine
{
    public WashingMachine()
   {
         // Code to initialize the class goes here.
    }
    abstract public void Wash();
    abstract public void Rinse(int loadSize);
    abstract public long Spin(int speed);
}
 
In the above example, an abstract class is declared with one implemented method and three unimplemented methods. A class inheriting from this class would have to implement the Wash, Rinse, and Spin methods. The following example shows what the implementation of this class might look like:
 
' Visual Basic
Public Class MyWashingMachine
                 Inherits WashingMachine

Public Overrides Sub Wash()

    ' Wash code goes here

End Sub

Public Overrides Sub Rinse (loadSize as Integer)

' Rinse code goes here

End Sub

Public Overrides Function Spin (speed as Integer) as Long

' Spin code goes here

End Sub

End Class

// C#
class MyWashingMachine : WashingMachine
{
    public MyWashingMachine()
   {
    // Initialization code goes here.
   }

    override public void Wash()
   {
        // Wash code goes here.
    }

    override public void Rinse(int loadSize)
    {
        // Rinse code goes here.
    }

    override public long Spin(int speed)
    {
        // Spin code goes here.
    }
}
 
When implementing an abstract class, you must implement each abstract (MustOverride) method in that class, and each implemented method must receive the same number and type of arguments, and have the same return value, as the method specified in the abstract class.

3. An interface contains only the signatures of methods, delegates or events. The implementation of the methods is done in the class that implements the interface, as shown in the following example:

interface ISampleInterface
{
    void SampleMethod();
}

class ImplementationClass : ISampleInterface
{
    // Explicit interface member implementation:

    void ISampleInterface.SampleMethod()
    {
        // Method implementation.
    }

    static void Main()
   {
        // Declare an interface instance.

        ISampleInterface obj = new ImplementationClass();

        // Call the member.
        obj.SampleMethod();
    }
}

Remarks

 An interface can be a member of a namespace or a class and can contain signatures of the following members:
•Methods
•Properties
•Indexers
•Events

An interface can inherit from one or more base interfaces.
When a base type list contains a base class and interfaces, the base class must come first in the list.

A class that implements an interface can explicitly implement members of that interface. An explicitly implemented member cannot be accessed through a class instance, but only through an instance of the interface, for example:

For more details and code examples on explicit interface implementation, see Explicit Interface Implementation (C# Programming Guide).

Example
 The following example demonstrates interface implementation. In this example, the interface IPoint contains the property declaration, which is responsible for setting and getting the values of the fields. The class Point contains the property implementation.

// keyword_interface_2.cs
// Interface implementation
using System;

interface IPoint
{
// Property signatures:
    int x { get; set; }
    int y { get; set; }
}

class Point : IPoint
{
    // Fields:
    private int _x;
    private int _y;

// Constructor:
public Point(int x, int y)
{
    _x = x;
    _y = y;
}
// Property implementation:
public int x
{
    get
    {
        return _x;
    }
    set
    {
        _x = value;
    }
}

public int y
    {
        get
        {
            return _y;
        }
set
        {
            _y = value;
        }
    }
}

class MainClass
{
    static void PrintPoint(IPoint p)

    {
        Console.WriteLine("x={0}, y={1}", p.x, p.y);
    }
    static void Main()
    {
        Point p = new Point(2, 3);
        Console.Write("My Point: ");
        PrintPoint(p);
    }
}

4. Design Patterns: check this MSDN  resource: http://msdn.microsoft.com/en-us/magazine/cc301852.aspx


SQL Answers:
1 & 2. SQL joins are used to query data from two or more tables, based on a relationship between certain columns in these tables. Different SQL JOINs:
JOIN: Return rows when there is at least one match in both tables
LEFT JOIN: Return all rows from the left table, even if there are no matches in the right table
RIGHT JOIN: Return all rows from the right table, even if there are no matches in the left table
FULL JOIN: Return rows when there is a match in one of the tables
 
SQL INNER JOIN Keyword
The INNER JOIN keyword return rows when there is at least one match in both tables.
SQL INNER JOIN Syntax
SELECT column_name(s) FROM table_name1
INNER JOIN table_name2 ON table_name1.column_name = table_name2.column_name

SQL LEFT JOIN Keyword
The LEFT JOIN keyword returns all rows from the left table (table_name1), even if there are no matches in the right table (table_name2).

 SQL LEFT JOIN Syntax
SELECT column_name(s) FROM table_name1
LEFT JOIN table_name2 ON table_name1.column_name=table_name2.column_name

PS: In some databases LEFT JOIN is called LEFT OUTER JOIN.

SQL RIGHT JOIN Keyword
The RIGHT JOIN keyword Return all rows from the right table (table_name2), even if there are no matches in the left table (table_name1).

SQL RIGHT JOIN Syntax
SELECT column_name(s) FROM table_name1
RIGHT JOIN table_name2 ON table_name1.column_name = table_name2.column_name

PS: In some databases RIGHT JOIN is called RIGHT OUTER JOIN.

SQL FULL JOIN Keyword
The FULL JOIN keyword return rows when there is a match in one of the tables.

SQL FULL JOIN Syntax
SELECT column_name(s) FROM table_name1
FULL JOIN table_name2 ON table_name1.column_name = table_name2.column_name

3. UNION
The UNION operator is used to combine the result-set of two or more SELECT statements.

Notice that each SELECT statement within the UNION must have the same number of columns. The columns must also have similar data types. Also, the columns in each SELECT statement must be in the same order.

Note: The UNION operator selects only distinct values by default. To allow duplicate values, use UNION ALL.

SQL UNION Syntax
SELECT column_name(s) FROM table_name1
UNION
SELECT column_name(s) FROM table_name2

SQL UNION ALL Syntax
SELECT column_name(s) FROM table_name1
UNION ALL
SELECT column_name(s) FROM table_name2

Note: The column names in the result-set of a UNION are always equal to the column names in the first SELECT statement in the UNION.

Wednesday, April 14, 2010

Visual Studio 2008 Cheet Sheet

The other day I searched for a keyboard shortcut reference for Visual Studio 2008. After I saw the list of all the different shortcuts, I compiled a list of Visual Studio 2008 keyboard shortcuts for tasks that I use most frequently:

F5Start your project in debug mode
F7 & Shift-F7Show the code windows & show the designer window
Alt-Enter or F4Show the properties panel for a selected object
F6 / Shift-F6 / Ctrl-Shift-B Build Solution / Build Project / Build Solution
Shift-Alt-CAdd a new class to the project
Ctrl-Shift-A / Alt-Shift-AAdd a new item to your project / add an existing item to your project
Ctrl-K + Ctrl-CComment a selected block of code
Ctrl-K + Ctrl-UUncomment a selected block of code
Ctrl-M + Ctrl-O /
Ctrl-M + Ctrl-P
Collapse all code to definitions / Expand all code (stop collapsing)
Ctrl-K + Ctrl-KToggle code bookmark
Ctrl-K + Ctrl-NGo to next bookmark
Ctrl-K + Ctrl-PGo to previous bookmark
Ctrl-Shift-TabScroll backwards through open windows
F9 Set or remove a breakpoint
F12 Go to definition
Shift-F12 Display all references

Below are some of the VS2008 Keyboard Shortcuts Cheat Sheets that originally came up in my search:

Cheat Sheet with Key Images

22 Visual Studio Short Keys and 6 Short-cut Ways to Custom Jobs: A List of Tips and Tricks

Visual C# 2008 Keybinding Reference Poster

Thursday, January 14, 2010

ASP.NET AJAX Date Picker

    Recently, while working on my Tai Chi club’s website, I realized that my club’s website is lacking the capability of scheduling a Tai Chi class (or any other type of event for that matter). In order to schedule any type of event, I would need to specify that event’s date and time, location, and maybe some kind of description or explanation of what’s going to be happening during that event. So, I started looking at different ways to create a Date Picker. After some research, I found out that it is very easily achieved with Ajax Control Toolkit, which provides CalendarExtender control.

     The CalendarExtender control is designed to extend a TextBox. Its default behavior prompts the end user to pick a date from a calendar when the user clicks inside a textbox associated with the CalendarExtender.

This is what it looks like:
It is also possible to add a button to the right of the textbox and have the calendar popup whenever the user clicks that button (all you need to do is specify the button's ID as value of PopupButtonID property).

To customize the look of the popup calendar, add all or some of the following classes to your stylesheet and customize them appropriately:

To achieve the look above, I added the following class to my stylesheet:

.ajax__calendar_active
{
    border: solid 2px green;
   font-weight: bold;
}

and then in my page’s Page_Load event added the following line of code:

this.CalendarExtender1.SelectedDate = DateTime.Today;

.ajax__calendar_container
you can change the style of outer container of calendar control. Outer container holds the .ajax__calendar_header, .ajax__calendar_body, .ajax__calendar_footer as child container css classes.

.ajax__calendar_header
This css class holds the previous arrow, next arrow and current Month, Year. Child Css Classes are .ajax__calendar_prev, .ajax__calendar_next, .ajax__calendar_title.

.ajax__calendar_prev
This class is associated with an element that displays the left side arrow to view the previous month, year or decade based on the current view of calendar.

.ajax__calendar_title
This class is associated with an element that displays the month name, year or decade range according to the current view of the calendar control.

.ajax__calendar_next
This class is associated with an element that displays the right side arrow to view the next month, year or decade based on the current view of calendar.

.ajax__calendar_body
It is a container of the elements for displaying the different views of calendar such as month view, year view and day view. This class holds the .ajax__calendar_days, .ajax__calendar_months and .ajax__calendar_years as child css classes

.ajax__calendar_days
It changes the look of day view of month. Child Css Classes are .ajax__calendar_dayname and .ajax__calendar_day.

.ajax__calendar_dayname
It changes the look of day name at the top of day view of month.

.ajax__calendar_day
This class is associated with the day layout of month.

.ajax__calendar_months
It controls the month layout of month view. Child Css class is .ajax__calendar_month.

.ajax__calendar_month
It controls the look and feel of month names of month view.

.ajax__calendar_years
This class controls the layout of year view of calendar control. Child css class: .ajax__calendar_year.

.ajax__calendar_year
It changes the look and feel of years in the year layout of year view.

.ajax__calendar_footer
It is associated with Footer container that holds the current date.

.ajax__calendar_today
It displays the current date in the footer container of calendar control.

.ajax__calendar_hover
Dynamic color behavior onmouseover can be modified by using this class. It can be used with .ajax__calendar_title, .ajax__calendar_day, .ajax__calendar_month, .ajax__calendar_year, .ajax__calendar_today to change the mouse over behavior.

.ajax__calendar_active
It sets the style of currently selected date.

.ajax__calendar_other
It sets the style of day, month name or year that is outside the current view e.g. days that don’t belong to the currently visible month.