Method Override vs Method Overload

February 19, 2012 Leave a comment

Some people might get confused between method overriding and method overloading, here i will explain the differences between these two methods and how each one of them is used:

Method Overriding means that you have 2 methods in 2 different classes with the same name, same parameters (signatures) but with different implementations in each class, mostly this happen between a base (abstract) class and derived class. Here is an example for method overriding:

Base Class:

Class Animal

protected string Name;

public string WhatAmI()

{

return “I am an animal”;

}

Derived Class:

Class Dog: Animal

public string WhatAmI()

{

return “I am a dog”;

}

Method Overloading means that you have 2 methods in the same class with the same name but different parameters (signatures), example for method overloading:

class Calculations
{
public static int Add(int number1, int number2)
{
return number1 + number2 ;
}

public static int Add(int number1, int number2, int number3)
{
return number1 + number2 + number3;
}
}

Advertisements

SQL CTE (common table expression)

February 17, 2012 Leave a comment

A common table expression (CTE) can be thought of as a temporary result set that is defined within the execution scope of a single SELECT, INSERT, UPDATE, DELETE, or CREATE VIEW statement. A CTE is similar to a derived table in that it is not stored as an object and lasts only for the duration of the query. Unlike a derived table, a CTE can be self-referencing and can be referenced multiple times in the same query.

A CTE can be used to:

  • Create a recursive query. For more information, see Recursive Queries Using Common Table Expressions.
  • Substitute for a view when the general use of a view is not required; that is, you do not have to store the definition in metadata.
  • Enable grouping by a column that is derived from a scalar subselect, or a function that is either not deterministic or has external access.
  • Reference the resulting table multiple times in the same statement.

Using a CTE offers the advantages of improved readability and ease in maintenance of complex queries. The query can be divided into separate, simple, logical building blocks. These simple blocks can then be used to build more complex, interim CTEs until the final result set is generated.

CTEs can be defined in user-defined routines, such as functions, stored procedures, triggers, or views.

SQL CTE Example

WITH ProductAndCategoryNamesOverTenDollars (ProductName, CategoryName, UnitPrice) AS
(
SELECT
p.ProductName,
c.CategoryName,
p.UnitPrice
FROM Products p
INNER JOIN Categories c ON
c.CategoryID = p.CategoryID
WHERE p.UnitPrice > 10.0
)

SELECT *
FROM ProductAndCategoryNamesOverTenDollars
ORDER BY CategoryName ASC, UnitPrice ASC, ProductName ASC

The CTE should be used directly by the query following it since it will be deleted after that, so you can’t define a cte and use it after 2 or 3 queries since an error will be generated telling you that the table ctetable1 (for example) doesn’t exist.

For Recursive Queries Using Common Table Expressions check this link:

http://msdn.microsoft.com/en-us/library/ms186243.aspx

The network BIOS command limit has been reached

October 21, 2011 Leave a comment

When you are building an asp.net deployment project, you may often recieve an ASPNETCOMPILER error “The network BIOS command limit has been reached”
. As describes by Microsoft, the error occurs because of the following reasons:

  • This issue may occur if the client computer submits simultaneous, long-term requests against a file server that uses the Server Message
    Block (SMB) protocol. An example of a long-term request is when a client computer uses the FindFirstChangeNotification function to
    monitor a server share for changes.
  • This issue may occur if the MaxCmds registry value setting on the client is less than 50, or the MaxMpxCt registry value
    setting on the server is less than 50.

To resolve this issue, verify that the MaxCmds and MaxMpxCt registry values are set to 50 or more. To do this, follow these steps:

  1. Click Start -> Run – > “regedit”
  2. Navigate to the following registry key:

    HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\lanmanworkstation\parameters

  3. Open the MaxCmds entry in the right listview.
  4. In the Value data, enter a value of 50 or more.
  5. Navigate to the following registry key:

    HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\lanmanserver\parameters

  6. Open the MaxMpxCt entry in the right listview.
  7. In the Value data, enter a value of 50 or more.
  8. Restart your computer for the changes to take effect.

Note The MaxCmds and MaxMpxCt registry entries are REG_DWORD decimal entries. If they do not exist on your computer, you can create them as new REG_DWORD values. The range of values for these registry entries is between 0 and 65535.

String Optimization in VB.NET

October 21, 2011 Leave a comment

This article describes a set of tips and tricks for using strings in vb.net that will boost the performance of your application especially
when the code contains lots of string related operations such as string concatenation.

String Declaration

The best way to declare a string variable is to set it to an empty string directly after declaration. It is a common mistake for most .net
developers to set the string to “” or Nothing. This is wrong because the “” is not really an empty string for the .net CLR and
the Nothing could throw a NullReferenceException if you reference the string later in the code. Below is the correct code for string declaration :

Dim str As String = “” ‘ wrong

Dim str2 As String = Nothing ‘ wrong

Dim str3 As String = vbNullString ‘ wrong

Dim str4 As String = String.Empty ‘ correct

String Concatenation

The usual way to concatenate strings is to use the + or the & operators. However the & is faster than the +
because it is specially designed for strings while the + also works for numeric addition.

Dim str As String = String.Empty

str = “Hello ” & ” world!” & vbCrLfFor i As Integer = 0 To 4

str &= str & vbCrLf

Next

Optimizing String Concatenation

A similar class to String is called StringBuilder located in System.Text.StringBuilder. The StringBuilder is specially
designed for extreme boosting of string concatenation. It is handled in a special way by the .net CLR. It’s easy to use and manipulate just like
the string class. If you know how much the string length will approximately be at the end of concatenation, you can set this capacity in the constructor
of the StringBuilder which gives additional performance boost:

Dim str As String = “Hello World” & vbCrLf

Dim sb As New System.Text.StringBuilder(str.Length * 4)

For i As Integer = 0 To 4

sb.Append(str)

Next

Dim final As String = sb.ToString ‘get the result string

StringBuilder also supports formatting strings using the AppendFormat method which is also highly optimized and easy to use:

Dim names As New StringCollection

names.Add(“Mike”)

names.Add(“Stacy”)

names.Add(“Bill”)

names.Add(“Krystelle”)Dim sb As New System.Text.StringBuilder

For Each name As String In names

sb.AppendFormat(“My name is {0}”, name)

Next

Dim final As String = sb.ToString

There is no need to instantiate multiple StringBuilder within the same procedure. You can reuse the same
StringBuilder object by setting the capacity property to 0 which deletes the current string:

Dim sb As New System.Text.StringBuilder

‘use sb…

sb.Capacity = 0

Exclude folders while building in web deployment projects

October 14, 2011 Leave a comment

Usually when you are building an application in visual studio using WDP you need it quickly to be done.
In this article i will show you how you can perform this in a very simple list of steps:

1 – Add your WDP (web deployment project).

2 – Right click on it and click on Open Project File.

3 – in the wdproj file you will see a tag called “<ItemGroup>”.

4 – Inside this tag add the following line:
<ExcludeFromBuild Include=”$(SourceWebPhysicalPath)\FolderName\**\*.*” />.

5 – Save and close your wdproj file and that’s it.

Note: you can add as many folders to exclude as you want.

Disposing Objects

October 14, 2011 Leave a comment

Although .net framework uses Garbage Collector to clean all the non referenced
objects from the memory, it sometimes need some help in doing that, so make
sure to use the method (Dispose()) on all objects(example: mycmd.dispose()) but only when you are 100% sure that
this object is no longer needed, because if you disposed an object that is still
functioning an Null Reference exception would occur.

BC30456: ‘CreateResourceBasedLiteralControl’ is not a member of…

October 14, 2011 Leave a comment

This Error occured frequently while deploying a project in visual studio 2005 or 2008.
The main reason of this error is the existance of duplicate dll names in the bin folder for the same project, alll what you have to do is to remove the unused dll file from the bin folder