Posts

Showing posts from September, 2008

Fields Vs Properties

In .NET, we can define attributes of a class as a field like: public class Employee { private System.Int32 employeeId = 0; ... } And if, we want this attribute to be accessed by other clasess, we make it public like : public System.Int32 EmployeeId = 0; We, also have an alternate way of declaring the same thing, as a property like: public class Employee { private System.Int32 employeeId = 0; public System.Int32 EmployeeId { get { return employeeId ; } set { employeeId = value; } } } Now, EmployeeId is a property, which is exposed to other classes. We can think and figure out the difference between the two approaches - of defining it as a public field or a property. If we declare the attribute as a property , we can go ahead and modify the get-set section like: public class Employee { private System.Int32 employeeId = 0; public System.Int32 EmployeeId { get { return employeeId ; } set { if (value>0) { employeeId=value; } } } } What is be

ArrayList Vs HashTable

ArrayList and HashTables are data structures provided by .NET framework.Both are collections and are bundled under namespace System.Collections. To create an ArrayList : ArrayList employees = new ArrayList(); employees .Add("Ram"); To create a HashTable: HashTable employees = new HashTable(); employees .Add("E1001,"Ram"); To understand, the difference we need to understand the way they store objects. In case of an ArrayList, it keeps on incrementing the index and stores the objects as you add them.The elements stored will not be sorted automatically, unless you explicitly sort them, by using Sort() or ReverseSort() methods of arraylist.In case of a HashTable, hash value of the key is computed and the object is stored in respective bucket.So the elements are stored in a sorted order. This provides an advantage when it comes to searching a specific element. Incase of a hash table, it is faster as compared to the array list.Search is simplified by search

Email Templates in ASP.NET

Till recently, whenever it was required to send an email, through a web application using SMTP, I would go ahead and use SMTPClient, a class provided by .NET Framework.All I needed to do for this, is to include namespace System.Net.Mail and I am ready to go.I would read the SMTP host IP, port number and timeout value from the configuration file and write a code snippet like below: String hostIP=Convert.ToString(ConfigurationManager.AppSettings["SMTPHostIP"]); Int16 portNumber = Convert.ToInt16(ConfigurationManager.AppSettings["SMTPPort"]); Int32 timeout = Convert.ToInt32(ConfigurationManager.AppSettings["SMTPTimeout"]); SmtpClient smtpClient = new SmtpClient(); smtpClient.Timeout = timeout; smtpClient.Host =hostIP; smtpClient.Port = portNumber; My next steps are to instantiate MailMessage class provided under the same namespace and invoke Send method, as below: MailMessage mailMessage = new MailMessage (fromAddress ,toAddress ,subject ,mess