Saturday, January 30, 2010

Everything Is an Object

In a true object-oriented language, all problem domain entities are expressed through the concept of objects. (Note that in this book I'll be using the Coad/Yourdon definition for "problem domain"-that is, that a problem domain is the problem you're attempting to solve, in terms of its specific complexities, terminology, challenges, and so on.) As you might guess, objects are the central idea behind object-oriented programming. Most of us don't walk around thinking in terms of structures, data packets, function calls, and pointers; instead, we typically think in terms of objects. Let's look at an example.
If you were writing an invoicing application and you needed to tally the detail lines of the invoice, which of the following mental approaches would be more intuitive from the client's perspective? -
  • Non-object-oriented approach I'll have access to a data structure representing an invoice header. This invoice header structure will also include a doubly linked list of invoice detail structures, each of which contains a total line amount.Therefore, to get an invoice total, I need to declare a variable named something like totalInvoiceAmount and initialize it to 0, get a pointer to the invoice header structure, get the head of the linked list of detail lines, and then traverse the linked list of detail lines. As I read each detail line structure, I'll get its member variable containing the total for that line and increment my totalInvoiceAmount variable.
  • Object-oriented approach I'll have an invoice object, and I'll send a message to that object to ask it for the total amount. I don't need to think about how the information is stored internally in the object, as I had to do with the non-object-oriented data structure. I simply treat the object in a natural manner, making requests to it by sending messages. (The group of messages that an object can process are collectively called the object's interface. In the following paragraph, I'll explain why thinking in terms of interface rather than implementation, as I have done here, is justifiable in the object-oriented approach.)
Obviously, the object-oriented approach is more intuitive and closer to how many of us would think our way through a problem. In the second solution, the invoice object probably iterates through a collection of invoice detail objects, sending a message to each one requesting its line amount. However, if what you're looking for is the total, you don't care how it's done. You don't care because oneof the main tenets of object-oriented programming is encapsulation-the ability of an object to hide its internal data and methods and to present an interface that makes the important parts of the object programmatically accessible. The internals of how an object carries out its job are unimportant as long as that object can carry out that job. You are simply presented with an interface to the object, and you use that interface to make the object perform a given task on your behalf. (I'll further explain the concepts of encapsulation and interfaces later in this chapter.) The point here is that programs written to simulate the real-world objects of the problem domain are much easier to design and write because they allow us to think in a more natural way.
Notice that the second approach required an object to perform work on your behalf-that is, to total the detail lines. An object doesn't contain data only, as a structure does. Objects, by definition, comprise data and the methods that work on that data. This means that when working with a problem domain we can do more than design the necessary data structures. We can also look at which methods should be associated with a given object so that the object is a fully encapsulatedbit of functionality. The examples that follow here and in the coming sections help illustrate this concept.
NOTE
The code snippets in this chapter present the concepts of object-oriented programming. Keep in mind that while I present many example code snippets in C#, the concepts themselves are generic to OOP and are not specific to any one programming language. For comparison purposes in this chapter, I'll also present examples in C, which is not object-oriented.
Let's say you're writing an application to calculate the pay of your new company's only employee, Amy. Using C, you would code something similar to the following to associate certain data with an employee: -
struct EMPLOYEE
{
    char szFirstName[25];
    char szLastName[25];

    int iAge;

    double dPayRate;

};
Here's how you'd calculate Amy's pay by using the EMPLOYEE structure: -
void main()
{
    double dTotalPay;

    struct EMPLOYEE* pEmp;
    pEmp = (struct EMPLOYEE*)malloc(sizeof(struct EMPLOYEE));

    if (pEmp)
    {
        pEmp->dPayRate = 100;

        strcpy(pEmp->szFirstName, "Amy");
        strcpy(pEmp->szLastName, "Anderson");
        pEmp->iAge = 28;

        dTotalPay = pEmp->dPayRate * 40;
        printf("Total Payment for %s %s is %0.2f",
                pEmp->szFirstName, pEmp->szLastName, dTotalPay);
    }

    free(pEmp);
}
In this example, the code is based on data contained in a structure and some external (to that structure) code that uses that structure. So what's the problem? The main problem is one of abstraction: the user of the EMPLOYEE structure must know far too much about the data needed for an employee. Why? Let's say that at a later date you want to change how Amy's pay rate is calculated. For example, you might want to factor in FICA and other assorted taxes when determining a net payment. Not only would you have to change all client code that uses the EMPLOYEE structure, but you would also need to document-for any future programmers in your company-the fact that a change in usage had occurred.
Now let's look at a C# version of this example: -
using System;

class Employee
{
    public Employee(string firstName, string lastName,
                    int age, double payRate)
    {
        this.firstName = firstName;
        this.lastName = lastName;
        this.age = age;
        this.payRate = payRate;
    }

    protected string firstName;
    protected string lastName;
    protected int age;
    protected double payRate;

    public double CalculatePay(int hoursWorked)
    {
        // Calculate pay here.
        return (payRate * (double)hoursWorked);
    }
}

class EmployeeApp
{
    public static void Main()
    {
        Employee emp = new Employee ("Amy", "Anderson", 28, 100);
        Console.WriteLine("\nAmy's pay is $" + emp.CalculatePay(40));
    }
}
In the C# version of the EmployeeApp example, the object's user can simply call the object's CalculatePay method to have the object calculate its own pay. The advantage of this approach is that the user no longer needs to worry about the internals of exactly how the pay is calculated. If at some time in the future you decide to modify how the pay is calculated, that modification will have no impact on existing code. This level of abstraction is one of the basic benefits of using objects.
Now, one valid comment might be that I could have abstracted the C client's code by creating a function to access the EMPLOYEE structure. However, the fact that I'd have to create this function completely apart from the structure being worked on is exactly the problem. When you use an object-oriented language such as C#, an object's data and the methods that operate on that data (its interface) are always together.
Keep in mind that only an object's methods should modify an object's variables. As you can see in the previous example, each Employee member variable is declared with the protected access modifier, except for the actual CalculatePay method, which is defined as public. Access modifiers are used to specify the level of access that derived class and client code has to a given class member. In the case of the protected modifier, a derived class would have access to the member, but client code would not. The public modifier makes the member accessible to both derived classes and client code. I'll go into more detail on access modifiers in Chapter 5, "Classes," but the key thing to remember for now is that modifiers enable you to protect key class members from being used incorrectly.

Tuesday, January 26, 2010

Allow only numbers/digits in TextBox


<HTML>
<HEAD>
<SCRIPT language=Javascript>

function isNumberKey(evt)
{
var charCode = (evt.which) ? evt.which : event.keyCode
if (charCode > 31 && (charCode <> 57))
return false;

return true;
}

// Name Space

using System;

using System.Net;

using System.Net.Mail;

//Function

private void MailSendMessage()

{

try

{

NetworkCredential loginInfo = new NetworkCredential("username@domain.com", "password");

MailMessage msg = new MailMessage();

msg.From = new MailAddress("from@domain.com","DisplayName");

msg.To.Add(new MailAddress("to@domain.com"));

msg.Subject = "Test";

msg.Body = "Test";

msg.IsBodyHtml = true;

SmtpClient client = new SmtpClient("smtp.gmail.com");

client.Port = 587;

client.EnableSsl = true;

client.UseDefaultCredentials = false;

client.Credentials = loginInfo;

client.Send(msg);

}

catch (Exception ex)

{

Response.Write("
[1] Message :"
+ ex.Message.ToString());

Response.Write("
[2] Stack Trace :"
+ ex.StackTrace.ToString());

}

}

Thursday, May 7, 2009

ASP.NET 1.1 vs 2.0

he latest version of ASP.NET, 2.0, is more than an upgrade—it is a major update of the technology, as it changes the way ASP.NET applications are designed, compiled, and deployed. Microsoft does recognize the existence of a large number of ASP.NET 1.1 installations, so 2.0 was designed to allow older applications to work without problems. On the other hand, the need to convert existing applications to the 2.0 platform may arise. With that said, let's examine the major differences between the versions, as well as areas that may be problems during the conversion process.Version changesWhile there are many changes between ASP.NET 1.1 and 2.0, there are certain ones that have a greater impact of
  • Code-behind model: ASP.NET 2.0 introduces the concept of partial classes, whereas a class can span multiple class files. It is a key feature used in Visual Studio 2005 to separate system-generated code from user code. This negates the scenario where you find yourself scrolling through source code and encountering the code generated by the system. The partial class approach reduces the risk of inadvertently editing system code and results in smaller code-behind files.
  • Directories: ASP.NET 2.0 has added a number of special directories where 1.1 had only the one required bin directory. Most of these new directories have the App_ prefix with names like App_Code, App_Data, App_GlobalResources, and App_LocalResources, while the bin directory still exists along with a themes directory. The new directories are a key aspect of 2.0's elimination of project files.
  • No more project files: Gone are the days of project files with ASP.NET and Visual Studio 2005. The project is now the complete Web project directory, which is a drastic shift from 1.1 and can cause migration issues as described later.
  • Compilation model: With 1.1, all code was compiled into one assembly placed in the bin directory. With 2.0, the assembly is separated into multiple assemblies. These multiple assemblies may be created on-the-fly or precompiled. Examples of multiple assemblies are one assembly for each ASP.NET directory like App_Code and App_Data as well as individual assemblies for Web Forms, User Controls, and so forth. This is a major shift in the application structure; it offers more deployment options in how the application is delivered to the users.
  • Application deployment: The deployment of 1.1 applications was fairly straightforward as you moved the content files along with the necessary assembly files—it could easily be accomplished with a simple copy and paste with limited options. The 2.0 model provides various options for deploying an application. For instance, you may choose to precompile all code and deploy it or go the other way with no precompilation. Also, you can lock down deployed content files so no changes may be made after deployment (this is a major difference from 1.1).

The list provides a high-level view of the main differences between ASP.NET 1.1 and 2.0 and what can affect the conversion of an existing application to 2.0. Now let's take a closer look at possible problem areas.