Monday, August 30, 2010

Changing constant value at runtime

In C#, you are allowed to set the value for a constant. This can be done by using the "readonly" keyword.

Normally, when we want to declare a constant that is not updateable through out the application by doing the following:

   public const int MAX_DAYS = 30;


In case, your application would like to allow the system administrator changing the MAX_DAYS value to accommodate their business environment, you need a way to do it. This can be done by replacing the "const" keyword with "readonly":

public class MyConstants
(
public readonly int MAX_DAYS;

public MyConstants()
{
MAX_DAYS = {read the value from database OR config file};
}

)

Monday, August 9, 2010

String Comparison Optimization

String myString = String.Intern("VERY LONG STRING #2");

if (Object.ReferenceEquals(myString, "VERY LONG STRING #1"))
{
...
}

else if (Object.ReferenceEquals(myString, "VERY LONG STRING #2"))
{
...
}

else if (Object.ReferenceEquals(myString, "VERY LONG STRING #3"))
{
...
}

...

else

{

...

}

Reference:
http://dotnetfacts.blogspot.com/2008/03/how-to-optimize-strings-comparison.html
http://msdn.microsoft.com/en-us/library/system.string.intern%28vs.71%29.aspx

Wednesday, August 4, 2010

Adding DLL reference automatically

It's quite sad when you are trying to add new reference by using the Add Reference provided in the Visual Studio because it is very slow. To speed up the process, you may have to add the following Macro and run it manually. It will add all your Dll references in one click:

    '4-8-10,lhw
'-add the commonly used dll to the selected project.
' No need to go through the Add Reference screen in VS.
Sub AddGeneralReferences()
Dim proj As EnvDTE.Project
Dim arr As Array = DTE.ActiveSolutionProjects

If Not arr Is Nothing Then
If arr.Length > 0 Then
proj = arr(0)

If Not proj Is Nothing Then
Dim v As VSLangProj.VSProject = proj.Object
Dim r As VSLangProj.References = v.References

If Not r Is Nothing Then
r.Add("System.configuration")
r.Add("System.Drawing")

'not sure why must include the
'full reference to system.web dll.
'Otherwise, COM error will occur.
r.Add("C:\WINDOWS\Microsoft.NET" & _
"\Framework\v2.0.50727\System.Web.dll")

r.Add("System.Web.Services")
End If
End If
End If
End If
End Sub

Thursday, July 29, 2010

Get the current row that fire the command


protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
GridViewRow row = (GridViewRow)((Control)e.CommandSource).NamingContainer;

... continue your codes here..
}

Saturday, July 17, 2010

Initialize properties in an object

For example, you have a class which looks like this:

class Customer
{
public string account_code { get; set; }
public string name { get; set; }
public string email { get; set; }
}
This is what we used to do to initialize the properties in a new object:

Customer c1 = new Customer();
c1.account_code = "a001";
c1.name = "ABC Ltd Co";
c1.email = "info@abc.testing.com";
Starts from C# 3.0, you may initialize the properties in a new object instance in a single line of code which looks like this:

Customer c2 = new Customer()
{ account_code = "a001",
name = "ABC Ltd Co",
email = "info@abc.testing.com" };

Sunday, June 20, 2010

Open Data Protocol

Sometimes you might wondering how to develop module by module and then integrate all modules into a single system. Basically, you need a protocol that is design for the integration purpose. This might be something that you are looking for.

Check this out:

http://www.odata.org/developers/protocols/overview

Tuesday, June 8, 2010

Macros for Visual Studio

Run Visual Studio.
  1. Select Tools \ Macros \ Macros IDE.
  2. In the Project Explorer (within the Macros IDE), double click MyMacros \ Module1.
  3. Paste the following codes into the editor.

After that, close the Macros IDE. Now, you may customize the shortcut key to execute the Macro.
  1. Select Tools \ Customize from VS menu.
  2. Click Keyboard button (next to the Close button).
  3. In the "show command containing" field, type "PrintDateTimeSingleLine". This will shortlist the commands that is accessible through VS.
  4. In the "Press shortcut keys" field, press CTRL+0 and click Assign button.
  5. Click OK button to save the customized keyboard settings.
  6. In the coding editor, press CTRL+0 and VS will execute the Macro. So, you don't have to repeatly typing the current date/time for the comments anymore.


Sub PrintDateTime()
If (Not IsNothing(DTE.ActiveDocument)) Then
Dim selection As TextSelection = DTE.ActiveDocument.Selection
selection.Insert(DateTime.Now.ToString("d.MMM.yyyy, ddd @ hh:mm"))
End If
End Sub


Sub PrintDateTimeSingleLine()
If (Not IsNothing(DTE.ActiveDocument)) Then
Dim selection As TextSelection = DTE.ActiveDocument.Selection
selection.Insert("//" & DateTime.Now.ToString("d.MMM.yyyy") & "-lhw-")
End If
End Sub

Sub PrintDateTimeProcHeader()
If (Not IsNothing(DTE.ActiveDocument)) Then
Dim selection As TextSelection = DTE.ActiveDocument.Selection
selection.Insert(DateTime.Now.ToString("d.MMM.yyyy") & "-lhw")
selection.NewLine()
selection.Insert("-")
End If
End Sub


Sub PrintSeparator()
If (Not IsNothing(DTE.ActiveDocument)) Then
Dim selection As TextSelection = DTE.ActiveDocument.Selection
selection.Insert("//-----------------------------------------------------------------------")
End If
End Sub

Sub PrintHeavySeparator()
If (Not IsNothing(DTE.ActiveDocument)) Then
Dim selection As TextSelection = DTE.ActiveDocument.Selection
selection.Insert("//=======================================================================")
End If
End Sub


Sub PrintShortSeparator()
If (Not IsNothing(DTE.ActiveDocument)) Then
Dim selection As TextSelection = DTE.ActiveDocument.Selection
selection.Insert("//-------------------------------------")
End If
End Sub

Sub PrintNotice()
If (Not IsNothing(DTE.ActiveDocument)) Then
Dim selection As TextSelection = DTE.ActiveDocument.Selection
selection.Insert("//<======")
End If
End Sub