Saturday, 17 September 2011

When to Use Inheritance

       Inheritance is a good choice when: 


  • Your inheritance hierarchy represents an "is-a" relationship and not a "has-a" relationship.
  • You can reuse code from the base classes.
  • You need to apply the same class and methods to different data types.
  • The class hierarchy is reasonably shallow, and other developers are not likely to add many more levels.
  • You want to make global changes to derived classes by changing a base class.

    Inheritance and "Is a" Relationships

    Two ways to show class relationships in object-oriented programming are "is a" and "has a" relationships. In an "is a" relationship, the derived class is clearly a kind of the base class. For example, a class named PremierCustomer represents an "is a" relationship with a base class named Customer because a premier customer is a customer. However, a class named CustomerReferral represents a "has a" relationship with the Customer class because a customer referral has a customer, but a customer referral is not a kind of customer.
    Objects in an inheritance hierarchy should have an "is a" relationship with their base class because they inherit the fields, properties, methods, and events defined in the base class. Classes that represent a "has a" relationship with other classes are not suited to inheritance hierarchies because they may inherit inappropriate properties and methods. For example, if the CustomerReferral class were derived from the Customer class discussed previously, it might inherit properties that make no sense, such as ShippingPrefs and LastOrderPlaced. "Has a" relationships such as this should be represented using unrelated classes or interfaces. The following illustration shows examples of both "is a" and "has a" relationships.

    Base Classess and code Reuse

    Another reason to use inheritance is the advantage of code reuse. Well-designed classes can be debugged once and used over and over as a basis for new classes.
    A common example of effective code reuse is in connection with libraries that manage data structures. Suppose, for example, that you have a large business application that manages several kinds of in-memory lists. One is an in-memory copy of your customer database, read in from a database at the beginning of the session for speed. The data structure might look something like the following:

    Global Changes to Derived Classes Through the Base Class 

    One of the most powerful features of inheritance is the ability to make changes in a base class that propagate to derived classes. When used carefully, you can update the implementation of a single method, and dozens—or even hundreds—of derived classes can use the new code. However, this can be a dangerous practice because such changes may cause problems with inherited classes designed by other people. Care must be taken to ensure that the new base class is compatible with classes that use the original. You should specifically avoid changing the name or type of base class members.





Tuesday, 13 September 2011

how to rename file when upload (change file name when upload)

  asp.net FileUpload example: how to rename file when upload (change file name when upload) 
string extension = Path.GetExtension(FileUpload1.PostedFile.FileName);
FileUpload1.SaveAs(@"C:\Test\" + FileUpload1.FileName.Replace(FileUpload1.FileName, "Mandar" + extension));

Tip : To use Path.GetExtension() method ,Add namespace System.IO namespace at the top

Thursday, 8 September 2011

how to know about modified Stored procedure in database

How to see modified SP's in Database :

For SQL Server 2005 and 2008

SELECT
        [name]
       ,create_date
       ,modify_date
FROM
        sys.objects
order by modify_date Desc

For SQL Server 2000

SELECT
        [name]
       ,create_date
       ,modify_date
FROM
        sysobjects
order by modify_date Desc

SQL Script: Search all tables for a specific field

To Search specific column from all tables of databases, use following Query

USE database_name
GO
SELECT t.name AS table_name,
SCHEMA_NAME(schema_id) AS schema_name,
c.name AS column_name
FROM sys.tables AS t
INNER JOIN sys.columns c ON t.OBJECT_ID = c.OBJECT_ID
WHERE c.name LIKE '%SupplierID%'
ORDER BY schema_name, table_name;