Showing posts with label Programming. Show all posts
Showing posts with label Programming. Show all posts

Friday, April 11, 2008

Binding ArrayList with DataGrid

I will discuss how to create an ArrayList of objects and bind it to a DataGrid control using DataSource property.

Step 1. Creating a Class

First step is to create a class. I call my class Developer, which is listed in Listing 1. It's properties are FirstName, LastName, Age, Skills, and Experience.

public class Developer

{
private string firstName;
private string lastName;
private int age;
private string skills;
private int experience;
public Developer(string firstName, string lastName, int age, string skills, int experience )
{
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
this.skills = skills;
this.experience = experience;
}
/// First Name
public string FirstName
{
get { return this.firstName ; }
}
/// Last Name
public string LastName
{
get { return this.lastName ; }
}
/// Age
public int Age
{
get { return this.age ; }
}
/// Skill set
public string Skills
{
get { return this.skills ; }
}
/// Number of year experience

public int Experience
{
get { return this.experience ; }
}
}

Listing 1. Developer class

Step 2. Creating an ArrayList of Objects

Now next step is to create an ArrayList object of Developer class. Listing 2 adds 6 items to the ArrayList.

private ArrayList GetList()

{
ArrayList list = new ArrayList();
list.Add(new Developer("Mahesh", "Chand", 30, "C#,ASP.NET,Windows Forms", 10)) ;
list.Add(new Developer("Michael", "Gold", 35, "GDI+, ASP.NET", 15)) ;
list.Add(new Developer("Bhasker", "Das", 26, "VB.NET, Web Applications", 4)) ;
list.Add(new Developer("Ashish", "Singhal", 24, "ADO.NET, GDI+", 4)) ;
list.Add(new Developer("Neel", "Beniwal", 3, "C#,ASP.NET,Windows Forms", 0)) ;
list.Add(new Developer("Melanie", "Talmadge", 25, "Java", 2)) ;
return list;
}

Listing 2. ArrayList of Developer

Step 3. Binding with DataGrid

Now we can bind our ArrayList using DataGrid.DataSource property and DataGrid would understand what to display in its columns. The following code binds the ArrayList to DataGrid.

ArrayList list = GetList();

dataGrid1.DataSource = list;


Step 4. The Result

Now if I run my application, the output in DataGrid looks like Figure 1.


Wednesday, March 26, 2008

C# Vs Java

C# is not a competitor to Java. Well, this is what Microsoft claims it to be. Every time someone writes or compares Java to C#, Microsoft always cries out foul. It is unfair to compare the two languages. As more developers start to play around with C#, many are starting to see strong similarities between the two languages.

Productivity is the key for development team

You can develop applications for the Microsoft platform using Java. But it's more sensible to use a .NET language instead for one simple reason: productivity. Microsoft makes it easier to take advantage of the Windows platform than Java does. Creating Windows services, writing to event logs, debugging SQL Server stored procedures, accessing the registry, working with COM—all of these are simpler in terms of time to market in .NET than they are in Java. It's not that C# is better than Java. But Microsoft has created all the classes to handle Windows-based tasks and made them available through wizards, drag and drop, and point and click.

Java is designed to be platform independent, so naturally it doesn't ship with as many Windows-rich features. Java developers who sit through the .NET classes often let out audible gasps, followed by jokes about how Microsoft is putting developers out of jobs because of the ease by which much .NET development is done.

The .NET decision

A debate on the technical merits of one language vs. the other is for the most part a moot discussion; you can pretty much do the same things in both languages, with minor exceptions. So at this point, your decision should be based on the answer to this question: “Does the application need to run only on the Microsoft platform?” If the answer is yes, go with a .NET language—primarily C#.

Bear in mind that we will, no doubt, eventually see .NET-written applications run on Linux and other platforms, but that day is still some time away. For now, pick your platform first and your language second.

Post back in ASP .NET

In old ASP, classic ASP, using post method in form is to post the values of a Form to another page. The another asp page will receive the data and processing it on the server side.

With ASP .NET, it is already changed. They are separated a entity with ability to process its own posted data. So the values of the Form are posted to the same page and the same page can process the data. It is called post back.

This post back is a read only property with each ASP .NET Page (System.Web.UI.Page) class. This is false when the first time the page is loaded and is true when the page is submitted and processed. This enables users to write the code depending on if the post back is true or false (with the use of the function Page.IsPostBack()).

Control it with AJAX. Then you can get the nice performance.

Wednesday, March 19, 2008

Difference between Composition and Aggregation!!!

Composition:

As we know, inheritance gives us an 'is-a' relationship. To make the understanding of composition easier, we can say that composition gives us a 'part-of' relationship. Composition is shown on a UML diagram as a filled diamond (see Figure 1).

Figure 1 - Composition

If we were going to model a car, it would make sense to say that an engine is part-of a car. Within composition, the lifetime of the part (Engine) is managed by the whole (Car), in other words, when Car is destroyed, Engine is destroyed along with it. So how do we express this in C#?

public class Engine
{
. . .
}

public class Car

{

Engine e = new Engine();

.......

}

As you can see in the example code above, Car manages the lifetime of Engine.

Aggregation:

If inheritance gives us 'is-a' and composition gives us 'part-of', we could argue that aggregation gives us a 'has-a' relationship. Within aggregation, the lifetime of the part is not managed by the whole. To make this clearer, we need an example. For the past 12+ months I have been involved with the implementation of a CRM system, so I am going to use part of this as an example.

The CRM system has a database of customers and a separate database that holds all addresses within a geographic area. Aggregation would make sense in this situation, as a Customer 'has-a' Address. It wouldn't make sense to say that an Address is 'part-of' the Customer, because it isn't. Consider it this way, if the customer ceases to exist, does the address? I would argue that it does not cease to exist. Aggregation is shown on a UML diagram as an unfilled diamond (see Figure 2).

Figure 2 - Aggregation

So how do we express the concept of aggregation in C#? Well, it's a little different to composition. Consider the following code:

public class Address
{
. . .
}

public class Person

{

private Address address;

public Person(Address address)

{

this.address = address;

}

. . .

}

Person would then be used as follows:

Address address = new Address();
Person person = new Person(address);

or

Person person = new Person( new Address() );

As you can see, Person does not manage the lifetime of Address. If Person is destroyed, the Address still exists. This scenario does map quite nicely to the real world.

Difference between abstract class and interface

There is no difference in the functionality of these two.The only difference is that a class cannot extend an abstract class if it already is extending some other class.An interface on the other hand can be implemented in any situation which makes them very powerful.Also user defined exceptions can be defined within an interface itself, which is not the case with an abstract class.
Moreover..
1. Abstract classes can have concrete method while interfaces have no methods implemented.
2. Interface do not come in inheriting chain, while abstract class come in inheritance.
3. Abstract classes are faster than interfaces.
4. By default, all members in Interface is Public whereas members in Abstract class may have different.