student Box Office

Learn more from here.

student Box Office

We will prepare the best articles for you to acquire knowledge.

student Box Office

We will help you always. Just follow our guidelines.

student Box Office

Come and join here to share the knowledge in latest Technologies.

student Box Office

Share your views with us. And update your knowledge.

May 30, 2012

Access Specifiers Or Access Modifiers in C#


In encapsulation, before creating any types you must always consider scope of the type or visibility of the type.  C# provides various number of keywords to decide the scope, those are private(default one), public, internal, protected and protected internal. If you create any type without these keywords, by default it will take as private.

Here we will discuss about each access specifier with an example. I created Class1.cs class which has several methods with the different access modifiers as shown below.

namespace AccessSpecifiers
{
    public class Class1
    {
        public void display1()
        {
            Console.WriteLine("public Access Specifier");
        }

        private void display2()
        {
            Console.WriteLine("Private Access Specifier");
        

        internal void display3()
        {
            Console.WriteLine("internal Access Specifier");
        

        protected void display4()
        {
            Console.WriteLine("protected Access Specifier");
        

        protected internal void display5()
        {
            Console.WriteLine("protected internal Access Specifier");
        }
  }
}

private Access Specifier or Modifier:

The scope of the private members is limited with in the Class or Structure in which they are defined. They are not accessible outside the Class or Structure.

In the attached example, we have display2() method which is defined as private in Access Specifiers project, Class1.cs class.

We tried to access this method in the Class2.cs class and Class3.cs class (which is derived from Class1.cs class). But it produces the error as AccessSpecifiers.Class1.display2() is inaccessible due to its protection level.

class Class2
{
        void display()
        {
            Class1 obj = new Class1();
            obj.display2(); //Error, because display2() is private
        }
}


class Class3:Class1
{
        void display()
        {
            display2(); //Error, because display2() is private
        }
}


public Access Specifier or Modifier:

Public members have no access restriction. You can access public members by creating object, in derived class and with in the assembly and in other assemblies also.

We have display1() method which is defined as public in AccessSpecifiers project, Class1.cs class.  We can access this method in Class2.cs class by creating object, in Class3.cs class which is derived from Class1.cs class and also in other assembly AccessSpecifiersTest also.

namespace AccessSpecifiers
{
    class Class2
    {
        void display()
        {
            Class1 obj = new Class1();
            obj.display1(); //No Error, because display1() public
        }
    }
}

namespace AccessSpecifiers
{
    class Class3:Class1
    {
        void display()
        {
            display1(); //No Error, because display1() public
        }
    }
}

In other assembly AccessSpecifiersTest,

namespace AccessSpecifiersTest
{
    class Program
    {
        static void Main(string[] args)
        {
            //try to access AccessSpecifier Assembly methods
            AccessSpecifiers.Class1 obj = new AccessSpecifiers.Class1();
            obj.display1(); //public method, No Error   
         }
    }
}


protected Access Specifier or Modifier:

You cannot access protected members directly i.e., you cannot access protected members by creating object. You can access protected memebers in derived class only.

We have display4() method which is defined as protected in AccessSpecifiers project, Class1.cs class.  We can access this method in Class3.cs class which is derived from Class1.cs class and elsewhere you cannot access this method.

namespace AccessSpecifiers
{
    class Class2
    {
        void display()
        {
            Class1 obj = new Class1();
            obj.display4(); //Error, because display4() is protected
        }
    }
}

namespace AccessSpecifiers
{
    class Class3:Class1
    {
        void display()
        {
            display4(); //No Error, because display4() is protected
        }
    }
}

namespace AccessSpecifiersTest
{
    class Program
    {
        static void Main(string[] args)
        {
            //try to access AccessSpecifier Assembly methods
            AccessSpecifiers.Class1 obj = new AccessSpecifiers.Class1();
            obj.display4();//Error, because display4() is protected
        }
    }
}


internal Access Specifier or Modifier:

Internal members are accessible with in the assembly only. You cannot access internal members outside of the assembly.

We have display3() method which is defined as internal in AccessSpecifiers project, Class1.cs class.  We can access this method in Class2.cs class by creating object, in Class3.cs class which is derived from Class1.cs class. Because Class2.cs and Class3.cs classes are in the same assembly AccessSpecifiers. You cannot access display3() method other assembly AccessSpecifiersTest, it produces compile time error.

namespace AccessSpecifiers
{
    class Class2
    {
        void display()
        {
            Class1 obj = new Class1();
            obj.display3(); //No Error, because display3() is internal
         }
    }
}

namespace AccessSpecifiers
{
    class Class3:Class1
    {
        void display()
        {
            display3(); //No Error, because display3() is internal
        }
    }
}


namespace AccessSpecifiersTest
{
    class Program
    {
        static void Main(string[] args)
        {
            //try to access AccessSpecifier Assembly methods
            AccessSpecifiers.Class1 obj = new AccessSpecifiers.Class1();
            obj.display3();//Error, because display3() is internal
        }
    }
}


protected internal Access Specifier or Modifier:

Protected internal scope is addition of protected and internal scope. That means protected internal members are accessible in the derived class as well as with in the assembly.

We have display5() method which is defined as protected internal in AccessSpecifiers project, Class1.cs class.  We can access this method in Class2.cs class by creating object, in Class3.cs class which is derived from Class1.cs class, but we cannot access in other assembly AccessSpecifiersTest.

namespace AccessSpecifiers
{
    class Class2
    {
        void display()
        {
            Class1 obj = new Class1();
            obj.display5(); //No Error, because display5() is protected                       internal
        }
    }
}

namespace AccessSpecifiers
{
    class Class3:Class1
    {
        void display()
        {
            display5(); //No Error, because display5() is protected internal
        }
    }
}

namespace AccessSpecifiersTest
{
    class Program
    {
        static void Main(string[] args)
        {
            //try to access AccessSpecifier Assembly methods
            AccessSpecifiers.Class1 obj = new AccessSpecifiers.Class1();
            obj.display5();//Error, because display5() is protected internal
        }
    }
}

May 27, 2012

Windows 8 makes it easier for parents to track kids‘ internet usage


Image

Microsoft's Family Safety tool helps parents limit site usage

 Microsoft's Windows 8 will include a brand new suite of tools to help parents keep track of what their kids are doing on the internet.
The Windows 8 Family Safety toolset will send weekly usage reports about the sites that have been visited, and will allow parents to block specific websites and apps, set time limits for usage, lock search engines into strict SafeSearch, and track Windows Store purchases.
All of this can be done through a parent's administrator account, so the kids don't have access to the information and tools.
The reports aren't limited to a single device, either, instead collecting information on websites visited, time spent on each website, search histories, and apps and games usage from every device a child uses his or her Windows account on.The administrator is meant to set up separate accounts for each child so they can keep track of exactly who is doing what.

Microsoft has some parenting advice

"Computers give children access to many positive experiences; however, parents face challenges in monitoring what their children see online, the people they meet, and the information they share," said Phil Sohn, Microsoft's senior program manager for family safety, in a blog post.
"Parenting techniques like this are important, but they may be difficult to employ if your household has multiple PCs or if your kids use laptops and tablets. And glancing over a teenager's shoulder can be awkward for both parents and kids," he continued.
Sohn said the purpose of using individual Windows accounts for each family member isn't solely to monitor what they're doing online.
"It is also a great way for each family member to maintain their own unique online identity while still sharing a single PC," he said.
He believes that the simplicity of the new Windows 8 Family Safety tools will cause more parents to readily use them.

May 21, 2012

No Java Required: Write Android Apps in C#


No Java Required: Write Android Apps in C#

XobotOS is a Xamarin-developed project that translated millions of lines of Java to C#.
Java is the underlying code for the Android OS. But one company has changed all that, ripping out most of the Java and replacing it with C#.
That means .NET developers can code in a familiar language and produce apps that leverage C#'s advantages, including speed increases.
It started as a skunkworks project for Xamarin. Xamarin's claim to fame is Mono, an open-source framework allowing Android and iOS apps to be built using C# running on top of Java. Now, with what the company calls the XobotOS Research Project, the Java layer has been removed via a "machine translation of Android from Java to C#," according to ablog post from Xamarin CTO Miguel de Icaza.

Building XobotOS involved converting more than a million lines of Java code into C#, de Icaza wrote. A tool called Sharpen was used to help in the translation, and the project resulted in an improved version of Sharpen, de Icaza says. Most of Androids layouts and controls, de Icaza says, have been converted, and to demonstrate, the post includes a Java-free screenshot of  XobotOS running on a Linux desktop.
In terms of speed improvements, the blog includes a bar chart of a benchmark from a simple binary tree implementation in Java and C#. The chart shows a huge increase in performance between the two languages.
Xamarin has made XobotOS available on github. de Icaza says that XobotOS won't be "a focus" going forward, as the company wants to put its efforts toward its core products, Mono for Android and MonoTouch.
One other possible benefit of using XobotOS for Android development is that it would eliminate the kind of potential legal entanglements that Google finds itself in with Oracle, the patent-holder for Java. Replacing Java with C# would, of course, inoculate developers from the clutches of Oracle's lawyers.

About the Author
 
Keith Ward is the editor in chief of Visual Studio Magazine. 

May 20, 2012

Ajax Control Toolkit May 2012 Release

Ajax Control Toolkit May 2012 Release

I’m happy to announce the May 2012 release of the Ajax Control Toolkit. This newest release of the Ajax Control Toolkit includes a new file upload control which displays file upload progress. We’ve also added several significant enhancements to the existing HtmlEditorExtender control such as support for uploading images and Source View.
You can download and start using the newest version of the Ajax Control Toolkit by entering the following command in the Library Package Manager console in Visual Studio:
Install-Package AjaxControlToolkit
Alternatively, you can download the latest version of the Ajax Control Toolkit from CodePlex:

The New Ajax File Upload Control

The most requested new feature for the Ajax Control Toolkit (according to the CodePlex Issue Tracker) has been support for file upload with progress. We worked hard over the last few months to create an entirely new file upload control which displays upload progress.
clip_image002
Here is a sample which illustrates how you can use the new AjaxFileUpload control:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="01_FileUpload.aspx.cs" Inherits="WebApplication1._01_FileUpload" %>
<html>
<head runat="server">
    <title>Simple File Upload</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
 
    <ajaxToolkit:ToolkitScriptManager runat="server" />
 
    <ajaxToolkit:AjaxFileUpload
        id="ajaxUpload1"
        OnUploadComplete="ajaxUpload1_OnUploadComplete"
        runat="server"  />
 
    </div>
    </form>
</body>
</html>
The page above includes a ToolkitScriptManager control. This control is required to use any of the controls in the Ajax Control Toolkit because this control is responsible for loading all of the scripts required by a control.
The page also contains an AjaxFileUpload control. The UploadComplete event is handled in the code-behind for the page:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
namespace WebApplication1
{
    public partial class _01_FileUpload : System.Web.UI.Page
    {
        protected void ajaxUpload1_OnUploadComplete(object sender, AjaxControlToolkit.AjaxFileUploadEventArgs e)
        {
            // Generate file path
            string filePath = "~/Images/" + e.FileName;
 
            // Save upload file to the file system
            ajaxUpload1.SaveAs(MapPath(filePath));
        }
    }
}
The UploadComplete handler saves each uploaded file by calling the AjaxFileUpload control’s SaveAs() method with a full file path.
Here’s a video which illustrates the process of uploading a file:
clip_image003
Warning: in order to write to the Images folder on a production IIS server, you need Write permissions on the Images folder. You need to provide permissions for the IIS Application Pool account to write to the Images folder. To learn more, see:

Showing File Upload Progress

The new AjaxFileUpload control takes advantage of HTML5 upload progress events (described in the XMLHttpRequest Level 2 standard). This standard is supported by Firefox 8+, Chrome 16+, Safari 5+, and Internet Explorer 10+. In other words, the standard is supported by the most recent versions of all browsers except for Internet Explorer which will support the standard with the release of Internet Explorer 10.
The AjaxFileUpload control works with all browsers, even browsers which do not support the new XMLHttpRequest Level 2 standard. If you use the AjaxFileUpload control with a downlevel browser – such as Internet Explorer 9 — then you get a simple throbber image during a file upload instead of a progress indicator.
Here’s how you specify a throbber image when declaring the AjaxFileUpload control:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="02_FileUpload.aspx.cs" Inherits="WebApplication1._02_FileUpload" %>
<html>
<head id="Head1" runat="server">
    <title>File Upload with Throbber</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
 
    <ajaxToolkit:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server" />
 
    <ajaxToolkit:AjaxFileUpload
        id="ajaxUpload1"
        OnUploadComplete="ajaxUpload1_OnUploadComplete"
        ThrobberID="MyThrobber"
        runat="server"  />
 
        <asp:Image
            id="MyThrobber"
            ImageUrl="ajax-loader.gif"
            Style="display:None"
            runat="server" />
 
    </div>
    </form>
</body>
</html>
Notice that the page above includes an image with the Id MyThrobber. This image is displayed while files are being uploaded.
I use the website http://AjaxLoad.info to generate animated busy wait images.

Drag-And-Drop File Upload

If you are using an uplevel browser then you can drag-and-drop the files which you want to upload onto the AjaxFileUpload control. The following video illustrates how drag-and-drop works:
clip_image004
Remember that drag-and-drop will not work on Internet Explorer 9 or older.

Accepting Multiple Files

By default, the AjaxFileUpload control enables you to upload multiple files at a time. When you open the file dialog, use the CTRL or SHIFT key to select multiple files.
clip_image005
If you want to restrict the number of files that can be uploaded then use the MaximumNumberOfFiles property like this:
1
2
3
4
5
6
<ajaxToolkit:AjaxFileUpload
    id="ajaxUpload1"
    OnUploadComplete="ajaxUpload1_OnUploadComplete"
    ThrobberID="throbber"
    MaximumNumberOfFiles="1"
    runat="server"  />
In the code above, the maximum number of files which can be uploaded is restricted to a single file.

Restricting Uploaded File Types

You might want to allow only certain types of files to be uploaded. For example, you might want to accept only image uploads. In that case, you can use the AllowedFileTypes property to provide a list of allowed file types like this:
1
2
3
4
5
6
<ajaxToolkit:AjaxFileUpload
    id="ajaxUpload1"
    OnUploadComplete="ajaxUpload1_OnUploadComplete"
    ThrobberID="throbber"
    AllowedFileTypes="jpg,jpeg,gif,png"
    runat="server"  />
The code above prevents any files except jpeg, gif, and png files from being uploaded.

Enhancements to the HTMLEditorExtender

Over the past months, we spent a considerable amount of time making bug fixes and feature enhancements to the existing HtmlEditorExtender control. I want to focus on two of the most significant enhancements that we made to the control: support for Source View and support for uploading images.

Adding Source View Support to the HtmlEditorExtender

When you click the Source View tag, the HtmlEditorExtender changes modes and displays the HTML source of the contents contained in the TextBox being extended. You can use Source View to make fine-grain changes to HTML before submitting the HTML to the server.
For reasons of backwards compatibility, the Source View tab is disabled by default. To enable Source View, you need to declare your HtmlEditorExtender with the DisplaySourceTab property like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="05_SourceView.aspx.cs" Inherits="WebApplication1._05_SourceView" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head id="Head1" runat="server">
    <title>HtmlEditorExtender with Source View</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
 
    <ajaxToolkit:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server" />
 
    <asp:TextBox
        id="txtComments"
        TextMode="MultiLine"
        Columns="60"
        Rows="10"
        Runat="server" />
 
    <ajaxToolkit:HtmlEditorExtender
        id="HEE1"
        TargetControlID="txtComments"
        DisplaySourceTab="true"
        runat="server"  />
 
    </div>
    </form>
</body>
</html>
The page above includes a ToolkitScriptManager, TextBox, and HtmlEditorExtender control. The HtmlEditorExtender extends the TextBox so that it supports rich text editing.
Notice that the HtmlEditorExtender includes a DisplaySourceTab property. This property causes a button to appear at the bottom of the HtmlEditorExtender which enables you to switch to Source View:
clip_image006
Note: when using the HtmlEditorExtender, we recommend that you set the DOCTYPE for the document. Otherwise, you can encounter weird formatting issues.

Accepting Image Uploads

We also enhanced the HtmlEditorExtender to support image uploads (another very highly requested feature at CodePlex). The following video illustrates the experience of adding an image to the editor:
clip_image007
Once again, for backwards compatibility reasons, support for image uploads is disabled by default. Here’s how you can declare the HtmlEditorExtender so that it supports image uploads:
1
2
3
4
5
6
7
8
9
10
11
12
13
<ajaxToolkit:HtmlEditorExtender
    id="MyHtmlEditorExtender"
    TargetControlID="txtComments"
    OnImageUploadComplete="MyHtmlEditorExtender_ImageUploadComplete"
    DisplaySourceTab="true"
    runat="server" >
    <Toolbar>
        <ajaxToolkit:Bold />
        <ajaxToolkit:Italic />
        <ajaxToolkit:Underline />
        <ajaxToolkit:InsertImage />
    </Toolbar>
</ajaxToolkit:HtmlEditorExtender>
There are two things that you should notice about the code above. First, notice that an InsertImage toolbar button is added to the HtmlEditorExtender toolbar. This HtmlEditorExtender will render toolbar buttons for bold, italic, underline, and insert image.
Second, notice that the HtmlEditorExtender includes an event handler for the ImageUploadComplete event. The code for this event handler is below:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
using System.Web.UI;
using AjaxControlToolkit;
 
namespace WebApplication1
{
    public partial class _06_ImageUpload : System.Web.UI.Page
    {
 
        protected void MyHtmlEditorExtender_ImageUploadComplete(object sender, AjaxFileUploadEventArgs e)
        {
            // Generate file path
            string filePath = "~/Images/" + e.FileName;
 
            // Save uploaded file to the file system
            var ajaxFileUpload = (AjaxFileUpload)sender;
            ajaxFileUpload.SaveAs(MapPath(filePath));
 
            // Update client with saved image path
            e.PostedUrl = Page.ResolveUrl(filePath);
        }
    }
}
Within the ImageUploadComplete event handler, you need to do two things:
1) Save the uploaded image (for example, to the file system, a database, or Azure storage)
2) Provide the URL to the saved image so the image can be displayed within the HtmlEditorExtender
In the code above, the uploaded image is saved to the ~/Images folder. The path of the saved image is returned to the client by setting the AjaxFileUploadEventArgs PostedUrl property.
Not surprisingly, under the covers, the HtmlEditorExtender uses the AjaxFileUpload. You can get a direct reference to the AjaxFileUpload control used by an HtmlEditorExtender by using the following code:
1
2
3
4
5
void Page_Load()
{
    var ajaxFileUpload = MyHtmlEditorExtender.AjaxFileUpload;
    ajaxFileUpload.AllowedFileTypes = "jpg,jpeg";
}
The code above illustrates how you can restrict the types of images that can be uploaded to the HtmlEditorExtender. This code prevents anything but jpeg images from being uploaded.

Summary

This was the most difficult release of the Ajax Control Toolkit to date. We iterated through several designs for the AjaxFileUpload control – with each iteration, the goal was to make the AjaxFileUpload control easier for developers to use. My hope is that we were able to create a control which Web Forms developers will find very intuitive.
Article Source: 


Share

Twitter Delicious Facebook Digg Stumbleupon Favorites More