tag:blogger.com,1999:blog-54853747972230917242024-03-05T18:42:33.834-08:00Manisha Ratnapala's IT blogAnonymoushttp://www.blogger.com/profile/13786432889535633028noreply@blogger.comBlogger6125tag:blogger.com,1999:blog-5485374797223091724.post-75778715032251260732018-04-25T01:52:00.000-07:002018-05-17T23:15:18.680-07:00Installing Jenkins on Windows<br />
<br />
Jenkins is an open source continuous integration (CI) tool written in Java.Anonymoushttp://www.blogger.com/profile/13786432889535633028noreply@blogger.com0tag:blogger.com,1999:blog-5485374797223091724.post-13316801655761623842015-07-19T01:33:00.000-07:002018-05-17T22:30:15.732-07:00Auto sacalling<div class="titlepage" style="margin: 0px; padding: 0px;">
<div style="margin: 0px; padding: 0px;">
<div style="margin: 0px; padding: 0px;">
<h1 class="topictitle" style="border-bottom-style: none; border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px; font-family: verdana, arial, sans-serif; padding-top: 12px;">
</h1>
<h2 style="font-family: verdana, arial, sans-serif; margin-bottom: 1em; text-align: center;">
<span style="background-color: #f7f7f7; color: #333333; line-height: 28px;"><span style="font-size: x-large;">Auto Scaling</span></span></h2>
<div style="margin-bottom: 1em;">
<span style="font-family: "verdana" , sans-serif;"><span style="font-size: 12px; font-weight: normal;">AWS resources, such as EC2 instances, are housed in highly-available data centers. To provide additional scalability and reliability, these data centers are in different physical locations. </span><span class="emphasis" style="font-size: 12px; font-weight: normal;"><em>Regions</em></span><span style="font-size: 12px; font-weight: normal;"> </span><span style="font-size: 12px; font-weight: normal;">are large and widely dispersed geographic locations. Each region contains multiple distinct locations, called</span><span style="font-size: 12px; font-weight: normal;"> </span><span class="emphasis" style="font-size: 12px; font-weight: normal;"><em>Availability Zones</em></span><span style="font-size: 12px; font-weight: normal;">, that are engineered to be isolated from failures in other Availability Zones and provide inexpensive, low-latency network connectivity to other Availability Zones in the same region. </span></span><span style="font-size: 12px; font-weight: normal;"><span style="font-family: "verdana" , sans-serif;">Auto Scaling enables you to take advantage of the safety and reliability of geographic redundancy by spanning Auto Scaling groups across multiple Availability Zones within a region. When one Availability Zone becomes unhealthy or unavailable, Auto Scaling launches new instances in an unaffected Availability Zone. When the unhealthy Availability Zone returns to a healthy state, Auto Scaling automatically redistributes the application instances evenly across all of the designated Availability Zones.</span></span></div>
<div style="font-size: 12px; margin-bottom: 1em;">
<span style="font-weight: normal;"><span style="font-family: "verdana" , sans-serif;">You can add new instances to the application only when necessary, and termin</span><span style="font-family: "verdana" , "arial" , sans-serif;">ate them when they're no longer needed. And because Auto Scaling uses EC2 instances, </span></span><b style="font-family: verdana, arial, sans-serif;">you only have to pay for the instances you use, when you use them</b><span style="font-family: "verdana" , "arial" , sans-serif;">. You now have a cost-effective architecture that provides the best customer experience while minimizing expenses.</span></div>
<div style="font-family: verdana, arial, sans-serif; font-size: 12px; font-weight: normal; margin-bottom: 1em;">
An Auto Scaling group can contain EC2 instances in one or more Availability Zones within the same region. However, <span style="color: red;">Auto Scaling groups cannot span multiple regions</span>.</div>
</div>
</div>
</div>
<div class="itemizedlist" style="font-family: verdana, arial, sans-serif; font-size: 12px; margin: 0px; padding: 0px;">
<div class="titlepage" style="margin: 0px; padding: 0px;">
<div style="margin: 0px; padding: 0px;">
<div style="margin: 0px; padding: 0px;">
<h2 class="title" id="autoscaling-design-example" style="clear: both; color: #cc6600; font-size: 14px; padding-top: 1.5em;">
Web App Architecture</h2>
</div>
</div>
</div>
<div style="margin-bottom: 1em;">
In a common web app scenario, you run multiple copies of your app simultaneously to cover the volume of your customer traffic. These multiple copies of your application are hosted on identical EC2 instances (cloud servers), each handling customer requests.</div>
<div style="margin-bottom: 1em;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgc398BiziolpIYyG1wP8N9tOoGCjgy4fGt4O7EGp2_9FNQmQi94fVeslMAH8Zq8Rn5Wem2WPlQK5d9cRfwi9CkqVCDf6yTnw0rqJb2Im_ucRJTNnuVwxHULnIGf3OX4AjMpcYkIJpymZ0p/s1600/Untitled.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="233" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgc398BiziolpIYyG1wP8N9tOoGCjgy4fGt4O7EGp2_9FNQmQi94fVeslMAH8Zq8Rn5Wem2WPlQK5d9cRfwi9CkqVCDf6yTnw0rqJb2Im_ucRJTNnuVwxHULnIGf3OX4AjMpcYkIJpymZ0p/s400/Untitled.png" width="400" /></a></div>
<div style="margin-bottom: 1em;">
<br /></div>
<div style="margin-bottom: 1em;">
<br /></div>
<div style="margin-bottom: 1em;">
You can create as many Auto Scaling groups as you need. For example, you can create an Auto Scaling group for each tier.Auto Scaling manages the launch and termination of these EC2 instances on your behalf. You define a set of criteria (such as an Amazon CloudWatch alarm) that determines when the Auto Scaling group launches or terminates EC2 instances. Adding Auto Scaling groups to your network architecture can help you make your application more highly available and fault tolerant.</div>
<div style="margin-bottom: 1em;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
</div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi0cZG5EsQ9O5iS6yFB_C50SUqUgSM-wopoXkr23nYTbCkQ5VGFegcUfXk7cq7vupd-lQmlACi3XpUJmxmN1tywBbDVy3hpQnZa3fp1yuc2pmQRlJBOj4TBUFuvT5r_zMM9YEsHMuV5NFoD/s1600/Untitled.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="310" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi0cZG5EsQ9O5iS6yFB_C50SUqUgSM-wopoXkr23nYTbCkQ5VGFegcUfXk7cq7vupd-lQmlACi3XpUJmxmN1tywBbDVy3hpQnZa3fp1yuc2pmQRlJBOj4TBUFuvT5r_zMM9YEsHMuV5NFoD/s400/Untitled.png" width="400" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div style="margin-bottom: 1em;">
To distribute traffic between the instances in your Auto Scaling groups, you can introduce a load balancer into your architecture.</div>
<div>
<br /></div>
<div style="margin-bottom: 1em;">
<span style="color: #006699;">Instance Distribution</span></div>
<div style="margin-bottom: 1em;">
Auto Scaling attempts to distribute instances evenly between the Availability Zones that are enabled for your Auto Scaling group. Auto Scaling does this by attempting to launch new instances in the Availability Zone with the fewest instances. If the attempt fails, however, Auto Scaling attempts to launch the instances in another Availability Zone until it succeeds. For each instance that Auto Scaling launches in a VPC, it selects a subnet from the Availability Zone at random.</div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhcO3zxPjF1jS4p7TDbOn_ss7xHNsN4Yt8cBGWo58hfmU33FPkufqEbhOVt8o96g5GGOVP2ndh2xA5UDnE58diSxwksppIH5IxGnLjE-DT-uYlfkkHrq9q9xvsOB6wTUuoVdGN2sO9C2gRP/s1600/Untitled.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="312" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhcO3zxPjF1jS4p7TDbOn_ss7xHNsN4Yt8cBGWo58hfmU33FPkufqEbhOVt8o96g5GGOVP2ndh2xA5UDnE58diSxwksppIH5IxGnLjE-DT-uYlfkkHrq9q9xvsOB6wTUuoVdGN2sO9C2gRP/s400/Untitled.png" width="400" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div class="titlepage" style="font-size: medium; margin: 0px; padding: 0px;">
<div style="margin: 0px; padding: 0px;">
<div style="margin: 0px; padding: 0px;">
<h1 class="topictitle" style="border-bottom-style: none; border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px; font-size: 18px; padding-top: 12px;">
<u>Benefits of Auto Scaling</u></h1>
<div style="font-size: 12px;">
<u><br /></u></div>
</div>
</div>
</div>
<div style="margin-bottom: 1em;">
Adding Auto Scaling to your application architecture is one way to maximize the benefits of the AWS cloud. When you use Auto Scaling, your applications gain the following benefits:</div>
<div class="itemizedlist" style="margin: 0px; padding: 0px;">
<ul class="itemizedlist" type="disc">
<li class="listitem"><div style="margin-bottom: 1em;">
<b>Better fault tolerance</b>.- Auto Scaling can detect when an instance is unhealthy, terminate it, and launch an instance to replace it.</div>
</li>
<li class="listitem"><div style="margin-bottom: 1em;">
<b>Better availability</b>. You can configure Auto Scaling to use multiple Availability Zones. If one Availability Zone becomes unavailable, Auto Scaling can launch instances in another one to compensate.</div>
</li>
<li class="listitem"><div style="margin-bottom: 1em;">
<b>Better cost management</b>. Auto Scaling can dynamically increase and decrease capacity as needed. Because you pay for the EC2 instances you use, you save money by launching instances when they are actually needed and terminating them when they aren't needed.</div>
</li>
</ul>
</div>
</div>
Anonymoushttp://www.blogger.com/profile/13786432889535633028noreply@blogger.com0tag:blogger.com,1999:blog-5485374797223091724.post-29433723969258934602015-07-19T01:32:00.003-07:002018-05-18T00:09:43.297-07:00<h1 class="topictitle" style="border-bottom-style: none; border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px; font-family: verdana, arial, sans-serif; font-size: 18px; padding-top: 12px;">
Using DYNAMO DB LOCAL to Node project</h1>
<div>
<br /></div>
<h3>
To install DynamoDB locally on Windows 7</h3>
<br />
DynamoDB Local supports the Java Runtime Engine (JRE) version 6.x or newer;<br />
it will not run on older JRE versions.You can download using following link.<br />
<br />
<a href="http://www.oracle.com/technetwork/java/javase/downloads/index-jsp-138363.html">http://www.oracle.com/technetwork/java/javase/downloads/index-jsp-138363.html</a><br />
<br />
Go to following link to download dynamo db jar file:<br />
<a href="http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Tools.DynamoDBLocal.html">http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Tools.DynamoDBLocal.html</a><br />
<br />
<br />
Once you have downloaded the archive to your computer, extract the contents and<br />
copy the extracted directory to C:\ drive.<br />
<br />
<br />
Open a command prompt window, navigate to the downloaded directory where you will find DynamoDBLocal.jar, and enter the following command:<br />
<br />
<h4>
java -Djava.library.path=./DynamoDBLocal_lib -jar DynamoDBLocal.jar -sharedDb</h4>
<div>
or</div>
<h4>
java -Djava.library.path=. -jar DynamoDBLocal.jar</h4>
<h4>
<br /><span style="background-color: white;"><span style="color: red;">
Now we’re ready to use our local DynamoDB </span></span></h4>
2013-09-29 12:11:38.530:INFO:oejs.Server:jetty-8.y.z-SNAPSHOT<br/><br />
2013-09-29 12:11:38.618:INFO:oejs.AbstractConnector:Started <br/><br />
SelectChannelConnector@0.0.0.0:8000<br />
<br />
<h4 style="border-bottom-style: none; border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px; font-family: verdana, arial, sans-serif; font-size: 18px; padding-top: 12px;">
JavaScript Shell for DynamoDB Local</h4>
<span style="font-family: Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal;">The JavaScript Shell for DynamoDB Local can help jump-start your usage of DynamoDB, all within an interactive, hands-on environment. The JavaScript Shell is bundled with DynamoDB Local, and provides an easy-to-use environment for prototyping and application development.</span><br />
<h3>
<ul class="itemizedlist" style="font-size: 12px; font-weight: normal;" type="disc">
<li><span style="font-family: Arial, Helvetica, sans-serif;">Its supports latest version of DynamoDB Local, to run it on your computer while you up and running the Dynamo DB Local server open a web browser on your computer and go to the following UR</span><span style="font-family: verdana, arial, sans-serif;">L:</span><a href="http://localhost:8000/shell" style="font-family: 'Courier New', Courier, mono;">http://localhost:8000/shell</a></li>
</ul>
<div>
<span style="font-family: verdana, arial, sans-serif;"><span style="font-size: 12px; font-weight: normal;"><br /></span></span></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhY1Txh3GQXencik7G1GHIv-j3_pndinTh5ky37oDOMuZNM5UuofuUINvoEq14ug3uTJURYdnAnD5-mE5ChukEcsnftS3kFw2S98HnbZNnnPLT5PfSCmZXJ5E2DV4ZYkfdw6UlSEpHGs7VT/s1600/Untitled.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="224" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhY1Txh3GQXencik7G1GHIv-j3_pndinTh5ky37oDOMuZNM5UuofuUINvoEq14ug3uTJURYdnAnD5-mE5ChukEcsnftS3kFw2S98HnbZNnnPLT5PfSCmZXJ5E2DV4ZYkfdw6UlSEpHGs7VT/s400/Untitled.png" width="400" /></a></div>
<div>
<span style="font-family: verdana, arial, sans-serif;"><span style="font-size: 12px; font-weight: normal;"><br /></span></span></div>
<div>
<span style="font-family: verdana, arial, sans-serif;"><span style="font-size: 12px; font-weight: normal;"><br /></span></span></div>
<div>
<span style="font-family: verdana, arial, sans-serif;"><span style="font-size: 12px; font-weight: normal;"><br /></span></span></div>
<div>
<span style="font-family: verdana, arial, sans-serif;"><span style="font-size: 12px; font-weight: normal;"><br /></span></span></div>
<div>
<span style="font-family: verdana, arial, sans-serif;"><span style="font-size: 12px; font-weight: normal;"><br /></span></span></div>
</h3>
<h3>
Install asw-sdk for node</h3>
<div>
The preferred way to install the AWS SDK for Node.js is to use the npm package manager for Node.js. Simply open the cmd window inside project folder and type the following </div>
<div>
<div>
<br /></div>
<div>
npm install aws-sdk</div>
</div>
<div>
<br /></div>
<div>
<div>
You can also use Bower to install the SDK by typing the following into a terminal window</div>
<div>
<br /></div>
<div>
bower install aws-sdk-js</div>
</div>
<div>
<br /></div>
<div>
<br /></div>
<div>
Create a js file and define configuration</div>
<div>
<div>
<br /></div>
<div>
var AWS = require('aws-sdk');</div>
<div>
<br /></div>
<div>
For work with Local DynamoDB define endpoint will be "http://localhost:8000"</div>
<div>
<br /></div>
<div>
var databaseConfig = {"endpoint": new AWS.Endpoint("http://localhost:8000")};</div>
<div>
var dynamoDB = new AWS.DynamoDB(databaseConfig);</div>
<div>
<br /></div>
<div>
To Connect with Local DynamoDB, we need provide dummy entries for required fields(accessKey, Secret and Region).</div>
<div>
<br /></div>
<div>
<div>
var dynamoDBConfiguration = {</div>
<div>
"accessKeyId": "xxxxxxxxxxxxx",</div>
<div>
"secretAccessKey": "xxxxxxxxxxxxxxxxx",</div>
<div>
"region": "eu-west-1"</div>
<div>
};</div>
</div>
<div>
<br /></div>
<div>
Later on to Connect with AWS DynamoDB Service place actual accessKey, Secret and Region.</div>
<div>
<br /></div>
<div>
provide your configurations.</div>
<div>
<br /></div>
<div>
AWS.config.update(dynamoDBConfiguration);</div>
<div>
<br /></div>
<div>
Now you can start use it locally.</div>
<div>
<br /></div>
<div>
<br /></div>
<div>
<br /></div>
<div>
<br /></div>
<div>
<br /></div>
<div>
<br /></div>
<div>
<br /></div>
<div>
<br /></div>
<div>
<br /></div>
<div>
<br /></div>
</div>
Anonymoushttp://www.blogger.com/profile/13786432889535633028noreply@blogger.com0tag:blogger.com,1999:blog-5485374797223091724.post-20667989229493003952013-09-27T02:39:00.000-07:002013-09-27T02:39:44.824-07:00Creating a Google map by fetching data from the MS SQL<br />
<br />
<br />
<span style="background-color: white; color: #111111; font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px;"> Using one of Google JavaScript API function. There are many articles on the Google Maps JavaScript API works. However, there was no article I found which pulls series of co-ordinates from a database or datatable and plots a continuous path on the run. I have explained in steps how to do the same as below:</span><br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiJHph7R1ZH7pGCstlr2uubd9NfRH7tU8obsL9azlM55lTP4XW8aKYaq1YpGflW3FWrVB3CdiLCCzwV5yIZ_7WWQROU62s6pUETvfdZgi3YFuB4tRWgcuhF0g1FcPIbyz_SrgoCNWo0dH8Q/s1600/Untitled.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiJHph7R1ZH7pGCstlr2uubd9NfRH7tU8obsL9azlM55lTP4XW8aKYaq1YpGflW3FWrVB3CdiLCCzwV5yIZ_7WWQROU62s6pUETvfdZgi3YFuB4tRWgcuhF0g1FcPIbyz_SrgoCNWo0dH8Q/s1600/Untitled.png" height="144" width="320" /></a></div>
<br />
using System;<br />
using System.Collections.Generic;<br />
using System.Linq;<br />
using System.Web;<br />
using System.Data;<br />
<br />
/// <summary><br />
/// Summary description for GPSLib<br />
/// </summary><br />
public static class GPSLib<br />
{<br />
public static String PlotGPSPoints(DataTable tblPoints)<br />
{<br />
try<br />
{<br />
<br />
String Locations = "";<br />
String sJScript = "";<br />
int i = 0;<br />
foreach (DataRow r in tblPoints.Rows)<br />
{<br />
// bypass empty rows<br />
if (r["Latitude"].ToString().Trim().Length == 0)<br />
continue;<br />
<br />
string Latitude = r["Latitude"].ToString();<br />
string Longitude = r["Logitude"].ToString();<br />
<br />
<br />
// create a line of JavaScript for marker on map for this record<br />
Locations += Environment.NewLine + @" path.push(new google.maps.LatLng(" + Latitude + ", " + Longitude + @"));<br />
<br />
var marker" + i.ToString() + @" = new google.maps.Marker({<br />
position: new google.maps.LatLng(" + Latitude + ", " + Longitude + @"),<br />
title: '#' + path.getLength(),<br />
map: map<br />
});<br />
<br />
infowindow.open(map,marker" + i.ToString() + @");<br />
google.maps.event.addListener(marker" + i.ToString() + @", 'click', function() {<br />
infowindow.open(map,marker" + i.ToString() + @");<br />
}); ";<br />
<br />
i++;<br />
<br />
<br />
}<br />
<br />
<br />
// construct the final script<br />
sJScript = @"<script type='text/javascript'><br />
var poly;<br />
var map;<br />
<br />
function initialize() {<br />
var cmloc = new google.maps.LatLng(18.964700,72.825800);<br />
<br />
var myOptions = {<br />
zoom: 9,<br />
center: cmloc,<br />
mapTypeId: google.maps.MapTypeId.HYBRID<br />
};<br />
<br />
<br />
var infoWindow = new google.maps.InfoWindow();<br />
map = new google.maps.Map(document.getElementById('map_canvas'), myOptions);<br />
<br />
var infowindow = new google.maps.InfoWindow({<br />
content: ('Latitude:' +document.getElementById('Latitude')+'<br>Logitude'+document.getElementById('Longitude'))<br />
});<br />
<br />
var polyOptions = {<br />
strokeColor: 'blue',<br />
strokeOpacity: 0.5,<br />
strokeWeight: 3,<br />
fillcolor:'blue',<br />
fillOpacity:0.4 <br />
<br />
}<br />
poly = new google.maps.Polygon(polyOptions);<br />
poly.setMap(map);<br />
<br />
<br />
var path = poly.getPath();<br />
" + Locations + @"<br />
}<br />
</script>";<br />
return sJScript;<br />
}<br />
<br />
<br />
catch (Exception ex)<br />
{<br />
throw ex;<br />
}<br />
}<br />
}Anonymoushttp://www.blogger.com/profile/13786432889535633028noreply@blogger.com0tag:blogger.com,1999:blog-5485374797223091724.post-26405665952548408502013-08-06T12:23:00.000-07:002015-08-11T09:21:13.573-07:00<div class="MsoNormal">
<b><span style="font-family: Times New Roman, serif;"><span style="font-size: 28pt; line-height: 115%;">Create
a </span><span style="font-size: 37px; line-height: 42px;">predefined</span><span style="font-size: 28pt; line-height: 115%;"> format dynamically for a
Excel sheet & display the data by
fetching data from the MSSQL database. <o:p></o:p></span></span></b></div>
<div>
<br /></div>
<div>
<br /></div>
<div>
<div class="MsoNormal">
This coding is use to generate a Environmental Impact
Assessment Report for
Leopold Matrix.</div>
<div class="MsoNormal">
Here this report has
predefine format and data are varies according to the available details for
particular project.Assesment is generate using MSExel according to the client
request.</div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b>Leopold Environmental
Assement</b> format:</div>
<div class="MsoNormal">
<br />
Sum of the main components total weighted averages should
equals to 1.</div>
<div class="MsoNormal">
<div class="separator" style="clear: both; text-align: center;">
</div>
<br /></div>
<div class="MsoNormal">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhQKxNxdWE07GQkuoLL4JakKvysY6yNke9uLalyVfOUYgryFDIRbTLsuRd39BhxAsnFIe054kCw8qGD8CpUY49WzyocZk-7O9ph6mbgDKZjt5iitBnxzAvC0FyQtW1RqhbrTjBOu2vj3Le6/s1600/Untitled.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" height="381" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhQKxNxdWE07GQkuoLL4JakKvysY6yNke9uLalyVfOUYgryFDIRbTLsuRd39BhxAsnFIe054kCw8qGD8CpUY49WzyocZk-7O9ph6mbgDKZjt5iitBnxzAvC0FyQtW1RqhbrTjBOu2vj3Le6/s640/Untitled.png" width="640" /></a></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoListParagraph" style="mso-list: l1 level1 lfo4; text-indent: -.25in;">
</div>
<ul>
<li><span style="color: #c00000; font-family: Wingdings; font-size: 14pt; line-height: 115%; text-indent: -0.25in;"> <span style="font-family: 'Times New Roman'; font-size: 7pt; line-height: normal;"> </span></span><span style="color: #c00000; font-size: 14pt; line-height: 115%; text-indent: -0.25in;">First you have to
import Excel Library to as project reference.</span></li>
</ul>
<br />
<div class="MsoNormal">
Ceate 3 objects called,</div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> Excel.<span style="color: #2b91af;">Application</span> oXL;
//Create an object for micrsoft Excel application.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
Excel.<span style="color: #2b91af;">_Workbook</span> oWB; //Create an object for set of Excel
Sheets.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
Excel.<span style="color: #2b91af;">_Worksheet</span> oSheet; //Create an
object for 1 Excel sheet.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
oXL = <span style="color: blue;">new</span> Excel.<span style="color: #2b91af;">Application</span>();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
oXL.Visible = <span style="color: blue;">true</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: green;">//Get a new workbook.</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
oWB = (Excel.<span style="color: #2b91af;">_Workbook</span>)(oXL.Workbooks.Add());<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: green; font-family: Consolas; font-size: 9.5pt;"> //Get a new worksheet.</span><span style="font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
oSheet = (Excel.<span style="color: #2b91af;">_Worksheet</span>)oWB.ActiveSheet;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: green; font-family: Consolas; font-size: 9.5pt;">// ---- Header Row ---</span><span style="font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">You can customize the excel sheet by creating
borders, <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoListParagraph" style="margin: 0in 0in 0.0001pt 22.5pt; text-indent: 0in;">
</div>
<ul>
<li><span style="color: #c00000; font-family: Consolas; font-size: 14pt; text-indent: 0in;"> </span><span style="color: #c00000; font-family: Consolas; font-size: 14pt; text-indent: 0in;">To customize cell value
in excel sheet use</span></li>
</ul>
<br />
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: #548dd4; font-family: Consolas; font-size: 9.5pt; mso-themecolor: text2; mso-themetint: 153;"> <span style="background: yellow; mso-highlight: yellow;">Cells[Row index ,Column index]</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">Ex-:<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> oSheet.<span style="color: #548dd4; mso-themecolor: text2; mso-themetint: 153;">Cells[3, 1]</span> = <span style="color: #a31515;">"Category"</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
</div>
<ul>
<li><span style="color: #c00000; font-family: Consolas; font-size: 14pt;">To custmize a cell, directly pass
cell name to</span></li>
</ul>
<br />
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: yellow; color: #548dd4; font-family: Consolas; font-size: 9.5pt; mso-highlight: yellow; mso-themecolor: text2; mso-themetint: 153;">
range(cell name)</span><span style="font-family: Consolas; font-size: 9.5pt;">
function<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">Ex-:<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
oSheet.<span style="color: #548dd4; mso-themecolor: text2; mso-themetint: 153;">get_Range("A3")</span>.ColumnWidth = 15;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
oSheet.<span style="color: #548dd4; mso-themecolor: text2; mso-themetint: 153;">get_Range("A3")</span>.Borders.Weight = 1;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
oSheet.<span style="color: #548dd4; mso-themecolor: text2; mso-themetint: 153;">get_Range("A3")</span>.Borders.LineStyle = 6;<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoListParagraph" style="mso-list: l0 level1 lfo1; text-indent: -.25in;">
</div>
<ul>
<li><span style="color: #c00000; font-family: Consolas; font-size: 14pt; line-height: 115%; text-indent: -0.25in;"> You
can merge set of cells,</span></li>
</ul>
<br />
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">To custmize a cell directly pass range of cell
indexes to <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="background: yellow; color: #548dd4; mso-highlight: yellow; mso-themecolor: text2; mso-themetint: 153;">get_Range(From cell index,To cell index)</span> function<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">EX-:<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 63.0pt;">
<span style="font-family: Consolas; font-size: 9.5pt; line-height: 115%;">oSheet.<span style="color: #548dd4; mso-themecolor: text2; mso-themetint: 153;">get_Range(Start cell name, End cell name)</span>.Merge(); oSheet.<span style="color: #548dd4; mso-themecolor: text2; mso-themetint: 153;">get_Range("A3",
"D3")</span>.Font.Bold = <span style="color: blue;">true</span>; oSheet.<span style="color: #00b0f0;">get_Range("A3",
"D3").</span>VerticalAlignment = <span style="color: #2b91af;">VerticalAlignment</span>.Center;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoListParagraph" style="margin-bottom: 0.0001pt; text-indent: -0.25in;">
</div>
<ul>
<li><span style="color: #c00000; font-family: Consolas; font-size: 14pt; text-indent: -0.25in;"> Get the values to
the list by calling the stored procedure</span><span style="font-family: Consolas; font-size: 9.5pt; text-indent: -0.25in;"> ”</span><span style="color: #00b050; font-family: Consolas; font-size: 9.5pt; text-indent: -0.25in;">Get Leopold Activities</span><span style="font-family: Consolas; font-size: 9.5pt; text-indent: -0.25in;">”</span></li>
</ul>
<br />
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> Stored Procedure ”Get Leopold Activities”<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: #00b050; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">ALTER proc
[dbo].[Get_Leopold_Activities]<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: #00b050; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">(<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: #00b050; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> @proj_reference as varchar (10)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: #00b050; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: #00b050; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">AS<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: #00b050; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">BEGIN<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: #00b050; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">SELECT
DISTINCT<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: #00b050; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">
A.actv_reference<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: #00b050; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> , A.actv_name<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: #00b050; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> , A.actv_phase , A.actv_id <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: #00b050; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">FROM LeopoldMatrixValues AS L <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: #00b050; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">INNER JOIN
Projects AS P ON L.proj_reference = P.proj_reference <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: #00b050; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">INNER JOIN
Impacts AS I ON L.impt_reference = I.impt_reference <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: #00b050; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">INNER JOIN
Components AS C ON I.impt_component = C.cmpt_reference <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: #00b050; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">INNER JOIN
Activities AS A ON L.actv_reference = A.actv_reference<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: #00b050; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">WHERE </span><span style="color: #c00000; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">P.proj_reference = @proj_reference<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: #00b050; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">ORDER BY
A.actv_phase , A.actv_id , A.actv_reference,A.actv_name <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: #00b050; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">END<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoListParagraph" style="margin: 0in 0in 0.0001pt 41.25pt; text-indent: -0.25in;">
</div>
<ul>
<li><span style="color: #c00000; font-family: Wingdings; font-size: 14pt; text-indent: -0.25in;"><span style="font-family: 'Times New Roman'; font-size: 7pt;"> </span></span><span style="color: #c00000; font-family: Consolas; font-size: 14pt; text-indent: -0.25in;">Store the values to the list by passing project reference to the
stored procedure.</span></li>
</ul>
<br />
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: #2b91af; font-family: Consolas; font-size: 9.5pt;">List</span><span style="font-family: Consolas; font-size: 9.5pt;"><<span style="color: #2b91af;">Get_Leopold_Activities_Result</span>>
lstActivities = ObjProjctsFunc.<span style="color: #00b050;">GetLeopoldActivities</span>(<span style="color: red;">labelProjrefrnceGenrated.Content.ToString()</span>);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoListParagraph" style="margin: 0in 0in 0.0001pt 41.25pt; text-indent: -0.25in;">
</div>
<ul>
<li><span style="color: #c00000; font-family: Wingdings; font-size: 14pt; text-indent: -0.25in;"><span style="font-family: 'Times New Roman'; font-size: 7pt;"> </span></span><span style="text-indent: -0.25in;"><span style="color: #c00000; font-family: Consolas;"><span style="font-size: 19px;">Retrieve</span></span><span style="color: #c00000; font-family: Consolas; font-size: medium;"> the data inside the list to the excel Sheet.</span></span></li>
</ul>
<br />
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: green;">// --- MATRIX DATA ----<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">string</span><span style="font-family: Consolas; font-size: 9.5pt;"> startColName, endColName;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">int</span><span style="font-family: Consolas; font-size: 9.5pt;"> startColNo, endColNo;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">foreach</span> (<span style="color: #2b91af;">Get_Leopold_Activities_Result</span>
oRstAct <span style="color: blue;">in</span> lstActivities)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
endColNo = startColNo + 1;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
endColName = GetColName(endColNo) + rowNo;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoListParagraph" style="margin-bottom: 0.0001pt; text-indent: -0.25in;">
</div>
<ul>
<li><span style="color: #c00000; font-family: Consolas; font-size: 14pt; text-indent: -0.25in;"> Numeric values get
from the MS SQL assign to the excel sheet.</span></li>
</ul>
<br />
<div class="MsoListParagraph" style="margin-bottom: 0.0001pt; text-indent: -0.25in;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">var</span><span style="font-family: Consolas; font-size: 9.5pt;"> filter = (<span style="color: blue;">from</span> i <span style="color: blue;">in</span> lstMatrice</span></div>
<div class="MsoNormal" style="margin: 0in 0in 0.0001pt 1in;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">where</span> i.actv_reference == oRstAct.actv_reference
&& i.impt_reference == oRstImpacts.impt_reference<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
<span style="color: blue;">select</span> i).ToList();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">if</span> (filter.Count() > 0)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> oSheet.Cells[rowNo, z]
= filter[0].mtrx_magnitude;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
oSheet.Cells[rowNo + 1, z + 1]
= filter[0].mtrx_importance;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> Total =
Total + (<span style="color: blue;">int</span>)(filter[0].mtrx_magnitude *
filter[0].mtrx_importance);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
oSheet.get_Range(startColName, endColName).ColumnWidth = 2.5;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
DrawCellBorders(rowNo, z + 1, rowNo, z + 1, <span style="color: blue;">true</span>,
<span style="color: blue;">false</span>, <span style="color: blue;">false</span>, <span style="color: blue;">true</span>, <span style="color: blue;">true</span>);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
DrawCellBorders(rowNo + 1, z, rowNo + 1, z, <span style="color: blue;">false</span>,
<span style="color: blue;">true</span>, <span style="color: blue;">true</span>, <span style="color: blue;">false</span>, <span style="color: blue;">true</span>);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
DrawCellBorders(rowNo + 1, z + 1, rowNo + 1, z + 1, <span style="color: blue;">false</span>, <span style="color: blue;">true</span>, <span style="color: blue;">false</span>, <span style="color: blue;">true</span>, <span style="color: blue;">false</span>);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
startColNo = endColNo + 1;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
startColName = GetColName(startColNo) + rowNo;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
z = z + 2;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoListParagraph" style="margin: 0in 0in 0.0001pt 41.25pt; text-indent: -0.25in;">
</div>
<ul>
<li><span style="color: #c00000; font-family: Consolas; font-size: 14pt; text-indent: -0.25in;"> Calculate and assign the valus to the Excel cell</span></li>
</ul>
<br />
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
oSheet.Cells[rowNo, z] = Total;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
endColName = GetColName(startColNo) + (rowNo + 1);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
oSheet.get_Range(startColName, endColName).Merge();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
GrandTotal = GrandTotal + Total;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
rowNo = rowNo + 2;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
LineNO = LineNO + 1;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
oSheet.Cells[4, z] = <span style="color: #a31515;">"Total"</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
oSheet.Cells[rowNo, z] = GrandTotal;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoListParagraph" style="margin: 0in 0in 0.0001pt 41.25pt; text-indent: -0.25in;">
</div>
<ul>
<li><span style="color: #c00000; font-family: Wingdings; font-size: 14pt; text-indent: -0.25in;"><span style="font-family: 'Times New Roman'; font-size: 7pt;"> </span></span><span style="color: #c00000; font-family: Consolas; font-size: 14pt; text-indent: -0.25in;">Calculate the weighted average</span></li>
</ul>
<br />
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">decimal</span> WAGrandTotal = 0;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">decimal</span> CatWiseWATotal = 0;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">int</span> catStrLNo = 5;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> oSheet.Cells[4, z + 1] = <span style="color: #a31515;">"Weighted Average"</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">for</span> (<span style="color: blue;">int</span> Lno = 5; Lno
<= rowNo - 1; Lno++)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
<span style="color: blue;">var</span> cell = (Excel.<span style="color: #2b91af;">Range</span>)oSheet.Cells[Lno, z];<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
<span style="color: blue;">if</span> (<span style="color: #2b91af;">Convert</span>.ToString(cell.Value2)
!= <span style="color: blue;">null</span>)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
<span style="color: blue;">if</span> ((<span style="color: blue;">int</span>)cell.Value2
!= 0)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
oSheet.Cells[Lno, z + 1] = (<span style="color: blue;">int</span>)cell.Value2/(<span style="color: blue;">decimal</span>)GrandTotal
;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
WAGrandTotal = WAGrandTotal + (<span style="color: blue;">int</span>)cell.Value2/(<span style="color: blue;">decimal</span>)GrandTotal
;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
<span style="color: blue;">else</span> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> oSheet.Cells[Lno, z +
1] = 0;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> WAGrandTotal =
WAGrandTotal + 0;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoListParagraph" style="margin: 0in 0in 0.0001pt 41.25pt; text-indent: -0.25in;">
</div>
<ul>
<li><span style="color: #c00000; font-family: Wingdings; font-size: 14pt; text-indent: -0.25in;"><span style="font-family: 'Times New Roman'; font-size: 7pt;"> </span></span><span style="color: #c00000; font-family: Consolas; font-size: 14pt; text-indent: -0.25in;">Calculate the total weighted average</span></li>
</ul>
<br />
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
<span style="color: blue;">if</span> ((<span style="color: blue;">int</span>)cell.Value2
!= 0)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
CatWiseWATotal = CatWiseWATotal + ( (<span style="color: blue;">int</span>)cell.Value2/(<span style="color: blue;">decimal</span>)GrandTotal );<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
<span style="color: blue;">else</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> CatWiseWATotal =
CatWiseWATotal + 0;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
oSheet.Cells[catStrLNo, z + 2] = CatWiseWATotal;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
<o:p></o:p></span></div>
<div class="MsoNormal" style="margin: 0in 0in 0.0001pt 0.5in;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">if</span><span style="font-family: Consolas; font-size: 9.5pt;"> (((<span style="color: #2b91af;">Convert</span>.ToString(((Excel.<span style="color: #2b91af;">Range</span>)oSheet.Cells[Lno
+ 2, 1]).Value2)) != <span style="color: blue;">null</span>) && (Lno !=
5))<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> catStrLNo = Lno + 2;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> CatWiseWATotal = 0;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
oSheet.Cells[catStrLNo, z + 2] = CatWiseWATotal;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
oSheet.Cells[rowNo, z + 1] = WAGrandTotal;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
oXL.Visible = <span style="color: blue;">true</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
oXL.UserControl = <span style="color: blue;">true</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoListParagraph" style="margin: 0in 0in 0.0001pt 41.25pt; text-indent: -0.25in;">
</div>
<ul>
<li><span style="color: #c00000; font-family: Wingdings; font-size: 14pt; text-indent: -0.25in;"><span style="font-family: 'Times New Roman'; font-size: 7pt;"> </span></span><span style="color: #c00000; font-family: Consolas; font-size: 14pt; text-indent: -0.25in;">Draw cell borders.</span></li>
</ul>
<br />
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">private</span><span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">void</span>
DrawCellBorders(<span style="color: blue;">int</span> frmrowNo, <span style="color: blue;">int</span> frmColNo, <span style="color: blue;">int</span>
TorowNo, <span style="color: blue;">int</span> ToColNo, <span style="color: blue;">bool</span>
top, <span style="color: blue;">bool</span> bottom, <span style="color: blue;">bool</span>
left, <span style="color: blue;">bool</span> right, <span style="color: blue;">bool</span>
diagup)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
Excel.<span style="color: #2b91af;">Borders</span> b = <span style="color: blue;">null</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
Excel.<span style="color: #2b91af;">Borders</span> fb = <span style="color: blue;">null</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> b
= (Excel.<span style="color: #2b91af;">Borders</span>)(oSheet.Cells[frmrowNo,
frmColNo] <span style="color: blue;">as</span> Excel.<span style="color: #2b91af;">Range</span>).Borders;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">
fb = (oSheet.Cells[frmrowNo, frmColNo] <span style="color: blue;">as</span>
Excel.<span style="color: #2b91af;">Range</span>).Borders;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">if</span> (diagup == <span style="color: blue;">true</span>)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> fb[Microsoft.Office.Interop.Excel.<span style="color: #2b91af;">XlBordersIndex</span>.xlDiagonalUp].Weight =
b[Microsoft.Office.Interop.Excel.<span style="color: #2b91af;">XlBordersIndex</span>.xlDiagonalUp].Weight;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">if</span> (top == <span style="color: blue;">true</span>)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">fb[Microsoft.Office.Interop.Excel.<span style="color: #2b91af;">XlBordersIndex</span>.xlEdgeTop].Weight =
b[Microsoft.Office.Interop.Excel.<span style="color: #2b91af;">XlBordersIndex</span>.xlEdgeTop].Weight;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">if</span> (bottom == <span style="color: blue;">true</span>)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">fb[Microsoft.Office.Interop.Excel.<span style="color: #2b91af;">XlBordersIndex</span>.xlEdgeBottom].Weight =
b[Microsoft.Office.Interop.Excel.<span style="color: #2b91af;">XlBordersIndex</span>.xlEdgeBottom].Weight;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <span style="color: blue;">if</span> (left == <span style="color: blue;">true</span>)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">fb[Microsoft.Office.Interop.Excel.<span style="color: #2b91af;">XlBordersIndex</span>.xlEdgeLeft].Weight =
b[Microsoft.Office.Interop.Excel.<span style="color: #2b91af;">XlBordersIndex</span>.xlEdgeLeft].Weight;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> i<span style="color: blue;">f</span> (right == <span style="color: blue;">true</span>)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;">fb[Microsoft.Office.Interop.Excel.<span style="color: #2b91af;">XlBordersIndex</span>.xlEdgeRight].Weight =
b[Microsoft.Office.Interop.Excel.<span style="color: #2b91af;">XlBordersIndex</span>.xlEdgeRight].Weight;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal">
</div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
</div>
Anonymoushttp://www.blogger.com/profile/13786432889535633028noreply@blogger.com0Kandana, Sri Lanka7.05 79.8999999999999777.0184825 79.859659499999978 7.0815174999999995 79.940340499999976tag:blogger.com,1999:blog-5485374797223091724.post-29808774605734733972013-02-05T12:54:00.000-08:002013-09-26T16:52:38.440-07:00<h2>
<b>What are the Coding Standards & Best practises that we can use when C# project developing</b></h2>
<div>
<b><br /></b></div>
<div>
<table align="LEFT" style="background-color: white; color: #333333; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 12px; line-height: 19px;"><tbody>
<tr><td></td></tr>
</tbody></table>
<span style="background-color: white; color: #333333; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 12px; line-height: 19px;">Anybody can write code! And with a few months of programming experience, you can write 'working applications'. Making it work is easy, but doing it the most efficient way requires more work, than just making it work!</span></div>
<div>
<span style="background-color: white; color: #333333; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 12px; line-height: 19px;"><br /></span></div>
<div>
<table border="1" cellpadding="0" cellspacing="0" class="CodingStandards" style="border-collapse: collapse; border: none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: .1in .1in .1in .1in; mso-yfti-tbllook: 480; width: 590px;">
<tbody>
<tr>
<td style="background: #FFFFCC; border: solid windowtext 1.0pt; mso-border-alt: solid windowtext .5pt; padding: .1in .1in .1in .1in; width: 6.15in;" valign="top" width="590"><div class="MsoNormal" style="margin-bottom: 12.0pt;">
<span style="font-family: Arial, sans-serif; font-size: 9pt;">Note : <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 12.0pt;">
<span style="font-family: Arial, sans-serif; font-size: 9pt;">The terms <u>Pascal
Casing</u> and <u>Camel Casing</u> are used throughout this document. <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 12.0pt;">
<b><span style="font-family: Arial, sans-serif; font-size: 9pt;">Pascal
Casing </span></b><span style="font-family: Arial, sans-serif; font-size: 9pt;">-<b> First character of all words are Upper Case
and other characters are lower case. </b><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 12.0pt;">
<span style="font-family: Arial, sans-serif; font-size: 9pt;">Example: <u>B</u>ack<u>C</u>olor<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 12.0pt;">
<b><span style="font-family: Arial, sans-serif; font-size: 9pt;">Camel
Casing - </span></b><span style="font-family: Arial, sans-serif; font-size: 9pt;"><b>First character of all words, <u>except the
first word</u> are Upper Case and other characters are lower case.</b><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 12.0pt;">
<span style="font-family: Arial, sans-serif; font-size: 9pt;">Example: <u>b</u>ack<u>C</u>olor<o:p></o:p></span></div>
</td>
</tr>
</tbody></table>
</div>
<div>
<b><br /></b></div>
<div>
<b></b><br />
<div>
<b><span class="Apple-tab-span" style="white-space: pre;"> </span><span style="color: red;">Use Pascal casing for Class names </span></b></div>
<b>
<div>
<br /></div>
<div>
public class HelloWorld</div>
<div>
{</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>...</div>
<div>
}</div>
<div>
<br /></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span><span style="color: red;">Use Pascal casing for Method names </span></div>
<div>
<br /></div>
<div>
void SayHello(string name)</div>
<div>
{</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>...</div>
<div>
}</div>
<div>
<br /></div>
<div>
<br /></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span><span style="color: red;">Use Camel casing for variables and method parameters </span></div>
<div>
<br /></div>
<div>
int totalCount = 0;</div>
<div>
void SayHello(string name)</div>
<div>
{</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>string fullMessage = "Hello " + name;</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>...</div>
<div>
}</div>
<div>
<br /></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>Use the prefix “I” with Camel Casing for interfaces ( Example: IEntity )</div>
<div>
<br /></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span><span style="color: blue;">Do not use Hungarian notation to name variables. </span></div>
<div>
<br /></div>
<div>
In earlier days most of the programmers liked it - having the data type as a prefix for the variable name and using m_ as prefix for member variables. Eg: </div>
<div>
<br /></div>
<div>
string m_sName;</div>
<div>
int nAge;</div>
<div>
<br /></div>
<div>
However, in .NET coding standards, this is not recommended. Usage of data type and m_ to represent member variables should not be used. All variables should use camel casing. </div>
<div>
<br /></div>
<div>
Some programmers still prefer to use the prefix m_ to represent member variables, since there is no other easy way to identify a member variable.</div>
<div>
<br /></div>
<div>
<br /></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>Use Meaningful, descriptive words to name variables. Do not use abbreviations. </div>
<div>
<br /></div>
<div>
Good:</div>
<div>
<br /></div>
<div>
string address</div>
<div>
int salary </div>
<div>
button_save</div>
<div>
<br /></div>
<div>
Not Good:</div>
<div>
<br /></div>
<div>
string nam</div>
<div>
string addr</div>
<div>
int sal</div>
<div>
btnSave</div>
<div>
<br /></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span><span style="color: red;">Do not use single character variable names like i, n, s etc. Use names like index, temp </span></div>
<div>
<br /></div>
<div>
One exception in this case would be variables used for iterations in loops: </div>
<div>
<br /></div>
<div>
for ( int i = 0; i < count; i++ )</div>
<div>
{</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>...</div>
<div>
}</div>
<div>
<br /></div>
<div>
If the variable is used only as a counter for iteration and is not used anywhere else in the loop, many people still like to use a single char variable (i) instead of inventing a different suitable name. </div>
<div>
<br /></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span><span style="color: red;">Do not use underscores (_) for local variable names. </span></div>
<div>
<br /></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>All member variables must be prefixed with underscore (_) so that they can be identified from other local variables.</div>
<div>
<br /></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span><span style="color: red;">Do not use variable names that resemble keywords.</span></div>
<div>
<br /></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>Prefix boolean variables, properties and methods with “is” or similar prefixes.</div>
<div>
<br /></div>
<div>
Ex: private bool _isFinished</div>
<div>
<br /></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span><span style="color: red;">Namespace names should follow the standard pattern </span></div>
<div>
<br /></div>
<div>
<company name>.<product name>.<top level module>.<bottom level module></div>
<div>
<br /></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>Use appropriate prefix for the UI elements so that you can identify them from the rest of the variables.</div>
<div>
<b></b><br />
<div style="display: inline !important;">
<ul style="display: inline !important;"><b>
<li style="display: inline !important;"><b><div style="display: inline !important;">
<br /></div>
</b></li>
</b></ul>
</div>
<b>
</b></div>
<div>
<b></b><br />
<div style="display: inline !important;">
<ul style="display: inline !important;"><b>
<li style="display: inline !important;"><b><div style="display: inline !important;">
<br /></div>
</b></li>
</b></ul>
</div>
<b>
</b></div>
<div>
<b></b><br />
<div style="display: inline !important;">
<ul style="display: inline !important;"><b>
<li style="display: inline !important;"><b><div style="display: inline !important;">
There are 2 different approaches recommended here.</div>
</b></li>
</b></ul>
</div>
<b>
</b></div>
<div>
<br /></div>
<div>
<ul>
<li><b><div style="display: inline !important;">
<span class="Apple-tab-span" style="white-space: pre;"> </span>Use a common prefix ( ui_ ) for all UI elements. This will help you group all of the UI elements together and easy to access all of them from the intellisense.</div>
</b></li>
</ul>
</div>
<div>
<br /></div>
<div>
<ul>
<li><b><div style="display: inline !important;">
<span class="Apple-tab-span" style="white-space: pre;"> </span>Use appropriate prefix for each of the ui element. A brief list is given below. Since .NET has given several controls, you may have to arrive at a complete list of standard prefixes for each of the controls (including third party controls) you are using.</div>
</b></li>
</ul>
</div>
<div>
<b><span style="color: blue;">Do not use Hungarian notation(Prefix) to name variables. </span></b></div>
<div>
<br /></div>
<div>
Control<span class="Apple-tab-span" style="white-space: pre;"> </span> Prefix Correct way</div>
<div>
Label<span class="Apple-tab-span" style="white-space: pre;"> </span> lbl label_InsertProject</div>
<div>
<b></b><br />
<div style="display: inline !important;">
<b>TextBox<span class="Apple-tab-span" style="white-space: pre;"> </span> txt textbox_Save</b></div>
<b>
</b></div>
<div>
DataGrid <span class="Apple-tab-span" style="white-space: pre;"> </span>dtg datagrid_DusoatMsmber</div>
<div>
Button<span class="Apple-tab-span" style="white-space: pre;"> </span> btn button_Save</div>
<div>
ImageButton <span class="Apple-tab-span" style="white-space: pre;"> </span>imb</div>
<div>
Hyperlink<span class="Apple-tab-span" style="white-space: pre;"> </span> hlk</div>
<div>
DropDownList<span class="Apple-tab-span" style="white-space: pre;"> </span>ddl</div>
<div>
ListBox<span class="Apple-tab-span" style="white-space: pre;"> </span> lst</div>
<div>
DataList<span class="Apple-tab-span" style="white-space: pre;"> </span> dtl</div>
<div>
Repeater <span class="Apple-tab-span" style="white-space: pre;"> </span>rep</div>
<div>
Checkbox<span class="Apple-tab-span" style="white-space: pre;"> </span> chk</div>
<div>
CheckBoxList<span class="Apple-tab-span" style="white-space: pre;"> </span>cbl</div>
<div>
RadioButton<span class="Apple-tab-span" style="white-space: pre;"> </span> rdo</div>
<div>
RadioButtonList<span class="Apple-tab-span" style="white-space: pre;"> </span>rbl</div>
<div>
Image<span class="Apple-tab-span" style="white-space: pre;"> </span> img</div>
<div>
Panel<span class="Apple-tab-span" style="white-space: pre;"> </span> pnl</div>
<div>
PlaceHolder <span class="Apple-tab-span" style="white-space: pre;"> </span>phd</div>
<div>
Table<span class="Apple-tab-span" style="white-space: pre;"> </span> tbl</div>
<div>
Validators <span class="Apple-tab-span" style="white-space: pre;"> </span>val</div>
<div>
<br /></div>
<div>
<br /></div>
<div>
<br /></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span><span style="color: red;">File name should match with class name.</span></div>
<div>
<br /></div>
<div>
For example, for the class HelloWorld, the file name should be helloworld.cs (or, helloworld.vb) </div>
<div>
<br /></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>Use Pascal Case for file names.</div>
<div>
<br /></div>
<div>
<br /></div>
<div>
<br /></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span><span style="color: red;">Indentation and Spacing</span></div>
<div>
<br /></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>Use TAB for indentation. Do not use SPACES. Define the Tab size as 4.</div>
<div>
<br /></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>Comments should be in the same level as the code (use the same level of indentation). </div>
<div>
<br /></div>
<div>
Good:</div>
<div>
<br /></div>
<div>
// Format a message and display</div>
<div>
<br /></div>
<div>
string fullMessage = "Hello " + name;</div>
<div>
DateTime currentTime = DateTime.Now;</div>
<div>
string message = fullMessage + ", the time is : " + currentTime.ToShortTimeString();</div>
<div>
MessageBox.Show ( message );</div>
<div>
<br /></div>
<div>
Not Good:</div>
<div>
<br /></div>
<div>
// Format a message and display</div>
<div>
string fullMessage = "Hello " + name;</div>
<div>
DateTime currentTime = DateTime.Now;</div>
<div>
string message = fullMessage + ", the time is : " + currentTime.ToShortTimeString();</div>
<div>
MessageBox.Show ( message );</div>
<div>
<br /></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span><span style="color: red;">Curly braces ( {} ) should be in the same level as the code outside the braces. </span></div>
<div>
<br /></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span> </div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>Use one blank line to separate logical groups of code. </div>
<div>
<br /></div>
<div>
Good:</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>bool SayHello ( string name )</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>{</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>string fullMessage = "Hello " + name;</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>DateTime currentTime = DateTime.Now;</div>
<div>
<br /></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>string message = fullMessage + ", the time is : " + currentTime.ToShortTimeString();</div>
<div>
<br /></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>MessageBox.Show ( message );</div>
<div>
<br /></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>if ( ... )</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>{</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>// Do something</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>// ...</div>
<div>
<br /></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>return false;</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>}</div>
<div>
<br /></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>return true;</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>}</div>
<div>
<br /></div>
<div>
Not Good:</div>
<div>
<br /></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>bool SayHello (string name)</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>{</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>string fullMessage = "Hello " + name;</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>DateTime currentTime = DateTime.Now;</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>string message = fullMessage + ", the time is : " + currentTime.ToShortTimeString();</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>MessageBox.Show ( message );</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>if ( ... )</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>{</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>// Do something</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>// ...</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>return false;</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>}</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>return true;</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>}</div>
<div>
<br /></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span><span style="color: red;">There should be one and only one single blank line between each method inside the class. </span></div>
<div>
<br /></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>The curly braces should be on a separate line and not in the same line as if, for etc. </div>
<div>
<br /></div>
<div>
Good: </div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>if ( ... )<span class="Apple-tab-span" style="white-space: pre;"> </span></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>{</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>// Do something</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>}</div>
<div>
<br /></div>
<div>
Not Good: </div>
<div>
<br /></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>if ( ... )<span class="Apple-tab-span" style="white-space: pre;"> </span>{</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>// Do something</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>}</div>
<div>
<br /></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>Use a single space before and after each operator and brackets. </div>
<div>
<br /></div>
<div>
Good: </div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>if ( showResult == true )</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>{</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>for ( int i = 0; i < 10; i++ )</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>{</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>//</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>}</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>}</div>
<div>
<br /></div>
<div>
Not Good: </div>
<div>
<br /></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>if(showResult==true)</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>{</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>for(int<span class="Apple-tab-span" style="white-space: pre;"> </span> i= 0;i<10;i++)</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>{</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>//</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>}</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>}</div>
<div>
<br /></div>
<div>
<br /></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>Use #region to group related pieces of code together. If you use proper grouping using #region, the page should like this when all definitions are collapsed.</div>
<div>
<br /></div>
<div>
</div>
<div>
<br /></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>Keep private member variables, properties and methods in the top of the file and public members in the bottom. </div>
<div>
<br /></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>Good Programming practices</div>
<div>
<br /></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>Avoid writing very long methods. A method should typically have 1~25 lines of code. If a method has more than 25 lines of code, you must consider re factoring into separate methods. </div>
<div>
<br /></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>Method name should tell what it does. Do not use mis-leading names. If the method name is obvious, there is no need of documentation explaining what the method does. </div>
<div>
<br /></div>
<div>
Good: </div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>void SavePhoneNumber ( string phoneNumber )</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>{</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>// Save the phone number.</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>}</div>
<div>
<br /></div>
<div>
Not Good: </div>
<div>
<br /></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>// This method will save the phone number.</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>void SaveDetails ( string phoneNumber )</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>{</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>// Save the phone number.</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>}</div>
<div>
<br /></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>A method should do only 'one job'. Do not combine more than one job in a single method, even if those jobs are very small. </div>
<div>
<br /></div>
<div>
Good: </div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>// Save the address.</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>SaveAddress ( address );</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>// Send an email to the supervisor to inform that the address is updated.</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>SendEmail ( address, email );<span class="Apple-tab-span" style="white-space: pre;"> </span></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>void SaveAddress ( string address )</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>{</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>// Save the address.</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>// ...</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>}</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>void SendEmail ( string address, string email )</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>{</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>// Send an email to inform the supervisor that the address is changed.</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>// ...</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>}</div>
<div>
<br /></div>
<div>
Not Good: </div>
<div>
<br /></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>// Save address and send an email to the supervisor to inform that </div>
<div>
// the address is updated.</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>SaveAddress ( address, email );</div>
<div>
<br /></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>void SaveAddress ( string address, string email )</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>{</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>// Job 1.</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>// Save the address.</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>// ...</div>
<div>
<br /></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>// Job 2.</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>// Send an email to inform the supervisor that the address is changed.</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>// ...</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>}</div>
<div>
<br /></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>Use the c# or VB.NET specific types (aliases), rather than the types defined in System namespace. </div>
<div>
<br /></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>int age; (not Int16)</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>string name; (not String)</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>object contactInfo; (not Object)</div>
<div>
<br /></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span></div>
<div>
Some developers prefer to use types in Common Type System than language specific aliases.</div>
<div>
<br /></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>Always watch for unexpected values. For example, if you are using a parameter with 2 possible values, never assume that if one is not matching then the only possibility is the other value.</div>
<div>
<br /></div>
<div>
Good:</div>
<div>
<br /></div>
<div>
If ( memberType == eMemberTypes.Registered )</div>
<div>
{</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>// Registered user… do something…</div>
<div>
}</div>
<div>
else if ( memberType == eMemberTypes.Guest )</div>
<div>
{</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>// Guest user... do something…</div>
<div>
}</div>
<div>
else</div>
<div>
{</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>// Un expected user type. Throw an exception</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>throw new Exception (“Un expected value “ + memberType.ToString() + “’.”)</div>
<div>
<br /></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>// If we introduce a new user type in future, we can easily find </div>
<div>
// the problem here.</div>
<div>
}</div>
<div>
<br /></div>
<div>
Not Good:</div>
<div>
<br /></div>
<div>
If ( memberType == eMemberTypes.Registered )</div>
<div>
{</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>// Registered user… do something…</div>
<div>
}</div>
<div>
else</div>
<div>
{</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>// Guest user... do something…</div>
<div>
<br /></div>
<div>
// If we introduce another user type in future, this code will </div>
<div>
// fail and will not be noticed.</div>
<div>
}</div>
<div>
<br /></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>Do not hardcode numbers. Use constants instead. Declare constant in the top of the file and use it in your code.</div>
<div>
<br /></div>
<div>
However, using constants are also not recommended. You should use the constants in the config file or database so that you can change it later. Declare them as constants only if you are sure this value will never need to be changed.</div>
<div>
<br /></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>Do not hardcode strings. Use resource files. </div>
<div>
<br /></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>Convert strings to lowercase or upper case before comparing. This will ensure the string will match even if the string being compared has a different case.</div>
<div>
<br /></div>
<div>
if ( name.ToLower() == “john” )</div>
<div>
{</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span> //…</div>
<div>
}</div>
<div>
<br /></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>Use String.Empty instead of “”</div>
<div>
<br /></div>
<div>
Good:</div>
<div>
<br /></div>
<div>
If ( name == String.Empty )</div>
<div>
{</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>// do something</div>
<div>
}</div>
<div>
<br /></div>
<div>
Not Good:</div>
<div>
<br /></div>
<div>
If ( name == “” )</div>
<div>
{</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>// do something</div>
<div>
}</div>
<div>
<br /></div>
<div>
<br /></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>Avoid using member variables. Declare local variables wherever necessary and pass it to other methods instead of sharing a member variable between methods. If you share a member variable between methods, it will be difficult to track which method changed the value and when. </div>
<div>
<br /></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>Use enum wherever required. Do not use numbers or strings to indicate discrete values. </div>
<div>
<br /></div>
<div>
Good: </div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>enum MailType</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>{</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>Html,</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>PlainText,</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>Attachment</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>}</div>
<div>
<br /></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>void SendMail (string message, MailType mailType)</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>{</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>switch ( mailType )</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>{</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>case MailType.Html:</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>// Do something</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>break;</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>case MailType.PlainText:</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>// Do something</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>break;</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>case MailType.Attachment:</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>// Do something</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>break;</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>default:</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>// Do something</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>break;</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>}</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>}</div>
<div>
<br /></div>
<div>
<br /></div>
<div>
Not Good: </div>
<div>
<br /></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>void SendMail (string message, string mailType)</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>{</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>switch ( mailType )</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>{</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>case "Html":</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>// Do something</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>break;</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>case "PlainText":</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>// Do something</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>break;</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>case "Attachment":</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>// Do something</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>break;</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>default:</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>// Do something</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>break;</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>}</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>}</div>
<div>
<br /></div>
<div>
<br /></div>
<div>
<ul>
<li><b><div style="display: inline !important;">
<span class="Apple-tab-span" style="white-space: pre;"> </span>Do not make the member variables public or protected. Keep them private and expose public/protected Properties. </div>
</b></li>
</ul>
</div>
<div>
<br /></div>
<div>
<ul>
<li><b><div style="display: inline !important;">
<span class="Apple-tab-span" style="white-space: pre;"> </span>The event handler should not contain the code to perform the required action. Rather call another method from the event handler.</div>
</b></li>
</ul>
</div>
<div>
<br /></div>
<div>
<ul>
<li><b><div style="display: inline !important;">
<span class="Apple-tab-span" style="white-space: pre;"> </span>Do not programmatically click a button to execute the same action you have written in the button click event. Rather, call the same method which is called by the button click event handler.</div>
</b></li>
</ul>
</div>
<div>
<br /></div>
<div>
<ul>
<li><b><div style="display: inline !important;">
<span class="Apple-tab-span" style="white-space: pre;"> </span>Never hardcode a path or drive name in code. Get the application path programmatically and use relative path. </div>
</b></li>
</ul>
</div>
<div>
<br /></div>
<div>
<ul>
<li><b><div style="display: inline !important;">
<span class="Apple-tab-span" style="white-space: pre;"> </span>Never assume that your code will run from drive "C:". You may never know, some users may run it from network or from a "Z:". </div>
</b></li>
</ul>
</div>
<div>
<br /></div>
<div>
<ul>
<li><b><div style="display: inline !important;">
<span class="Apple-tab-span" style="white-space: pre;"> </span>In the application start up, do some kind of "self check" and ensure all required files and dependancies are available in the expected locations. Check for database connection in start up, if required. Give a friendly message to the user in case of any problems. </div>
</b></li>
</ul>
</div>
<div>
<br /></div>
<div>
<ul>
<li><b><div style="display: inline !important;">
<span class="Apple-tab-span" style="white-space: pre;"> </span>If the required configuration file is not found, application should be able to create one with default values. </div>
</b></li>
</ul>
</div>
<div>
<br /></div>
<div>
<ul>
<li><b><div style="display: inline !important;">
<span class="Apple-tab-span" style="white-space: pre;"> </span>If a wrong value found in the configuration file, application should throw an error or give a message and also should tell the user what are the correct values. </div>
</b></li>
</ul>
</div>
<div>
<br /></div>
<div>
<ul>
<li><b><div style="display: inline !important;">
<span class="Apple-tab-span" style="white-space: pre;"> </span>Error messages should help the user to solve the problem. Never give error messages like "Error in Application", "There is an error" etc. Instead give specific messages like "Failed to update database. Please make sure the login id and password are correct." </div>
</b></li>
</ul>
</div>
<div>
<br /></div>
<div>
<ul>
<li><b><div style="display: inline !important;">
<span class="Apple-tab-span" style="white-space: pre;"> </span>When displaying error messages, in addition to telling what is wrong, the message should also tell what should the user do to solve the problem. Instead of message like "Failed to update database.", suggest what should the user do: "Failed to update database. Please make sure the login id and password are correct." </div>
</b></li>
</ul>
</div>
<div>
<br /></div>
<div>
<ul>
<li><b><div style="display: inline !important;">
<span class="Apple-tab-span" style="white-space: pre;"> </span>Show short and friendly message to the user. But log the actual error with all possible information. This will help a lot in diagnosing problems. </div>
</b></li>
</ul>
</div>
<div>
<br /></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span><span style="color: red;">Do not have more than one class in a single file.</span></div>
<div>
<br /></div>
<div>
<ul>
<li><b><div style="display: inline !important;">
<span class="Apple-tab-span" style="white-space: pre;"> </span>Have your own templates for each of the file types in Visual Studio. You can include your company name, copy right information etc in the template. You can view or edit the Visual Studio file templates in the folder C:\Program Files\Microsoft Visual Studio 8\Common7\IDE\ItemTemplatesCache\CSharp\1033. (This folder has the templates for C#, but you can easily find the corresponding folders or any other language)</div>
</b></li>
</ul>
</div>
<div>
<br /></div>
<div>
<ul>
<li><b><div style="display: inline !important;">
<span class="Apple-tab-span" style="white-space: pre;"> </span>Avoid having very large files. If a single file has more than 1000 lines of code, it is a good candidate for refactoring. Split them logically into two or more classes.</div>
</b></li>
</ul>
</div>
<div>
<br /></div>
<div>
<ul>
<li><b><div style="display: inline !important;">
<span class="Apple-tab-span" style="white-space: pre;"> </span>Avoid public methods and properties, unless they really need to be accessed from outside the class. Use “internal” if they are accessed only within the same assembly.</div>
</b></li>
</ul>
</div>
<div>
<br /></div>
<div>
<ul>
<li><b><div style="display: inline !important;">
<span class="Apple-tab-span" style="white-space: pre;"> </span>Avoid passing too many parameters to a method. If you have more than 4~5 parameters, it is a good candidate to define a class or structure.</div>
</b></li>
</ul>
</div>
<div>
<br /></div>
<div>
<ul>
<li><b><div style="display: inline !important;">
<span class="Apple-tab-span" style="white-space: pre;"> </span>If you have a method returning a collection, return an empty collection instead of null, if you have no data to return. For example, if you have a method returning an ArrayList, always return a valid ArrayList. If you have no items to return, then return a valid ArrayList with 0 items. This will make it easy for the calling application to just check for the “count” rather than doing an additional check for “null”.</div>
</b></li>
</ul>
</div>
<div>
<br /></div>
<div>
<ul>
<li><b><div style="display: inline !important;">
<span class="Apple-tab-span" style="white-space: pre;"> </span>Use the AssemblyInfo file to fill information like version number, description, company name, copyright notice etc.</div>
</b></li>
</ul>
</div>
<div>
<br /></div>
<div>
<ul>
<li><b><div style="display: inline !important;">
<span class="Apple-tab-span" style="white-space: pre;"> </span>Logically organize all your files within appropriate folders. Use 2 level folder hierarchies. You can have up to 10 folders in the root folder and each folder can have up to 5 sub folders. If you have too many folders than cannot be accommodated with the above mentioned 2 level hierarchy, you may need re factoring into multiple assemblies.</div>
</b></li>
</ul>
</div>
<div>
<br /></div>
<div>
<ul>
<li><b><div style="display: inline !important;">
<span class="Apple-tab-span" style="white-space: pre;"> </span>Make sure you have a good logging class which can be configured to log errors, warning or traces. If you configure to log errors, it should only log errors. But if you configure to log traces, it should record all (errors, warnings and trace). Your log class should be written such a way that in future you can change it easily to log to Windows Event Log, SQL Server, or Email to administrator or to a File etc without any change in any other part of the application. Use the log class extensively throughout the code to record errors, warning and even trace messages that can help you trouble shoot a problem.</div>
</b></li>
</ul>
</div>
<div>
<br /></div>
<div>
<ul>
<li><b><div style="display: inline !important;">
<span class="Apple-tab-span" style="white-space: pre;"> </span>If you are opening database connections, sockets, file stream etc, always close them in the finally block. This will ensure that even if an exception occurs after opening the connection, it will be safely closed in the finally block.</div>
</b></li>
</ul>
</div>
<div>
<br /></div>
<div>
<ul>
<li><b><div style="display: inline !important;">
<span class="Apple-tab-span" style="white-space: pre;"> </span>Declare variables as close as possible to where it is first used. Use one variable declaration per line.</div>
</b></li>
</ul>
</div>
<div>
<br /></div>
<div>
<ul>
<li><b><div style="display: inline !important;">
<span class="Apple-tab-span" style="white-space: pre;"> </span>Use StringBuilder class instead of String when you have to manipulate string objects in a loop. The String object works in weird way in .NET. Each time you append a string, it is actually discarding the old string object and recreating a new object, which is a relatively expensive operations.</div>
</b></li>
</ul>
</div>
<div>
<br /></div>
<div>
Consider the following example:</div>
<div>
<br /></div>
<div>
public string ComposeMessage (string[] lines)</div>
<div>
{ </div>
<div>
string message = String.Empty; </div>
<div>
<br /></div>
<div>
for (int i = 0; i < lines.Length; i++)</div>
<div>
{ </div>
<div>
message += lines [i]; </div>
<div>
}</div>
<div>
<br /></div>
<div>
return message;</div>
<div>
}</div>
<div>
<br /></div>
<div>
In the above example, it may look like we are just appending to the string object ‘message’. But what is happening in reality is, the string object is discarded in each iteration and recreated and appending the line to it.</div>
<div>
<br /></div>
<div>
If your loop has several iterations, then it is a good idea to use StringBuilder class instead of String object.</div>
<div>
<br /></div>
<div>
See the example where the String object is replaced with StringBuilder.</div>
<div>
<br /></div>
<div>
public string ComposeMessage (string[] lines)</div>
<div>
{</div>
<div>
StringBuilder message = new StringBuilder();</div>
<div>
<br /></div>
<div>
for (int i = 0; i < lines.Length; i++)</div>
<div>
{</div>
<div>
message.Append( lines[i] ); </div>
<div>
}</div>
<div>
<br /></div>
<div>
return message.ToString();</div>
<div>
}</div>
<div>
<br /></div>
<div>
<br /></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>Architecture</div>
<div>
<br /></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>Always use multi layer (N-Tier) architecture. </div>
<div>
<br /></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>Never access database from the UI pages. Always have a data layer class which performs all the database related tasks. This will help you support or migrate to another database back end easily.</div>
<div>
<br /></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>Use try-catch in your data layer to catch all database exceptions. This exception handler should record all exceptions from the database. The details recorded should include the name of the command being executed, stored proc name, parameters, connection string used etc. After recording the exception, it could be re thrown so that another layer in the application can catch it and take appropriate action.</div>
<div>
<br /></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>Separate your application into multiple assemblies. Group all independent utility classes into a separate class library. All your database related files can be in another class library.</div>
<div>
<br /></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>ASP.NET</div>
<div>
<br /></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>Do not use session variables throughout the code. Use session variables only within the classes and expose methods to access the value stored in the session variables. A class can access the session using System.Web.HttpCOntext.Current.Session</div>
<div>
<br /></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>Do not store large objects in session. Storing large objects in session may consume lot of server memory depending on the number of users.</div>
<div>
<br /></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>Always use style sheet to control the look and feel of the pages. Never specify font name and font size in any of the pages. Use appropriate style class. This will help you to change the UI of your application easily in future. Also, if you like to support customizing the UI for each customer, it is just a matter of developing another style sheet for them</div>
<div>
<br /></div>
<div>
<ul>
<li><b><div style="display: inline !important;">
<span class="Apple-tab-span" style="white-space: pre;"> </span><u>Comments</u></div>
</b></li>
</ul>
</div>
<div>
<br /></div>
<div>
Good and meaningful comments make code more maintainable. However, </div>
<div>
<br /></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>Do not write comments for every line of code and every variable declared. </div>
<div>
<br /></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>Use // or /// for comments. Avoid using /* … */</div>
<div>
<br /></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>Write comments wherever required. But good readable code will require very less comments. If all variables and method names are meaningful, that would make the code very readable and will not need many comments. </div>
<div>
<br /></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>Do not write comments if the code is easily understandable without comment. The drawback of having lot of comments is, if you change the code and forget to change the comment, it will lead to more confusion.</div>
<div>
<br /></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>Fewer lines of comments will make the code more elegant. But if the code is not clean/readable and there are less comments, that is worse. </div>
<div>
<br /></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>If you have to use some complex or weird logic for any reason, document it very well with sufficient comments. </div>
<div>
<br /></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>If you initialize a numeric variable to a special number other than 0, -1 etc, document the reason for choosing that value. </div>
<div>
<br /></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>The bottom line is, write clean, readable code such a way that it doesn't need any comments to understand. </div>
<div>
<br /></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>Perform spelling check on comments and also make sure proper grammar and punctuation is used. </div>
<div>
<br /></div>
<div>
<ul>
<li><b><div style="display: inline !important;">
<span class="Apple-tab-span" style="white-space: pre;"> </span><u>Exception Handling</u></div>
</b></li>
</ul>
</div>
<div>
<br /></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>Never do a 'catch exception and do nothing'. If you hide an exception, you will never know if the exception happened or not. Lot of developers uses this handy method to ignore non significant errors. You should always try to avoid exceptions by checking all the error conditions programmatically. In any case, catching an exception and doing nothing is not allowed. In the worst case, you should log the exception and proceed.</div>
<div>
<br /></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>In case of exceptions, give a friendly message to the user, but log the actual error with all possible details about the error, including the time it occurred, method and class name etc. </div>
<div>
<br /></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>Always catch only the specific exception, not generic exception. </div>
<div>
<br /></div>
<div>
Good: </div>
<div>
<br /></div>
<div>
<br /></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>void ReadFromFile ( string fileName )</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>{</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>try</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>{</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>// read from file.</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>}</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>catch (FileIOException ex)</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>{</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>// log error.</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>// re-throw exception depending on your case.</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>throw;</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>}</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>}</div>
<div>
<br /></div>
<div>
Not Good: </div>
<div>
<br /></div>
<div>
<br /></div>
<div>
void ReadFromFile ( string fileName )</div>
<div>
{</div>
<div>
try</div>
<div>
{</div>
<div>
// read from file.</div>
<div>
}</div>
<div>
catch (Exception ex)<span class="Apple-tab-span" style="white-space: pre;"> </span></div>
<div>
{</div>
<div>
// Catching general exception is bad... we will never know whether </div>
<div>
// it was a file error or some other error.<span class="Apple-tab-span" style="white-space: pre;"> </span></div>
<div>
// Here you are hiding an exception. </div>
<div>
// In this case no one will ever know that an exception happened.</div>
<div>
<br /></div>
<div>
return "";<span class="Apple-tab-span" style="white-space: pre;"> </span></div>
<div>
}</div>
<div>
}</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span></div>
<div>
<br /></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>No need to catch the general exception in all your methods. Leave it open and let the application crash. This will help you find most of the errors during development cycle. You can have an application level (thread level) error handler where you can handle all general exceptions. In case of an 'unexpected general error', this error handler should catch the exception and should log the error in addition to giving a friendly message to the user before closing the application, or allowing the user to 'ignore and proceed'. </div>
<div>
<br /></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>When you re throw an exception, use the throw statement without specifying the original exception. This way, the original call stack is preserved.</div>
<div>
<br /></div>
<div>
Good:</div>
<div>
<br /></div>
<div>
catch</div>
<div>
{</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>// do whatever you want to handle the exception </div>
<div>
<br /></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>throw;<span class="Apple-tab-span" style="white-space: pre;"> </span></div>
<div>
}</div>
<div>
<br /></div>
<div>
Not Good:</div>
<div>
<br /></div>
<div>
catch (Exception ex)</div>
<div>
{</div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>// do whatever you want to handle the exception </div>
<div>
<br /></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>throw ex;</div>
<div>
}</div>
<div>
<br /></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>Do not write try-catch in all your methods. Use it only if there is a possibility that a specific exception may occur and it cannot be prevented by any other means. For example, if you want to insert a record if it does not already exists in database, you should try to select record using the key. Some developers try to insert a record without checking if it already exists. If an exception occurs, they will assume that the record already exists. This is strictly not allowed. You should always explicitly check for errors rather than waiting for exceptions to occur. On the other hand, you should always use exception handlers while you communicate with external systems like network, hardware devices etc. Such systems are subject to failure anytime and error checking is not usually reliable. In those cases, you should use exception handlers and try to recover from error.</div>
<div>
<br /></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>Do not write very large try-catch blocks. If required, write separate try-catch for each task you perform and enclose only the specific piece of code inside the try-catch. This will help you find which piece of code generated the exception and you can give specific error message to the user. </div>
<div>
<br /></div>
<div>
<span class="Apple-tab-span" style="white-space: pre;"> </span>Write your own custom exception classes if required in your application. Do not derive your custom exceptions from </div>
<div>
<br /></div>
</b></div>
Anonymoushttp://www.blogger.com/profile/13786432889535633028noreply@blogger.com0Kandana, Sri Lanka7.05 79.8999999999999777.0184825 79.859659499999978 7.0815174999999995 79.940340499999976