Friday, October 5, 2007

Refactoring in visual studio

Why refactor? you know better why, because we have written crappy codes and its making our life hell, and killing lots of precious development hours. we could write thousands of line just to describe why we need refactoring, but lets not go in that debate.

We are here to learn refactoring features provided by visual studio to clean up our codes. The refactoring techniques are given bellow.

  • Rename
  • Extract Method
  • Encapsulate Field
  • Extract interface
  • Promote Local Variable to Parameter
  • Remove parameters
  • Reorder parameters

Bellow a screen shot from visual studio is given

image

All of the refactoring have shortcut associated with it and all of them are two key shortcut. Starts with CTRL+R, and followed by CTRL+”ANY Main Key”. for instance R is for Rename. Bellow example of rename refactor is given.

Rename Refactor

Step 1: Select the class, property, variable, field, or method. hit CTRL+R followed by CTRL+R, a window will popup and then put required name then click ok. if preview is been selected, it would popup another window.

image

Step 2: A preview will be shown what the ide will going to do with the name provided. Click apply to proceed.

image

This is just a tip of the iceberg. There are huge possibilities with this new features, how we use it and whether we choose to use it or not totally depends on us. And I highly recommend to use this refactor feature more and more.

Friday, September 7, 2007

Logical grouping of projects using solution folders.

Its unlikely that any of our professional life projects have only one project in our solution, and likely that in every of our project we would have huge number of projects in it. It might get hectic for us to keep track of the projects and find it in a glance, moreover it not healthy for the project also.

Visual studio have a nice feature called “Solution Folder” which is a logical folder structure and does not have any physical existence in user’s disk. Bellow I have given a screen shot of one of my personal project, where I have categorized three part of my solution using solutions explorer.

  • Architecture
  • Infrastructure
  • UI

image

In architecture I kept Business logic, data access layer and service layer. In infrastructure I kept helpers and business objects project and finally in UI solution folder I kept the web ui and window ui project. There could be more folder like this for instance “deployment” where all installer projects will go.

If solution folder had physical existence it would have been more helpful for us. But whatever we have serves our purpose.

Friday, August 3, 2007

How to get a free SVN repository?

Why we need SVN or any kind of source control, off course we  want to keep our source safe and want to maintain our source so that no one can see our private code. One easy step is to private SVN repository in your local network. In this particular scenario we have to install server all by yourself, all backup and labeling needs to be done all by our self as well.
www.assembla.com
It would be nicer if we have hosted SVN repository where we can keep our source as private, all backup and system recovery solutions will be provided by the hosting company. Luckily there are some services in the internet for that, for instance http://www.assembla.com/ is a wonderful site which allows commercial trails and if you have a open source project you can get a free SVN repository. You can also get limited space of around 250mb of free personal private SVN repository.
www.sourceforge.net
Yet another solution is to use Source forge or other free open source code hosting sites, note that we need to confirm what kind of open source SVN they support.
Unfortunate thing is that I kind find a good Microsoft based, source control on internet for free, that would be a nice addition to the open source work force.
Update 1:
Now we can host code in google code and also have svn support, but code needs to be open source.
Update 2:
Now we can also host code in codeplex which is sponsored by Microsoft. In this case code needs to be open source.
Update 3:
CodePlex now support team foundation server and teamexplorer of VS2008
Update 4:
https://bitbucket.org/ is now offering unlimited private project hosting... awesome

Thursday, July 5, 2007

How to Add Web Application Project in Visual Studio 2005.

The new version of visual studio does not content the web application project template, which was available in visual studio 2003. The way you can create web project is to create a new web site. I have no idea why its like that but the web application project was cooler than web site template.

Anyway may be Microsoft wanted to try new things. But there is always a work around for the problems, you can download the web application project template from  here. Installing this project template would give you web application project template for both c# and vb.net.

Update 1:

We found a very cool web site about Visual studio web application project template and lots of help with it. Thanks to Scott Gu. He have provided a awesome site with lots of tutorials.

image

Please visit Visual Studio 2005 Web Application Project for more information.

Update 2:

Never mind the old stuff web project is now available with the latest service pack. Life would be lot easier if they had provided it earlier.

Cheers.

Thursday, June 21, 2007

Asp.net File Upload Control Using with just one step

In our projects sometimes we need to use the file upload control. Asp.net 2.0 have
a build-in File-upload. In asp.net 1.x web controls didn’t had any builtin file-upload
control. Then we had to use a html input tag with type attribute set to “file” and
to use in server side add runat=”server” attribute.

<input id="myfileuploader" type="file" runat="server"/>

We all know how to work with a file upload control, today we will be discussion
about a new approach of file-upload. file-upload consists of two step . first we
have to select the file and then submit the page with a button or some kind of other
mechanism. To day we will try to minimize one step. If we want to upload the file
with just one step that is selecting the file here is the Technique

A file upload control which is eventually truned into a input html element have
three client side events

    * onBlur      
    * onChange
    * onFocus

we will utilize the onChange event… here is what we will do. frist add a asp.net
2.0 fileupload control in our page. then in pageload event add a attribute in attribute
collection. then write a javascript funtion to submit the form and in code do what
ever we whan….

 
task 1 : add a fileupload control

task 2: add the onchange attribute as FileUpload1.Attributes.Add("onchange", "doSomeStuff()");

task 3: add the javascript to submit the page

<script language="javascript"
type="text/javascript">

function doSomeStuff()
{

         document.forms[0].submit();

}

</script>

Thats it you are good to go.. in page load event check wheither the filename is
empty or not and then do the file upload.

Wednesday, May 2, 2007

Select Items of a html Select

As you all know in asp .net when we define a dropdown list it is converted to a html select.

<asp:DropDownList id="ddlNewsLetter" runat="server"></asp:DropDownList>

Now in html…

Its look like this

<select name="GcbsUC:ddlNewsLetter" id="GcbsUC_ddlNewsLetter">
<option value="1">July 11, 2006</option>
<option value="2">June 14, 2006</option>
<option value="3">May 14, 2006</option>
<option value="4">April 9, 2006</option>
<option value="5">March 16, 2006</option>
<option value="6">February 19, 2006</option>
</select>

In C# code i have populated the dropdown list with a counter and a data…

Well if we what to select a perticular dropdown item from the list… how we do it…

Option

1 . document.getElementById(‘ControlName’).selectedIndex = ‘ourvalue’; [here ourvalue must be an integer and between a range of the options.length.

2. document.getElementById(‘ControlName’).value = “ourknownvalue”;

[here ourknownvalue can be 6 that is <option value="6">February 19, 2006</option>]

3. well can can always loop through the items and do what ever we like with the property

    document.test.test2.options[0].value
    document.test.test2.options[2].text

Here is an example


var ddlNewsLetter = document.getElementById('ddlNewsLetter');
if(ddlNewsLetter!=null)
{
for(var i=0; i < ddlnewsletter.options.length; i++)
{
if(ddlnewsletter.options[i].text == strnewsletterdate)
{
ddlnewsletter.options[i].selected = true;
break;
}
}
}