Monday, March 10, 2014

Understanding Inheritance and Different Types of Inheritance

nheritance is a mechanism of acquiring the features and behaviors of a class by another class. The class whose members are inherited is called the base class, and the class that inherits those members is called the derived class. Inheritance implements the IS-A relationship.
For example, mammal IS-A animal, dog IS-A mammal; Hence dog IS-A animal as well.

Advantages

  1. Reduce code redundancy.
  2. Provides code reusability.
  3. Reduces source code size and improves code readability.
  4. Code is easy to manage and divided into parent and child classes.
  5. Supports code extensibility by overriding the base class functionality within child classes.

Disadvantages

  1. In Inheritance base class and child classes are tightly coupled. Hence If you change the code of parent class, it will get affects to the all the child classes.
  2. In class hierarchy many data members remain unused and the memory allocated to them is not utilized. Hence affect performance of your program if you have not implemented inheritance correctly.

Different Types of Inheritance

OOPs supports the six types of inheritance as given below-
  1. Single inheritance

    In this inheritance, a derived class is created from a single base class.
    1. //Base Class
    2. class A
    3. {
    4. public void fooA()
    5. {
    6. //TO DO:
    7. }
    8. }
    9.  
    10. //Derived Class
    11. class B : A
    12. {
    13. public void fooB()
    14. {
    15. //TO DO:
    16. }
    17. }
  2. Multi-level inheritance

    In this inheritance, a derived class is created from another derived class.
    1. //Base Class
    2. class A
    3. {
    4. public void fooA()
    5. {
    6. //TO DO:
    7. }
    8. }
    9.  
    10. //Derived Class
    11. class B : A
    12. {
    13. public void fooB()
    14. {
    15. //TO DO:
    16. }
    17. }
    18.  
    19. //Derived Class
    20. class C : B
    21. {
    22. public void fooC()
    23. {
    24. //TO DO:
    25. }
    26. }
  3. Multiple inheritance

    In this inheritance, a derived class is created from more than one base class. This inheritance is not supported by .NET Languages like C#, F# etc.
    1. //Base Class
    2. class A
    3. {
    4. public void fooA()
    5. {
    6. //TO DO:
    7. }
    8. }
    9.  
    10. //Base Class
    11. class B
    12. {
    13. public void fooB()
    14. {
    15. //TO DO:
    16. }
    17. }
    18.  
    19. //Derived Class
    20. class C : A, B
    21. {
    22. public void fooC()
    23. {
    24. //TO DO:
    25. }
    26. }
  4. Multipath inheritance

    In this inheritance, a derived class is created from another derived classes and the same base class of another derived classes. This inheritance is not supported by .NET Languages like C#, F# etc.
    1. //Base Class
    2. class A
    3. {
    4. public void fooA()
    5. {
    6. //TO DO:
    7. }
    8. }
    9.  
    10. //Derived Class
    11. class B : A
    12. {
    13. public void fooB()
    14. {
    15. //TO DO:
    16. }
    17. }
    18.  
    19. //Derived Class
    20. class C : A
    21. {
    22. public void fooC()
    23. {
    24. //TO DO:
    25. }
    26. }
    27.  
    28. //Derived Class
    29. class D : B, A, C
    30. {
    31. public void fooD()
    32. {
    33. //TO DO:
    34. }
    35. }
  5. Hierarchical inheritance

    In this inheritance, more than one derived classes are created from a single base.
    1. //Base Class
    2. class A
    3. {
    4. public void fooA()
    5.  
    6. {
    7. //TO DO:
    8. }
    9. }
    10.  
    11. //Derived Class
    12. class B : A
    13. {
    14. public void fooB()
    15. {
    16. //TO DO:
    17. }
    18. }
    19.  
    20. //Derived Class
    21. class C : A
    22. {
    23. public void fooC()
    24. {
    25. //TO DO:
    26. }
    27. }
    28.  
    29. //Derived Class
    30. class D : C
    31. {
    32. public void fooD()
    33. {
    34. //TO DO:
    35. }
    36. }
    37.  
    38. //Derived Class
    39. class E : C
    40. {
    41. public void fooE()
    42. {
    43. //TO DO:
    44. }
    45. }
    46.  
    47. //Derived Class
    48. class F : B
    49. {
    50. public void fooF()
    51. {
    52. //TO DO:
    53. }
    54. }
    55.  
    56. //Derived Class
    57. class G :B
    58. {
    59. public void fooG()
    60. {
    61. //TO DO:
    62. }
    63. }
  6. Hybrid inheritance

    This is combination of more than one inheritance. Hence, it may be a combination of Multilevel and Multiple inheritance or Hierarchical and Multilevel inheritance or Hierarchical and Multipath inheritance or Hierarchical, Multilevel and Multiple inheritance.
    Since .NET Languages like C#, F# etc. does not support multiple and multipath inheritance. Hence hybrid inheritance with a combination of multiple or multipath inheritance is not supported by .NET Languages.
    1. //Base Class
    2. class A
    3. {
    4. public void fooA()
    5. {
    6. //TO DO:
    7. }
    8. }
    9.  
    10. //Base Class
    11. class F
    12. {
    13. public void fooF()
    14. {
    15. //TO DO:
    16. }
    17. }
    18.  
    19. //Derived Class
    20. class B : A, F
    21. {
    22. public void fooB()
    23. {
    24. //TO DO:
    25. }
    26. }
    27.  
    28. //Derived Class
    29. class C : A
    30. {
    31. public void fooC()
    32. {
    33. //TO DO:
    34. }
    35. }
    36.  
    37. //Derived Class
    38. class D : C
    39. {
    40. public void fooD()
    41. {
    42. //TO DO:
    43. }
    44. }
    45.  
    46. //Derived Class
    47. class E : C
    48. {
    49. public void fooE()
    50. {
    51. //TO DO:
    52. }
    53. }

Disable cut, copy and paste in textbox using jquery, javascript

Due to some reasons (like don’t allow to copy Email from Email TextBox to Confirm Email TextBox), we restrict users to copy, paste and cut contents from TextBox by using CTRL+C, CTRL+V and CTRL+X. We can implement this functionality by using below methods.

Disable cut, copy & paste using Javascript

  1. When we don't want to display any message on cut, copy & paste


    1. <asp:TextBox ID="TextBox1" runat="server" oncopy="return false" 
     onpaste="return false" oncut="return false"></asp:TextBox>
  2. When we want to display an alert message on copy, paste and cut


    1. <script language="javascript" type="text/javascript">
    2. function DisableCopyPaste (e)
    3. {
    4. // Message to display
    5. var message = "Cntrl key/ Right Click Option disabled";
    6. // check mouse right click or Ctrl key press
    7. var kCode = event.keyCode || e.charCode;
    8. //FF and Safari use e.charCode, while IE use e.keyCode
    9. if (kCode == 17 || kCode == 2)
    10. {
    11. alert(message);
    12. return false;
    13. }
    14. }
    15. </script>
    16. <asp:TextBox ID="TextBox1" runat="server" 
      onKeyDown="return DisableCopyPaste(event)" 
           onMouseDown="return DisableCopyPaste (event)"></asp:TextBox> 

Disable cut, copy & paste using JQuery

  1. <script type="text/javascript">
  2. $(document).ready(function() {
  3. $('#TextBox1').bind('copy paste cut',function(e) {
  4. e.preventDefault(); //disable cut,copy,paste
  5. alert('cut,copy & paste options are disabled !!');
  6. });
  7. });
  8. </script>
  9. <asp:TextBox ID="TextBox1" runat="server" ></asp:TextBox>

Get file size before upload using jquery

 
 File uploading functionality is generally used by the developers. Before 
uploading file on the server, to know the size of file is a good 
practice. By knowing file size, we can restrict the end user to upload 
large size files on the server since we have limited space on the 
server. We should check uploaded file size on server side as well client
 side. In this article I am going to expose how you can get size of file
 before uploading on client side using JQuery.
 
 
 
  1. <html xmlns="http://www.w3.org/1999/xhtml">
  2. <head>
  3. <title>Get File Size</title>
  4. <script src="Scripts/jquery-1.7.1.min.js" type="text/javascript" > </script>
  5. <script type="text/javascript">
  6. function GetFileSize(fileid) {
  7. try {
  8. var fileSize = 0;
  9. //for IE
  10. if ($.browser.msie) {
  11. //before making an object of ActiveXObject,
  12. //please make sure ActiveX is enabled in your IE browser
  13. var objFSO = new ActiveXObject("Scripting.FileSystemObject"); var filePath = $("#" + fileid)[0].value;
  14. var objFile = objFSO.getFile(filePath);
  15. var fileSize = objFile.size; //size in kb
  16. fileSize = fileSize / 1048576; //size in mb
  17. }
  18. //for FF, Safari, Opeara and Others
  19. else {
  20. fileSize = $("#" + fileid)[0].files[0].size //size in kb
  21. fileSize = fileSize / 1048576; //size in mb
  22. }
  23. alert("Uploaded File Size is" + fileSize + "MB");
  24. }
  25. catch (e) {
  26. alert("Error is :" + e);
  27. }
  28. }
  29. </script>
  30. </head>
  31. <body>
  32. <form name="upload" action="">
  33. <input type="file" name="fUpload" id="fUpload" />
  34. <input type="button" value="Get File Size" onclick="GetFileSize('fUpload');" />
  35. </form>
  36. </body>
  37. </html> 

Example to get file size before upload in Asp.Net using JQuery

Using above defined function "GetFileSize", we can also get file size in Asp.net also like as :
  1. <form id="form1" runat="server">
  2. <asp:FileUpload ID="fUpload" runat="server" />
  3. <asp:Button ID="btnGetSize" runat="server" Text="Button" OnClientClick="GetFileSize('fUpload');" />
  4. </form>
Above example will work on all browsers including IE, FF, Chrome, Safari, Opera etc.
 

Convert SharePoint Foundation 2010 Web applications that use classic-mode authentication to use claims-based authentication using Powershell

Convert SharePoint Foundation 2010 Web applications that use classic-mode authentication to use claims-based authentication

Perform the steps in the following procedure to use Windows PowerShell to convert existing Web applications to claims-based authentication.

To convert Web applications to claims-based authentication

  1. Verify that you meet the following minimum requirements
  2. On the Start menu, click All Programs.
  3. Click Microsoft SharePoint 2010 Products.
  4. Click SharePoint 2010 Management Shell.
  5. From the Windows PowerShell command prompt, type the following:
    $WebAppName = "http:// yourWebAppUrl"
    $account = "yourDomain\yourUser"
    $wa = get-SPWebApplication $WebAppName
    
    Set-SPwebApplication $wa -AuthenticationProvider (New-SPAuthenticationProvider) -Zone Default
    
    
  6. At the Migration prompt, click Yes to continue.
  7. From the Windows PowerShell command prompt, type the following to set the user as an administrator for the site:
    $account = "yourDomain\yourUser"
    $account = (New-SPClaimsPrincipal -identity $account -identitytype 1).ToEncodedString()
    $zp = $wa.ZonePolicies("Default")
    $p = $zp.Add($account,"PSPolicy")
    $fc=$wa.PolicyRoles.GetSpecialRole("FullControl")
    $p.PolicyRoleBindings.Add($fc)
    $wa.Update()
    
    
  8. From the Windows PowerShell command prompt, type the following to configure the policy to enable the user to have full access:
    $zp = $wa.ZonePolicies("Default")
    $p = $zp.Add($account,"PSPolicy")
    $fc=$wa.PolicyRoles.GetSpecialRole("FullControl")
    $p.PolicyRoleBindings.Add($fc)
    $wa.Update()
    
  9. From the Windows PowerShell command prompt, type the following to perform user migration:
    $wa = get-SPWebApplication $WebAppName
    $wa.MigrateUsers($true)
    
note Note:
We recommend that you use Windows PowerShell when performing command-line administrative tasks. The Stsadm command-line tool has been deprecated, but is included to support compatibility with previous product versions.

Additional migration guidelines

After you perform the steps in the previous procedures, you might experience one or more of the following issues.
  • Users who submit valid credentials might be notified that they do not have permissions. If this occurs, the portalsuperuseraccount property and the portalsuperreaderaccount property of the Web application were probably configured prior to migration. If this is the case, you must update the portalsuperuseraccount property and the portalsuperreaderaccount property to use the new claims-based account name. After migration, you can find the new claims-based account name in the Web application policy for the migrated Web application.
  • If existing alerts are not invoked after migration, you might have to delete and recreate the alerts.
  • If Search crawl does not function after migration, make sure the Search crawl account lists the new converted account name. If the new converted account name is not listed, you must manually create a new policy for the crawl account.

How to Create a Read Only Farm or Site in SharePoint 2010

In SharePoint 2010 you can create a Read-Only Farm/site or site-collection by setting the Content database as Read-only. In a read-only farm, only content databases are read-only. All other databases, including the configuration database, Central Administration content database, and search database, are read/write. The site collection that is associated with a read-only content database is automatically set to be read-only.

The farm is considered to be read-only if any of the following are true:
  • All content databases are set to read-only.
  • Service application databases are set to read-only.

Use of read-only databases

If you plan to provide users with access to a read-only site or farm, you should set expectations for what they will be able to do on the site and how the user interface will differ.

Sites that use read-only content databases

The user experience of a site that uses a content database that is set to read-only is characterized by the following:
  • Common tasks that do not require writing to the content database are fully available.
  • Most of the common tasks that require writing to the content database are not available, either because they have been disabled in the UI, or because the user is no longer allowed to apply changes.
  • Some common tasks that require writing to the content database appear to be available, but return errors.

Farms that use read-only service application databases

The user experience of a farm that uses service application databases that are set to read-only is characterized by the following:
  • Common tasks that do not require writing to the service databases are fully available.
  • All common tasks that require writing to the service databases appear to be available, but return errors.

How to find the content databases to set read only

Before you set content databases to be read-only, you may need to determine which content database is associated with a particular site collection. To determine which content database is associated with a site collection
  1. On the Start menu, click All Programs.
  2. Click Microsoft SharePoint 2010 Products.
  3. Click SharePoint 2010 Management Shell.
  4. At the Windows PowerShell command prompt (PS C:\>), type the following command, and then press ENTER:
  5. Get-SPContentDatabase -Site <Site URL>

    -Site specifies the site collection for which you want to know the associated content database.

    The command returns the content database associated with the site.

To set content databases to be read-only
  1. Verify that you have the following administrative credentials: You must be a member of the db_owner fixed database role in each database.
  2. Open SQL Server Management Studio.
  3. Right-click the content database that you want to change to read-only, and then click Properties.
  4. Select the Options page, and, in the Other options list, scroll to the State section.
  5. In the Database Read-Only row, click the arrow next to False, select True, and then click OK.
  6. Repeat for all other content databases.

Set service application databases to be read-only

It is possible to set any service application database to be read-only. However, some service applications do not function when their databases are set to read-only, including those associated with Search and Project Server.
  1. To set service application databases to be read-only
    Verify that you have the following administrative credentials: You must be a member of the db_owner fixed database role in each database.
  2. Open SQL Server Management Studio.
  3. Right-click the database that you want to change to read-only, and then click Properties.
  4. Select the Options page, and, in the Other options list, scroll to the State section.
  5. In the Database Read-Only row, click the arrow next to False, select True, and then click OK.
  6. Repeat for other service application databases as appropriate.