JavaScript Part-12 Closures

A closure in JavaScript is an inner function that has access to its outer function's scope, even after the outer function has returned control. A closure makes the variables of the inner function private. A

Simple example of a closure is shown below:

var count = (function () {
var _counter = 0;
return function () {return _counter += 1;}
})();

count();
count();
count();

// the counter is now 3



The variable count is assigned an outer function. The outer function runs only once, which sets the counter to zero and returns an inner function. The _counter variable can be accessed only by the inner function, which makes it behave like a private variable.


Saturday, 10 March 2018
Posted by Sudhir Chekuri

JavaScript Part-11 IIFE ( Immediately invoked function expressions )

IIFE ( Immediately invoked function expression ) is a function that's executed as soon as it's created. It is a self invoking anonymous function. It has no connection with any events or asynchronous execution.

Syntax:

( function( ) {
     // Code goes here
     // ...
    } )( );

Explanation:

1.  ( function( ) {
     // Code goes here
     // ...
    } )( );

All the code inside the function will be converted into an expression.

2.  ( function( ) {
     // Code goes here
     // ...
    } )( );

The second pair of parentheses calls the function resulting from the expression.

Its most common usage is to limit the scope of a variable made via var or to encapsulate context to avoid name collisions.


Posted by Sudhir Chekuri

JavaScript Part-10 Event handlers


Event handlers are used to execute a function based on the action performed by the user like onclick, onload, onmouseover etc

In the below example 3 buttons are available on the web page - Touch me 1, Touch me 2 and Touch me 3
onClick event is fired when user clicks on each button.
Alert boxes are displayed on user button click.
Touch me 1 - Alert button code is written in html tag.
Touch me 2 - Function named "first" is called on onClick.
Touch me 3 - Function named "second" is called by passing a parameter named "a".

Example code for onClick event handler

<!DOCTYPE html>
<html>

  <head>
 
   <script type="text/javascript">
   
      function first()
      {
     
        alert('Without parameter');
      }
      function second(a)
      {
     
         alert('With parameter '+a);
      }
   
   
    </script>
 
  </head>

  <body>
    <form>
      <!--alert box code in click event-->
      <input type="button" value="Touch me 1" onClick="alert('Hello')" />
      <!--Function called in click event-->
      <input type="button" value="Touch me 2" onClick="first()" />
      <!--Function with parameter called in click event-->
      <input type="button" value="Touch me 3" onClick="second('Hello')" />
    </form>
 
  </body>

</html>

Output



Posted by Sudhir Chekuri

JavaScript Part-9 Calling function in a function

Two functions named as first and second are created then i called both functions in other function called start.

On the page load "start function" will be called when script is executed. Start function will call "first function" and then "second function".

Javascript example code for calling function in a function:

<!DOCTYPE html>
<html>

  <head>
 
   <script type="text/javascript">
      function First()
      {
        document.write("I am first function ")
      }
      function Second()
      {
        document.write("I am second function")
      }
      function start(){
        First();
        Second();
      }
      start();
    </script>
 
  </head>

  <body>
   
    <h1>Hello World!</h1>
  </body>

</html>

Output:

I am first function I am second function

Hello World!



Posted by Sudhir Chekuri

C#.NET Interview Questions and Answers

1. What is C#.NET?

Ans: C#.NET is an object oriented programming language used as code behind language to create .NET applications.

2. What is namespace?

Ans: .NET Framework uses namespaces to organize its many classes, as follows: System.Console.WriteLine("Hello World!");
System is a namespace and Console is a class in that namespace.

Namespaces are declared at the top of every program as shown below to use classes inside it in the program.
Example: using System;

3. What is the base class for all .NET datatypes?


Ans: The Object Type is the ultimate base class for all data types in C# Common Type System (CTS). Object is an alias for System.Object class. The object types can be assigned values of any other types, value types, reference types, predefined or user-defined types. However, before assigning values, it needs type conversion.

4. What is Boxing in C#.NET?

Ans: When a value type is converted to object type, it is called boxing.

5. What is UnBoxing in C#.NET?

Ans: When an object type is converted to a value type, it is called unboxing.

6. What are pointer types in C#?

Ans: Pointer type variables store the memory address of another type. Pointers in C# have the same capabilities as the pointers in C or C++.
Syntax for declaring a pointer type is −type* identifier;
For example:
char* cptr;
int* iptr;

7. What is encapsulation?

Ans: Encapsulation is defined 'as the process of enclosing one or more items within a physical or logical package'. Encapsulation, in object oriented programming methodology, prevents access to implementation details.

8. C#.NET Access specifiers?

Ans: Access specifiers are used to define scope of a type as well as its members ie.,who can access it and who can't access it.
C#.NET supports five different access specifiers. They are:
1)Private
2)Public
3)Protected
4)Internal
5)Protected Internal

Default access specifier of method is private.
Default access specifier of class is internal.

Private:Members declared as private cannot be accessed outside the block.
Members declared as private within class or structure aren't possible outside of them in which they are defined.
In C# the default scope for members of a class or structure is "Private".
Public:
Members declared as public can be accessed from anywhere.
Protected:
Members declared as protected can be accessed only in the inherited classes
Internal:
Members declare as internal can be used only with in the project.
Members which are declared as internal were accessible only with in the project both from child or non-child classes.
The default scope for class is Internal.
Protected Internal:
Members declared as Protected Internal, enjoy dual scope ie., Internal and Protected.
Within the project they behave as internal, providing access to all other classes.
Outside the project they behave as Protected, and still provide access to child classes


Wednesday, 14 June 2017
Posted by Sudhir Chekuri

3 layer architecture



Create website- ASP Project
Create class library - BAL
Create class library - DAL

Add references as below:

BAL in ASP Project
DAL in BAL

Adding class:

Right click on dal project
add class with name as DUser.cs
Make class as public bcoz it is used in other project.

To use some namespaces you have to add reference in DAL project

Right click on DAL Project
Add references
In assemblies/.NET add System.Configuration to use ConfigurationManager class in DAL layer.
Create DUser class - Write method with parameters and ADO.NET Code.

Right click on BAL Project
Add BUser class - Write method with parameters and C# logic

Sending inputs:

PL - input parameter (txt.text) - > BAL  - input parameter (variables) -> DAL

Returning values:

DAL - return value -> BAL - return value -> PL

Calling methods:

Call methods of BAL from Presentation layer.
Call methods of DAL from BAL.


Saturday, 18 March 2017
Posted by Sudhir Chekuri
Tag :

Developer Unit Testing

Unit testing is done by the developer to check your code is working as expected or not.
It is called unit testing as the developed functionality is tested individuals as small units.
Visual Studio Test Explorer is used to find the list of unit test methods available in the project and provides options to run/debug your unit tests.
It improves the quality of the code as it is easy to test every unit of program using automated unit testing as soon as new code is added into the project.
With Test driven development by proper unit testing bugs in the code can be reduced.

You can also install add ons of other 3rd party unit testing tools in Visual Studio to use them for unit testing.


Code Example:

In the below example Unit Test project is created.
In test class(UnitTest1) two test methods are created 1. TestAuthSignInSuccess and 2. TestAuthSignInFail methods.
Original method(DUserAuth) used for user registration which has to be tested is also written in the same class.

--------------

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Data.SqlClient;
using System.Data;

namespace UTDigitalWorkSpace
{
    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void TestAuthSignInSuccess()
        {
            //inputs
            string username = "ravi";
            string password = "ravi123";
            //outputs
            UnitTest1 ut = new UnitTest1();
            int result = ut.DUserAuth(username, password);

            //expected outputs
            int expectedResult = 1;
            //check
            Assert.AreEqual(result, expectedResult);
        }
        [TestMethod]
        public void TestAuthSignInFail()
        {
            //inputs
            string username = "ravi";
            string password = "ravi12344";
            //outputs
            UnitTest1 ut = new UnitTest1();
            int result = ut.DUserAuth(username, password);

            //expected outputs
            int expectedResult = 0;
            //check
            Assert.AreEqual(result, expectedResult);

        }

        public int DUserAuth(string UserName, string Password)
        {
            SqlConnection con1 = new SqlConnection(@"Data Source=.;Initial Catalog=DigitalWorkSpace;Integrated Security=True");

            SqlCommand cmd = new SqlCommand("select * from Tbl_SignUp where UserName='" + UserName + "'and password='" + Password + "'and Status='A'", con1);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            return ds.Tables[0].Rows.Count;
        }
    }
}


Wednesday, 15 March 2017
Posted by Sudhir Chekuri
Tag :

Followers

Total Pageviews

Powered by Blogger.

- Copyright © 2013 DevStudent - Metrominimalist - Powered by Blogger - Designed by Johanes Djogan -