Sunday, 29 October 2017

All Keyboard Shortcuts of Windows 7 at one place.

Windows 7 have lots of keyboard shortcuts which can be used to make our daily task much easier, interactive, and efficiently saves our time & effort. Keyboard shortcut is nothing but just combinations of two or more keys. When the combination is pressed a task is performed which may require a mouse or other pointing device.


In general accelerator keys are used with menus and other commands to make the programs easier. Accelerator keys can be seen in case any letter in a menu is underlined which means if we press ALT key in combination with that underlined key will do the same job as clicking that menu item.
List of keyboard shortcuts of Windows 7 available are given below with their categories:

Ease of Access keyboard shortcuts


General keyboard shortcuts
Dialog box keyboard shortcuts
Windows logo key keyboard shortcuts
Windows Explorer keyboard shortcuts
Taskbar keyboard shortcuts
Magnifier keyboard shortcuts
Remote Desktop Connection keyboard shortcuts
Paint keyboard shortcuts
WordPad keyboard shortcutsCalculator keyboard shortcutsWindows Journal keyboard shortcuts
Windows Help viewer keyboard shortcuts


Tuesday, 17 October 2017

Why 'using namespace std' used in cpp

Lets Start With Scopes

Named entities, such as variables, functions, and compound types need to be declared before being used in C++. The point in the program where this declaration happens influences its visibility:

An entity declared outside any block has global scope, meaning that its name is valid anywhere in the code. While an entity declared within a block, such as a function or a selective statement, has block scope, and is only visible within the specific block in which it is declared, but not outside it.

Variables with block scope are known as local variables.

For example, a variable declared in the body of a function is a local variable that extends until the end of the the function (i.e., until the brace } that closes the function definition), but not outside it:

1
2
3
4
5
6
7
8
9
10
11
12
13
int foo;        // global variable

int some_function ()
{
  int bar;      // local variable
  bar = 0;
}

int other_function ()
{
  foo = 1;  // ok: foo is a global variable
  bar = 2;  // wrong: bar is not visible from this function
}


In each scope, a name can only represent one entity. For example, there cannot be two variables with the same name in the same scope:

1
2
3
4
5
6
7
int some_function ()
{
  int x;
  x = 0;
  double x;   // wrong: name already used in this scope
  x = 0.0;
}


The visibility of an entity with block scope extends until the end of the block, including inner blocks. Nevertheless, an inner block, because it is a different block, can re-utilize a name existing in an outer scope to refer to a different entity; in this case, the name will refer to a different entity only within the inner block, hiding the entity it names outside. While outside it, it will still refer to the original entity. For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// inner block scopes
#include <iostream>
using namespace std;

int main () {
  int x = 10;
  int y = 20;
  {
    int x;   // ok, inner scope.
    x = 50;  // sets value to inner x
    y = 50;  // sets value to (outer) y
    cout << "inner block:\n";
    cout << "x: " << x << '\n';
    cout << "y: " << y << '\n';
  }
  cout << "outer block:\n";
  cout << "x: " << x << '\n';
  cout << "y: " << y << '\n';
  return 0;
}
inner block:
x: 50
y: 50
outer block:
x: 10
y: 50


Note that y is not hidden in the inner block, and thus accessing y still accesses the outer variable.

Variables declared in declarations that introduce a block, such as function parameters and variables declared in loops and conditions (such as those declared on a for or an if) are local to the block they introduce.

Namespaces

Only one entity can exist with a particular name in a particular scope. This is seldom a problem for local names, since blocks tend to be relatively short, and names have particular purposes within them, such as naming a counter variable, an argument, etc...

But non-local names bring more possibilities for name collision, especially considering that libraries may declare many functions, types, and variables, neither of them local in nature, and some of them very generic.

Namespaces allow us to group named entities that otherwise would have global scope into narrower scopes, giving them namespace scope. This allows organizing the elements of programs into different logical scopes referred to by names.

The syntax to declare a namespaces is:


namespace identifier
{
  named_entities
}

Where identifier is any valid identifier and named_entities is the set of variables, types and functions that are included within the namespace. For example:

1
2
3
4
namespace myNamespace
{
  int a, b;
}


In this case, the variables a and b are normal variables declared within a namespace called myNamespace.

These variables can be accessed from within their namespace normally, with their identifier (either a or b), but if accessed from outside the myNamespace namespace they have to be properly qualified with the scope operator ::. For example, to access the previous variables from outside myNamespace they should be qualified like:

1
2
myNamespace::a
myNamespace::b 


Namespaces are particularly useful to avoid name collisions. For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// namespaces
#include <iostream>
using namespace std;

namespace foo
{
  int value() { return 5; }
}

namespace bar
{
  const double pi = 3.1416;
  double value() { return 2*pi; }
}

int main () {
  cout << foo::value() << '\n';
  cout << bar::value() << '\n';
  cout << bar::pi << '\n';
  return 0;
}
5
6.2832
3.1416


In this case, there are two functions with the same name: value. One is defined within the namespace foo, and the other one in bar. No redefinition errors happen thanks to namespaces. Notice also how pi is accessed in an unqualified manner from within namespace bar (just as pi), while it is again accessed in main, but here it needs to be qualified as bar::pi.

Namespaces can be split: Two segments of a code can be declared in the same namespace:

1
2
3
namespace foo { int a; }
namespace bar { int b; }
namespace foo { int c; }


This declares three variables: a and c are in namespace foo, while b is in namespace bar. Namespaces can even extend across different translation units (i.e., across different files of source code).

using

The keyword using introduces a name into the current declarative region (such as a block), thus avoiding the need to qualify the name. For example:

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
// using
#include <iostream>
using namespace std;

namespace first
{
  int x = 5;
  int y = 10;
}

namespace second
{
  double x = 3.1416;
  double y = 2.7183;
}

int main () {
  using first::x;
  using second::y;
  cout << x << '\n';
  cout << y << '\n';
  cout << first::y << '\n';
  cout << second::x << '\n';
  return 0;
}
5
2.7183
10
3.1416


Notice how in main, the variable x (without any name qualifier) refers to first::x, whereas y refers to second::y, just as specified by the using declarations. The variables first::y and second::x can still be accessed, but require fully qualified names.

The keyword using can also be used as a directive to introduce an entire namespace:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// using
#include <iostream>
using namespace std;

namespace first
{
  int x = 5;
  int y = 10;
}

namespace second
{
  double x = 3.1416;
  double y = 2.7183;
}

int main () {
  using namespace first;
  cout << x << '\n';
  cout << y << '\n';
  cout << second::x << '\n';
  cout << second::y << '\n';
  return 0;
}
5
10
3.1416
2.7183


In this case, by declaring that we were using namespace first, all direct uses of x and y without name qualifiers were also looked up in namespace first.

using and using namespace have validity only in the same block in which they are stated or in the entire source code file if they are used directly in the global scope. For example, it would be possible to first use the objects of one namespace and then those of another one by splitting the code in different blocks:

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
// using namespace example
#include <iostream>
using namespace std;

namespace first
{
  int x = 5;
}

namespace second
{
  double x = 3.1416;
}

int main () {
  {
    using namespace first;
    cout << x << '\n';
  }
  {
    using namespace second;
    cout << x << '\n';
  }
  return 0;
}
5
3.1416

Namespace aliasing

Existing namespaces can be aliased with new names, with the following syntax:

namespace new_name = current_name;

The std namespace

All the entities (variables, types, constants, and functions) of the standard C++ library are declared within the stdnamespace. Most examples in these tutorials, in fact, include the following line:

 
using namespace std;


This introduces direct visibility of all the names of the std namespace into the code. This is done in these tutorials to facilitate comprehension and shorten the length of the examples, but many programmers prefer to qualify each of the elements of the standard library used in their programs. For example, instead of:

 
cout << "Hello world!";


It is common to instead see:

 
std::cout << "Hello world!";


Whether the elements in the std namespace are introduced with using declarations or are fully qualified on every use does not change the behavior or efficiency of the resulting program in any way. It is mostly a matter of style preference, although for projects mixing libraries, explicit qualification tends to be preferred.

Storage classes

The storage for variables with global or namespace scope is allocated for the entire duration of the program. This is known as static storage, and it contrasts with the storage for local variables (those declared within a block). These use what is known as automatic storage. The storage for local variables is only available during the block in which they are declared; after that, that same storage may be used for a local variable of some other function, or used otherwise.

But there is another substantial difference between variables with static storage and variables with automatic storage:
- Variables with static storage (such as global variables) that are not explicitly initialized are automatically initialized to zeroes.
- Variables with automatic storage (such as local variables) that are not explicitly initialized are left uninitialized, and thus have an undetermined value.

For example:
1
2
3
4
5
6
7
8
9
10
11
12
13
// static vs automatic storage
#include <iostream>
using namespace std;

int x;

int main ()
{
  int y;
  cout << x << '\n';
  cout << y << '\n';
  return 0;
}
0
4285838


The actual output may vary, but only the value of x is guaranteed to be zero. y can actually contain just about any value (including zero).

Monday, 16 October 2017

Difference Between Response.Redirect() and Server.Transfer() Methods in ASP.NET

 

Introduction

Both Response.Redirect and Server.Transfer methods are used to transfer a user from one web page to another web page. Both methods are used for the same purpose, but still there are some differences as follows.
The Response.Redirect method redirects a request to a new URL and specifies the new URL while the Server.Transfer method for the current request, terminates execution of the current page and starts execution of a new page using the specified URL path of the page.
Both Response.Redirect and Server.Transfer have the same syntax like:
Response.Redirect("UserDetail.aspx");
Server.Transfer("UserDetail.aspx");

HTTP Status Codes

Before touching on more points, I want to explain some HTTP status codes, these are important for the understanding of the basic differences between these two. The HTTP status codes are the codes that the Web server uses to communicate with the Web browser or user agent.
  1. HTTP 200 OK
    This is the most common HTTP status message. It indicates that the request was successful and the server was able to deliver on the request. The information returned with the response is dependent on the method used in the request. In a GET request, the response will contain an entity corresponding to the requested resource. In a POST request, the response will contain an entity describing or containing the result of the action.
  2. HTTP 302 Found
    The HTTP response status code 302 Found is a common way of performing a redirection. The 302 status code indicates that the resource you are requesting has redirected to another resource.

Response.Redirect and Server.Transfer Request Handling

Response.Redirect sends an HTTP request to the browser, then the browser sends that request to the web server, then the web server delivers a response to the web browser. For example, suppose you are on the web page "UserRegister.aspx" page and it has a button that redirects you to the "UserDetail.aspx" web page.
HTTP request to the browser
Figure 1.1 Request and Response using Response.Redirect method
You can notice in Figure 1.1 that when you run the application, then you get a web page successfully so the HTTP status code is "HTTP 200 OK". Then you click on the button that redirects to another page using the Response.Redirect method. The Response.Redirect method first sends a request to the web browser so it is the "HTTP 302 Found" status, then the browser sends a request to the server and the server delivers a response so it is "HTTP 200 OK". It is called a round trip. Let's see that in Figure 1.2.
Round Trip by Response.Redirect method
Figure 1.2 Round Trip by Response.Redirect method.
Server.Transfer sends a request directly to the web server and the web server delivers the response to the browser.
Request and Response using Server.Transfer method
Figure 1.3 Request and Response using Server.Transfer method
Now Figure 1.3 explains that the first request goes to the web server and then gets a response so the round-trip is not made by the Server.Transfer method. You get the same page response but different content. That means your web page URL in the address bar will not be changed. For example, see Figure 1.3 showing you get the same page as the previous request page so the previous page still remains while in Response.Redirect, you get a different page in the response (see Figure 1.1 ). That means the address bar URL will be changed in the Response.Redirect method and also updates the browser history so you can move back from the browser back button.
Server.Transfer method request and response
Figure 1.4 Server.Transfer method request and response
Response.Redirect can be used for both .aspx and HTML pages whereas Server.Transfer can be used only for .aspx pages and is specific to ASP and ASP.NET.
Response.Redirect can be used to redirect a user to an external website. Server.Transfer can be used only on sites running on the same server. You cannot use Server.Transfer to redirect the user to a page running on a different server.

Using the Code

When you use Server.Transfer, then the previous page also exists in server memory while in the Response.Redirect method, the previous page is removed from server memory and loads a new page in memory. Let's see an example.
We add some items in the context of the first page "UserRegister.aspx" and thereafter the user transfers to another page "UserDetail.aspx" using Server.Transfer on a button click. The user will then be able to request data that was in the context because the previous page also exists in memory. Let's see the code.
Code of the UserRegister.aspx.cs page:
using System;
public partial class UserRegister : System.Web.UI.Page
{
    protected void btn_Detail_Click(object sender, EventArgs e)
    {
        Context.Items.Add("Name", "Sandeep Singh Shekhawat");
        Context.Items.Add("Email", "sandeep.shekhawat88@gmail.com");
       Server.Transfer("UserDetail.aspx");
    }
} 
Code of the UserDetail.aspx.cs page:
 using System;
public partial class UserDetail : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            Response.Write(string.Format("My name is {0} and email address is {1}",
                                          Context.Items["Name"].ToString(),
                                          Context.Items["Email"].ToString()));
        }
        catch (NullReferenceException ex)
        {
            Response.Write(ex.Message);
        }
    }
} 
Using the code above, we can get data from the previous page to the new page by the Context.Itemcollection. But if you use the Response.Redirect method, then you can't get context data on another page and get an exception object as null because the previous page doesn't exist in server memory.
Null Exception by Context object
Figure 1.5 Null Exception by Context object
Mostly the Server.Transfer method is preferable to use because Server.Transfer is faster since there is one less roundtrip, but some people say that Server.Transfer is not recommended since the operations typically flow through several different pages causing a loss of the correct URL of the page, but again it all depends on your requirement.
It is not a complete list. I wrote the basic differences between these two. If you know more differences, then please provide them in the comments.