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

Saturday, May 1, 2010

Fullscreen Internet Explorer

To run the Internet Explorer in fullscreen,
   iexplore.exe -k
Below is the C++ code in calling Internet Explorer to show 2 tab pages in fullscreen mode:

CComQIPtr sp = pActiveSite;
(pActiveSite is IOleClientSite *)
CComPtr m_pBrowser;
HRESULT hr = sp->QueryService(IID_IWebBrowserApp, IID_IWebBrowser2,
(void**)&m_pBrowser);
VARIANT_BOOL v = VARIANT_FALSE;
VARIANT_BOOL v1 = VARIANT_TRUE;

m_pBrowser->put_TheaterMode(v1);
m_pBrowser->put_TheaterMode(v);
Reference:
http://www.vistaheads.com/forums/microsoft-public-internetexplorer-general/289892-ie7-fullscreen-toggle-effect-programmatically-similar-like-f11.html