Sep 16 2010

Use Properties to Encapsulate the Hidden Fields or ViewState in Asp.Net

Category: .Net | asp.net | C# | VS2008 ashish sheth @ 05:16

In asp.net you might be using lot of hidden variables and ViewState to maintain state between page postbacks. This can make your code look cluttered. You can use properties to encapsulate the hidden fields or ViewState.

If you are writing lot of code like this:

if(myHidden.Value == string.Empty)
{
	myHidden.Value = "someValue"; 	
}

someVariable = myHidden.Value

You can use properties to encapsulate the access to myHidden field. For example:

public string MyHiddenFieldValue
{
	get
	{
		if(myHidden.Value == string.Empty)
		{
			myHidden.Value = "someValue"; 	
		}
		return myHidden.Value
	}
	set
	{
		myHidden.Value = value;
	}
}

Then you can access the myHidden field just by the property

someVariable = MyHiddenFieldValue;

Similarly if you are storing some custom values in the ViewState of the page the you can create property for the ViewState also.

public string MyCustomViewState
{
	get
	{
		if(ViewState["MyViewState"] == null)
		{
			ViewState["MyViewState"] = "someValue"; 	
		}
		return ViewState["MyViewState"].ToString();
	}
	set
	{
		ViewState["MyViewState"] = value;
	}
}

Tags: , , ,

Aug 24 2010

VisualStudio ReflectedSchemas folder in ApplicationData

Category: .Net | asp.net | VS2008 ashish sheth @ 23:23

Just now I found out that C:\Documents and Settings\[Username]\Application Data\Microsoft\VisualStudio\9.0\ReflectedSchemas

folder takes lots of space. VS 2008 stores schema files which are dynamically generated when you compile a webcontrol.

To release the space on your drive you can delete the files safely and VisualStudio will re-generate these files whenever needed.

 

For more information see below:

http://blogs.msdn.com/b/mikhailarkhipov/archive/2004/05/14/131949.aspx

http://stackoverflow.com/questions/878345/reflectedschemas-folder-in-the-users-appdata-folder-visual-studio

http://forums.asp.net/t/1264942.aspx

http://www.ureader.com/msg/15364998.aspx

Tags: , ,

Apr 24 2010

Violating Single Resposibility Principle using VisualStudio Region

Single Responsibility Principle (SRP)says that "THERE SHOULD NEVER BE MORE THAN ONE REASON FOR A CLASS TO CHANGE.". Although this article mentions only about class, I think the SRP also applies to methods within the class: there should never be more than one reason for a method to change.
Visual Studio provides a good way to mark off section of file in the form of "region"so they can be collapsible and the code can be organized. Many people use region in a big method to organize the code. For example:

public void CreeateOrder(/*some parameters*/)
{
	#region Validate the parameters
	//code goes here
	#endregion

	#region create the order
	//insert the order data in the database
	#endregion

	#region create the order item
	//insert the item data in the database
	#endregion
}

Note that not all people use regions like this. Many people use comments instead of regions in this kind of methods.
As you can see this is a clear violation of the single responsibility principle. The method does more than one thing: it validates the order data, create a top level order and create order items. This can certainly be put into separate method.

private bool ValidateOrderData(/*some parameters*/)
{
	#region Validate the parameters
	//code goes here
	#endregion
}

private bool InsertOrder(/*order related parameter*/)
{
	#region create the order
	//insert the order data in the database
	#endregion
}

private bool InsertOrderItem(/*order item related parameter*/)
{
	#region create the order item
	//insert the item data in the database
	#endregion
}

public  void CreateOrder(/*Some parameter*/)
{
	If(ValidateOrder(/*parameter list*/))
	{
		if(InsertOrder(/*order parameter*/))
		{
			InsertOrderItem(/*order item parameter*/);
		}	
	}
}

As you can see, wherever you are using "region" in your method to demarcate the code, you can very well put that code in a separate method.

Tags: , , , ,

Feb 10 2010

Find-n-Replace using Regular Expression in Visual Studio 2008

Category: .Net | VS2008 ashish sheth @ 04:22

Here is a tip how you can convert a multiline text to single line(ie., how to remove newline characters) using regular expression.

Select "Use Regular Expression" in the find-n-replace dialog box. Enter "\n" in the "Find What:" field. Don't provide anything in the "Replace With:" and click the Replace or Replace All button. And voila.. All your contents are in a single lines now.

Here are more resources I found on the net on Find-n-Replace using regular expression:

http://www.aaronlerch.com/blog/2007/03/28/visual-studio-find-and-replace-regular-expressions/
http://msmvps.com/blogs/paulomorgado/pages/visual-studio-find-and-replace-regular-expression-patterns.aspx
http://www.codinghorror.com/blog/archives/000633.html
http://weblogs.asp.net/joelvarty/archive/2008/03/25/find-replace-in-visual-studio-with-regular-expressions-and-variables.aspx
http://developers.de/blogs/scott_munro/archive/2006/07/14/_2700_Find-and-Replace_2700_-with-regular-expressions-in-Visual-Studio.aspx
http://blogs.msdn.com/vseditor/archive/2004/06/18/159515.aspx
http://msdn.microsoft.com/en-us/library/2k3te2cs%28VS.80%29.aspx

Tags: ,

Dec 7 2009

Invoke Google from withing VS 2008

Category: .Net | .Net Framework | C# | CodeProject | VS2008 ashish sheth @ 11:25

As programmers we need to search for many tasks that we want to accomplish. And the F1 key in visual studio does not help because it always launches the local MSDN search, even if MSDN is not installed on the local computer.

Brian Schmitt has provided the code to write a macro to search variouse sites. Here I am describing steps to how you can configure your Visual studio 2008 to invoke google search from within the IDE based on his post.

  • In the VS 2008 IDE, click Alt + F8 to launch the Macro explorer.  It should show you a tree like below at the right hand side of the IDE.

 

 

  • Right Click on theMyMacros project and create a new module named Search. (Alternately, you can goto Tools menu-> Macros -> New Macro command). This will launch the macro editor.
  • Copy and paste the below code in the macro editor. (Code taken from Brian Schmitt's article on Better Visual Studio F1)

 

Imports EnvDTE
Imports System.Web

Public Module Search

#Region "Search Internet Sites"
  Public Const GOOGLE_FORMAT As String = "www.google.com/search?q={0}"
  Public Const STACKOVERFLOW_FORMAT As String = "http://www.stackoverflow.com/search?q={0}"
  Public Const SEARCHDOTNET_FORMAT As String = "http://searchdotnet.com/results.aspx?cx=002213837942349435108:jki1okx03jq&q={0}&sa=Search&cof=FORID:9#1144"
  Public Const MSDN_FORMAT As String = "http://social.msdn.microsoft.com/Search/en-US/?query={0}&ac=8"
   
  Public Sub SearchStackOverflowForSelectedText()
      SearchWebPage(STACKOVERFLOW_FORMAT)
  End Sub
  Public Sub SearchGoogleForSelectedText()
      SearchWebPage(GOOGLE_FORMAT)
  End Sub
  Public Sub SearchSearchDotNetForSelectedText()
      SearchWebPage(SEARCHDOTNET_FORMAT)
  End Sub
      Public Sub SearchMSDNForSelectedText()
      SearchWebPage(MSDN_FORMAT)
  End Sub
  Private Sub SearchWebPage(ByVal SearchURLFormat As String)
      Dim sel As EnvDTE.TextSelection = DTE.ActiveWindow.Selection
      Dim srchTxt As String = sel.Text.Trim
      If srchTxt.Length > 0 Then
          DTE.ItemOperations.Navigate(String.Format(SearchURLFormat, HttpUtility.UrlEncode(srchTxt)))
      End If
  End Sub
#End Region

End Module

 

  • Now the macro is ready (there are four macros created one each for google, stackoverflow, searchdotnet and MSDN). Now we will assign short cut for all the four macros.
  • Go to Tools -> Options -> keyboard. In the “Show command containing” type macro. Or simply scroll through the list box to find the macro we just created.

 

 

  • In the “Press shortcut key” type in the short cut you want (for e.g. Alt + F1, Alt + Shift + F1). Note that if you press just F1, it will replace the existing binding for the F1 key to the MSDN help.
  • In the similar way type in the short cut for other macros.

Tags: ,

Jul 25 2009

What is vshost.exe and vshost.exe.config?

Category: .Net | VS2008 ashish sheth @ 12:20

When we compile a windows application(forms or sevice) in VS 2005 or VS 2008 then we see that there is a *.vshost.exe and *.vshost.exe.config file generated in the bin folder. And we wonder what are these files and why are they generated. Here (http://blogs.msdn.com/dtemp/archive/2004/08/17/215764.aspx) is the anser. In short they are there to provide support for improved F5 performance, partial trust debugging, and design time expression evaluation. These files are there just for developent environment. It sould not be deployed to production environment.

Tags: ,