Thursday, 25 October 2012


SQL Server Get Created, Last Modified date of Stored Procedure or Table



Introduction:

In this article  will explain how to find or get last modified date and created date of stored procedure or table in sql server


To get created date or last modified date of stored procedure we need to write the query like this


SELECT name,type,type_desc,create_date,modify_date FROM sys.objects
WHERE type = 'P' AND name = 'YourProcedureName'

Here if you observe above query I mentioned type = 'P' this mean to identify stored procedure or table or function etc based on 'P','U','FN',etc…


Here type = 'P'   (Procedure)
        type = 'U'  (User Table)
        type = 'S'  (System Table)
        type = 'PK' (Primary Key)
        type = 'FN' (Scalar Function)

Suppose if we want to get modified or created date of table we need to mention type = 'U' and the query will be like this


SELECT name,type,type_desc,create_date,modify_date FROM sys.objects
WHERE type = 'U' AND name = 'YourTableName'

Example:


SELECT name,[type],type_desc,create_date,modify_date FROM sys.objects
WHERE [type] = 'P' AND name = 'GetUserDetails'

Output:


Friday, 5 October 2012



Microsoft Announces "TypeScript"


TypeScript is a new script language developed by Microsoft for application-scale JavaScript development. TypeScript extends JavaScript and compiles to plain JavaScript. Since it is JavaScript, it eventually can run in any browser, host or OS. It is an open source language.


"TypeScript is Open Source"

Microsoft is going open source and we have seen that when Visual Studio 2012 had support for HTML 5 and JavaScript. TypeScript is essentially an extension of that. If you are a Microsoft developer, you may want to get an early preview of TypeScript.

Microsoft has also announced a plug-in for Visual Studio 2012 so VS developer that you can download here.
TypeScript is a language for application-scale JavaScript. TypeScript adds optional types, classes, and modules to JavaScript. TypeScript supports tools for large-scale JavaScript applications for any browser, for any host, on any OS. TypeScript compiles to clean, readable, standards-based JavaScript.
So, you may ask this question. Why do we need another JavaScript extended library? Well, TypeScript is much more than that. Today, JavaScript is limited in what you can do with it, but with TypeScript, developers can write complex and large applications. 

TypeScript extends JavaScript and compiles to plain JavaScript. 

Friday, 28 September 2012

Setup and Deployment Project in windows




-The final stage in the development of an application is deploying the project on client machines, which should be managed carefully. To organize the things in a proper fashion and install required files on the required folders we were provided with Setup and Deployment project under .net. This has to be added under the same solution where we developed our application.

-To add Setup and Deployment Project, open “Add New Project” window and in LHS panel expand the option "Other Project Types" and select Setup and Deployment, now in RHS panel choose "Setup Project" for any type of application or "Web Setup Project" for web applications.


When Setup Project is selected and opened it shows the options as following:
File System on Target Machine
-Application Folder
                                -User's Desktop
                                -User's Programs Menu

-File System on Target Machine in sense the target system where the project is being installed referring to folders on that machine. Application Folder refers to the project installation folder, which has to be specified while installing. User's Desktop refers to the desktop folder of target machine. User's Programs Menu refers to the programs menu folder of target machine.

-We can still add other folders referring to the target machine like Program Files, Fonts, GAC Folders etc. To add a new folder right click on "File System on Target Machine" and select "Add Special Folder" and then choose from the list of folders displayed:

-Now copy the appropriate content into the appropriate folders so that they get’s installed on the target machine in appropriate locations.

-Under Application Folder copy the assemblies (exe’s, dll’s) and config file that has to be installed on the target machine, to do this right click on the Application Folder & select the option Add -> Project Output:

-This opens a window showing the list of projects, select the exe project from it

-This will add the necessary exe's, dll's and config file

-Apart from Project Output we can also choose Folder or File or Assembly and add them under the Application Folder. Add Folder is used for adding a new folder for storing any images. Add File is used for adding any help documents. Add Assembly is used for adding any assemblies that are created outside of the solution



-If we want any shortcuts to be created for our application and place them either on desktop or added to programs menu do the following: Right click on the exe assembly (item of type output) under the application folder and select "Create Shortcut"


-This will create a shortcut specify a name to it. 

-For a shortcut we need to bind an display image of type Icon (.ico), to add an icon image go to the properties of shortcut -> select icon property and select browse from the list, which opens a window -> click on browse -> select application folder -> Images folder -> click on the add file button -> select the image from its physical location -> click on ok button -> again ok button.

-Now to place the short cut on desktop or program's menu folder, go to properties of shortcut again and select the property "Folder" -> click on the button beside it which opens a window, from it select user's desktop or user's programs menu folder which copies the shortcut to the selected folder.
-Installation user interface dialog boxes: setup project provides a number of predefined dialog boxes that you can use to display information or gather input during an installation. The following is a list of available dialog boxes. Not all dialog boxes are available for all deployment project types or for Admin installers. To view the current interfaces in the setup go to view menu -> Editor -> Select User Interface.

-You can still add new user interfaces like Splash, License Agreement, Register User, Read Me, Customer Information etc. To add a new user interface right click on the node Start -> select add dialog which displays the list of interface, choose what u require. e.g.: Splash, License Agreement.

-After adding required user interfaces, we can order them by right clicking on them and select MoveUp and MoveDown options.


-Splash requires a bitmap image to be set that has to be displayed, to set it go to the properties of splash interface -> under SplashBitmap property -> select browse -> choose an .bmp or .jpg image from its physical location same as we selected the .ico image previously for the shortcut.

-License Agreement & ReadMe requires any .rtf file to be set for displaying, which needs to be added same as above using the LicenseFile property and ReadMeFile properties of the interfaces.

-Customer Information will prompt for Name, Organization & Serial Number options; by default Serial Number Options will not be visible to make it visible set the ShowSerialNumber property as true:


-Setting the SerialNumberTemplate property to "<### - %%%%>" creates two text boxes separated by a dash surrounded by spaces. Validation for the first box (###) simply verifies that the user has entered three digits. The second box (%%%%) is validated by an algorithm that adds the digits together and divides the sum by 7. If the remainder is 0, validation succeeds; otherwise, it fails.

After configuring all the things under the Setup project, now in the top of the Visual Studio we find a ComboBox showing the options Debug & Release, default will be Debug change it as Release and then right click on the SetUp Project in the solution explorer and Select Build, which will compile all the Projects and prepare's the SetUp File which u can find them under the SetUp Projects, Release Folder which can be copied on to a CD or a DVD, which is carried to the client system for installing.


Note: Before installing the Setup on the Client Machine make sure that the .Net Framework is installed on it.

Tuesday, 25 September 2012

A general console application programs Day-5

Day5- Console application Programs




listing 1
// A program that uses the Building class.

using System;

class Building {
  public int floors;    // number of floors
  public int area;      // total square footage of building
  public int occupants; // number of occupants
}
 
// This class declares an object of type Building.
class BuildingDemo {
  public static void Main() {
    Building house = new Building(); // create a Building object
    int areaPP; // area per person
 
    // assign values to fields in house
    house.occupants = 4;
    house.area = 2500;
    house.floors = 2;
 
    // compute the area per person
    areaPP = house.area / house.occupants;
 
    Console.WriteLine("house has:\n  " +
                      house.floors + " floors\n  " +
                      house.occupants + " occupants\n  " +
                      house.area + " total area\n  " +
                      areaPP + " area per person");
  }
}

listing 2
// This program creates two Building objects.


using System;

class Building {
  public int floors;    // number of floors
  public int area;      // total square footage of building
  public int occupants; // number of occupants
}
 
// This class declares two objects of type Building.
class BuildingDemo {
  public static void Main() {
    Building house = new Building();
    Building office = new Building();

    int areaPP; // area per person
 
    // assign values to fields in house
    house.occupants = 4;
    house.area = 2500;
    house.floors = 2;

    // assign values to fields in office
    office.occupants = 25;
    office.area = 4200;
    office.floors = 3;
 
    // compute the area per person in house
    areaPP = house.area / house.occupants;
 
    Console.WriteLine("house has:\n  " +
                      house.floors + " floors\n  " +
                      house.occupants + " occupants\n  " +
                      house.area + " total area\n  " +
                      areaPP + " area per person");

    Console.WriteLine();

    // compute the area per person in office
    areaPP = office.area / office.occupants;

    Console.WriteLine("office has:\n  " +
                      office.floors + " floors\n  " +
                      office.occupants + " occupants\n  " +
                      office.area + " total area\n  " +
                      areaPP + " area per person");
  }
}


listing 3
// Add a method to Building.

using System;

class Building {
  public int floors;    // number of floors
  public int area;      // total square footage of building
  public int occupants; // number of occupants

  // Display the area per person.
  public void areaPerPerson() {
    Console.WriteLine("  " + area / occupants +
                      " area per person");
  }
}

// Use the areaPerPerson() method.
class BuildingDemo {
  public static void Main() {
    Building house = new Building();
    Building office = new Building();


    // assign values to fields in house
    house.occupants = 4;
    house.area = 2500;
    house.floors = 2;

    // assign values to fields in office
    office.occupants = 25;
    office.area = 4200;
    office.floors = 3;
 

    Console.WriteLine("house has:\n  " +
                      house.floors + " floors\n  " +
                      house.occupants + " occupants\n  " +
                      house.area + " total area");
    house.areaPerPerson();

    Console.WriteLine();

    Console.WriteLine("office has:\n  " +
                      office.floors + " floors\n  " +
                      office.occupants + " occupants\n  " +
                      office.area + " total area");
    office.areaPerPerson();
  }
}

listing 4
public void myMeth() {
  int i;

  for(i=0; i<10; i++) {
    if(i == 5) return; // stop at 5
    Console.WriteLine();
  }
}

listing 5
// Return a value from areaPerPerson().

using System;

class Building {
  public int floors;    // number of floors
  public int area;      // total square footage of building
  public int occupants; // number of occupants

  // Display the area per person.
  public int areaPerPerson() {
    return area / occupants;
  }
}
 
// Use the return value from areaPerPerson().
class BuildingDemo {
  public static void Main() {
    Building house = new Building();
    Building office = new Building();
    int areaPP; // area per person

    // assign values to fields in house
    house.occupants = 4;
    house.area = 2500;
    house.floors = 2;

    // assign values to fields in office
    office.occupants = 25;
    office.area = 4200;
    office.floors = 3;
 
    // obtain area per person for house
    areaPP = house.areaPerPerson();

    Console.WriteLine("house has:\n  " +
                      house.floors + " floors\n  " +
                      house.occupants + " occupants\n  " +
                      house.area + " total area\n  " +
                      areaPP + " area per person");


    Console.WriteLine();

    // obtain area per person for office
    areaPP = office.areaPerPerson();

    Console.WriteLine("office has:\n  " +
                      office.floors + " floors\n  " +
                      office.occupants + " occupants\n  " +
                      office.area + " total area\n  " +
                      areaPP + " area per person");
  }
}

listing 6
// A simple example that uses a parameter.

using System;

class ChkNum {
  // Return true if x is prime.
  public bool isPrime(int x) {
    for(int i=2; i < x/2 + 1; i++)
      if((x %i) == 0) return false;

    return true;
  }
}

class ParmDemo {
  public static void Main() {
    ChkNum ob = new ChkNum();

    for(int i=1; i < 10; i++)
      if(ob.isPrime(i)) Console.WriteLine(i + " is prime.");
      else Console.WriteLine(i + " is not prime.");

  }
}

listing 7
// Add a method that takes two arguments.

using System;

class ChkNum {
  // Return true if x is prime.
  public bool isPrime(int x) {
    for(int i=2; i < x/2 + 1; i++)
      if((x %i) == 0) return false;

    return true;
  }

  // Return the least common denominator.
  public int lcd(int a, int b) {
    int max;

    if(isPrime(a) | isPrime(b)) return 1;

    max = a < b ? a : b;

    for(int i=2; i < max/2 + 1; i++)
      if(((a%i) == 0) & ((b%i) == 0)) return i;

    return 1;
  }
}

class ParmDemo {
  public static void Main() {
    ChkNum ob = new ChkNum();
    int a, b;

    for(int i=1; i < 10; i++)
      if(ob.isPrime(i)) Console.WriteLine(i + " is prime.");
      else Console.WriteLine(i + " is not prime.");

    a = 7;
    b = 8;
    Console.WriteLine("Least common denominator for " +
                      a + " and " + b + " is " +
                      ob.lcd(a, b));

    a = 100;
    b = 8;
    Console.WriteLine("Least common denominator for " +
                      a + " and " + b + " is " +
                      ob.lcd(a, b));

    a = 100;
    b = 75;
    Console.WriteLine("Least common denominator for " +
                      a + " and " + b + " is " +
                      ob.lcd(a, b));

  }
}

listing 8
/*
   Add a parameterized method that computes the
   maximum number of people that can occupy a
   buiding assuming each needs a specified
   minimum space.
*/

using System;

class Building {
  public int floors;    // number of floors
  public int area;      // total square footage of building
  public int occupants; // number of occupants

  // Display the area per person.
  public int areaPerPerson() {
    return area / occupants;
  }

  /* Return the maximum number of occupants if each
     is to have at least the specified minimum area. */
  public int maxOccupant(int minArea) {
    return area / minArea;
  }
}

// Use maxOccupant().
class BuildingDemo {
  public static void Main() {
    Building house = new Building();
    Building office = new Building();

    // assign values to fields in house
    house.occupants = 4;
    house.area = 2500;
    house.floors = 2;

    // assign values to fields in office
    office.occupants = 25;
    office.area = 4200;
    office.floors = 3;
 
    Console.WriteLine("Maximum occupants for house if each has " +
                      300 + " square feet: " +
                      house.maxOccupant(300));

    Console.WriteLine("Maximum occupants for office if each has " +
                      300 + " square feet: " +
                      office.maxOccupant(300));
  }
}

listing 9
// A simple constructor.

using System;

class MyClass {
  public int x;

  public MyClass() {
    x = 10;
  }
}
 
class ConsDemo {
  public static void Main() {
    MyClass t1 = new MyClass();
    MyClass t2 = new MyClass();

    Console.WriteLine(t1.x + " " + t2.x);
  }
}

listing 10
// A parameterized constructor.

using System;

class MyClass {
  public int x;

  public MyClass(int i) {
    x = i;
  }
}
 
class ParmConsDemo {
  public static void Main() {
    MyClass t1 = new MyClass(10);
    MyClass t2 = new MyClass(88);

    Console.WriteLine(t1.x + " " + t2.x);
  }
}

listing 11
// Add a constructor to Building.

using System;

class Building {
  public int floors;    // number of floors
  public int area;      // total square footage of building
  public int occupants; // number of occupants


  public Building(int f, int a, int o) {
    floors = f;
    area = a;
    occupants = o;
  }

  // Display the area per person.
  public int areaPerPerson() {
    return area / occupants;
  }

  /* Return the maximum number of occupants if each
     is to have at least the specified minum area. */
  public int maxOccupant(int minArea) {
    return area / minArea;
  }
}
 
// Use the parameterized Building constructor.
class BuildingDemo {
  public static void Main() {
    Building house = new Building(2, 2500, 4);
    Building office = new Building(3, 4200, 25);

    Console.WriteLine("Maximum occupants for house if each has " +
                      300 + " square feet: " +
                      house.maxOccupant(300));

    Console.WriteLine("Maximum occupants for office if each has " +
                      300 + " square feet: " +
                      office.maxOccupant(300));
  }
}

listing 12
// Use new with a value type.

using System;

class newValue {
  public static void Main() {
    int i = new int(); // initialize i to zero

    Console.WriteLine("The value of i is: " + i);
  }
}

listing 13
// Demonstrate a destructor.

using System;

class Destruct {
  public int x;

  public Destruct(int i) {
    x = i;
  }  

  // called when object is recycled
  ~Destruct() {
    Console.WriteLine("Destructing " + x);
  }
 
  // generates an object that is immediately destroyed
  public void generator(int i) {
    Destruct o = new Destruct(i);
  }

}  
 
class DestructDemo {  
  public static void Main() {  
    int count;

    Destruct ob = new Destruct(0);

    /* Now, generate a large number of objects.  At
       some point, garbage collection will occur.
       Note: you might need to increase the number
       of objects generated in order to force
       garbage collection. */

    for(count=1; count < 100000; count++)
      ob.generator(count);

    Console.WriteLine("Done");
  }  
}

listing 14
using System;

class Rect {
  public int width;
  public int height;

  public Rect(int w, int h) {
    width = w;
    height = h;
  }

  public int area() {
    return width * height;
  }
}

class UseRect {
  public static void Main() {
    Rect r1 = new Rect(4, 5);
    Rect r2 = new Rect(7, 9);

    Console.WriteLine("Area of r1: " + r1.area());

    Console.WriteLine("Area of r2: " + r2.area());

  }
}

listing 15
using System;

class Rect {
  public int width;
  public int height;

  public Rect(int w, int h) {
    this.width = w;
    this.height = h;
  }

  public int area() {
    return this.width * this.height;
  }
}

class UseRect {
  public static void Main() {
    Rect r1 = new Rect(4, 5);
    Rect r2 = new Rect(7, 9);

    Console.WriteLine("Area of r1: " + r1.area());

    Console.WriteLine("Area of r2: " + r2.area());

  }
}

listing 16
public Rect(int width, int height) {
  this.width = width;
  this.height = height;
}

A general console application programs Day-4

Day4-Console application Programs

listing 1
// Determine if a value is positive or negative.
using System;

class PosNeg {
  public static void Main() {
    int i;

    for(i=-5; i <= 5; i++) {
      Console.Write("Testing " + i + ": ");

      if(i < 0) Console.WriteLine("negative");
      else Console.WriteLine("positive");
    }

  }
}

listing 2
// Determine if a value is positive, negative, or zero.
using System;

class PosNegZero {
  public static void Main() {
    int i;

    for(i=-5; i <= 5; i++) {

      Console.Write("Testing " + i + ": ");

      if(i < 0) Console.WriteLine("negative");
      else if(i == 0) Console.WriteLine("no sign");
        else Console.WriteLine("positive");
    }

  }
}

listing 3
// Determine smallest single-digit factor.
using System;

class Ladder {  
  public static void Main() {  
    int num;

    for(num = 2; num < 12; num++) {
      if((num % 2) == 0)
        Console.WriteLine("Smallest factor of " + num + " is 2.");
      else if((num % 3) == 0)
        Console.WriteLine("Smallest factor of " + num + " is 3.");
      else if((num % 5) == 0)
        Console.WriteLine("Smallest factor of " + num + " is 5.");
      else if((num % 7) == 0)
        Console.WriteLine("Smallest factor of " + num + " is 7.");
      else
        Console.WriteLine(num + " is not divisible by 2, 3, 5, or 7.");
    }
  }
}

listing 4
// Demonstrate the switch.

using System;

class SwitchDemo {
  public static void Main() {
    int i;

    for(i=0; i<10; i++)
      switch(i) {
        case 0:
          Console.WriteLine("i is zero");
          break;
        case 1:
          Console.WriteLine("i is one");
          break;
        case 2:
          Console.WriteLine("i is two");
          break;
        case 3:
          Console.WriteLine("i is three");
          break;
        case 4:
          Console.WriteLine("i is four");
          break;
        default:
          Console.WriteLine("i is five or more");
          break;
      }
   
  }
}

listing 5
// Use a char to control the switch.

using System;

class SwitchDemo2 {
  public static void Main() {
    char ch;

    for(ch='A'; ch<= 'E'; ch++)
      switch(ch) {
        case 'A':
          Console.WriteLine("ch is A");
          break;
        case 'B':
          Console.WriteLine("ch is B");
          break;
        case 'C':
          Console.WriteLine("ch is C");
          break;
        case 'D':
          Console.WriteLine("ch is D");
          break;
        case 'E':
          Console.WriteLine("ch is E");
          break;
      }  
  }
}

listing 6
// Empty cases can fall through.

using System;

class EmptyCasesCanFall {
  public static void Main() {
    int i;

    for(i=1; i < 5; i++)
      switch(i) {
        case 1:
        case 2:
        case 3: Console.WriteLine("i is 1, 2 or 3");
          break;
        case 4: Console.WriteLine("i is 4");
          break;
      }

  }
}

listing 7
// A negatively running for loop.

using System;

class DecrFor {  
  public static void Main() {  
    int x;

    for(x = 100; x > -100; x -= 5)
      Console.WriteLine(x);
  }
}

listing 8
/*
   Determine if a number is prime.  If it is not,
   then display its largest factor.
*/

using System;

class FindPrimes {  
  public static void Main() {  
    int num;
    int i;
    int factor;
    bool isprime;


    for(num = 2; num < 20; num++) {
      isprime = true;
      factor = 0;

      // see if num is evenly divisible
      for(i=2; i <= num/2; i++) {
        if((num % i) == 0) {
          // num is evenly divisible -- not prime
          isprime = false;
          factor = i;
        }
      }

      if(isprime)
        Console.WriteLine(num + " is prime.");
      else
        Console.WriteLine("Largest factor of " + num +
                          " is " + factor);
    }
  }  
}

listing 9
// Use commas in a for statememt.  

using System;

class Comma {  
  public static void Main() {  
    int i, j;

    for(i=0, j=10; i < j; i++, j--)
      Console.WriteLine("i and j: " + i + " " + j);
  }
}

listing 10
/*
   Use commas in a for statememt to find
   the largest and smallest factor of a number.
*/

using System;

class Comma {  
  public static void Main() {  
    int i, j;
    int smallest, largest;
    int num;

    num = 100;
 
    smallest = largest = 1;

    for(i=2, j=num/2; (i <= num/2) & (j >= 2); i++, j--) {

      if((smallest == 1) & ((num % i) == 0))
        smallest = i;

      if((largest == 1) & ((num % j) == 0))
        largest = j;

    }

    Console.WriteLine("Largest factor: " + largest);
    Console.WriteLine("Smallest factor: " + smallest);
  }
}

listing 11
// Loop condition can be any bool expression.
using System;

class forDemo {  
  public static void Main() {  
    int i, j;
    bool done = false;

    for(i=0, j=100; !done; i++, j--) {

      if(i*i >= j) done = true;

      Console.WriteLine("i, j: " + i + " " + j);
    }

  }  
}

listing 12
// Parts of the for can be empty.

using System;

class Empty {
  public static void Main() {
    int i;

    for(i = 0; i < 10; ) {
      Console.WriteLine("Pass #" + i);
      i++; // increment loop control var
    }
  }
}

listing 13
// Move more out of the for loop.

using System;

class Empty2 {
  public static void Main() {
    int i;

    i = 0; // move initialization out of loop
    for(; i < 10; ) {
      Console.WriteLine("Pass #" + i);
      i++; // increment loop control var
    }
  }
}

listing 14
// The body of a loop can be empty.

using System;

class Empty3 {
  public static void Main() {
    int i;
    int sum = 0;

    // sum the numbers through 5
    for(i = 1; i <= 5; sum += i++) ;

    Console.WriteLine("Sum is " + sum);
  }
}

listing 15
// Declare loop control variable inside the for.

using System;

class ForVar {
  public static void Main() {
    int sum = 0;
    int fact = 1;

    // compute the factorial of the numbers through 5
    for(int i = 1; i <= 5; i++) {
      sum += i;  // i is known throughout the loop
      fact *= i;
    }

    // but, i is not known here.

    Console.WriteLine("Sum is " + sum);
    Console.WriteLine("Factorial is " + fact);
  }
}

listing 16
// Compute the order of magnitude of an integer

using System;

class WhileDemo {
  public static void Main() {
    int num;
    int mag;

    num = 435679;
    mag = 0;

    Console.WriteLine("Number: " + num);

    while(num > 0) {
      mag++;
      num = num / 10;
    };

    Console.WriteLine("Magnitude: " + mag);
  }
}

listing 17
// Compute integer powers of 2.

using System;

class Power {
  public static void Main() {
    int e;
    int result;

    for(int i=0; i < 10; i++) {
      result = 1;
      e = i;

      while(e > 0) {
        result *= 2;
        e--;
      }

      Console.WriteLine("2 to the " + i +
                         " power is " + result);      
    }
  }
}

listing 18
// Display the digits of an integer in reverse order.

using System;

class DoWhileDemo {
  public static void Main() {
    int num;
    int nextdigit;

    num = 198;

    Console.WriteLine("Number: " + num);

    Console.Write("Number in reverse order: ");

    do {
      nextdigit = num % 10;
      Console.Write(nextdigit);
      num = num / 10;
    } while(num > 0);

    Console.WriteLine();
  }
}

listing 19
// Using break to exit a loop.

using System;

class BreakDemo {
  public static void Main() {

    // use break to exit this loop
    for(int i=-10; i <= 10; i++) {
      if(i > 0) break; // terminate loop when i is positive
      Console.Write(i + " ");
    }
    Console.WriteLine("Done");
  }
}

listing 20
// Using break to exit a do-while loop.

using System;

class BreakDemo2 {
  public static void Main() {
    int i;

    i = -10;  
    do {
      if(i > 0) break;
      Console.Write(i + " ");
      i++;
    } while(i <= 10);

    Console.WriteLine("Done");
  }
}

listing 21
// Find the smallest factor of a value.

using System;

class FindSmallestFactor {
  public static void Main() {
    int factor = 1;
    int num = 1000;
   
    for(int i=2; i < num/2; i++) {
      if((num%i) == 0) {
        factor = i;
        break; // stop loop when factor is found
      }
    }
    Console.WriteLine("Smallest factor is " + factor);
  }
}

listing 22
// Using break with nested loops.

using System;

class BreakNested {
  public static void Main() {

    for(int i=0; i<3; i++) {
      Console.WriteLine("Outer loop count: " + i);
      Console.Write("    Inner loop count: ");

      int t = 0;          
      while(t < 100) {
        if(t == 10) break; // terminate loop if t is 10
        Console.Write(t + " ");
        t++;
      }
      Console.WriteLine();
    }
    Console.WriteLine("Loops complete.");
  }
}

listing 23
// Use continue.

using System;

class ContDemo {
  public static void Main() {
    // print even numbers between 0 and 100
    for(int i = 0; i <= 100; i++) {
      if((i%2) != 0) continue; // iterate
      Console.WriteLine(i);
    }
  }
}

listing 24
// Use goto with a switch.

using System;

class SwitchGoto {
  public static void Main() {

    for(int i=1; i < 5; i++) {
      switch(i) {
        case 1:
          Console.WriteLine("In case 1");
          goto case 3;
        case 2:
          Console.WriteLine("In case 2");
          goto case 1;
        case 3:
          Console.WriteLine("In case 3");
          goto default;
        default:
          Console.WriteLine("In default");
          break;
      }

      Console.WriteLine();
    }

//    goto case 1; // Error! Can't jump into a switch.
  }
}

listing 25
// Demonstrate the goto.

using System;

class Use_goto {  
  public static void Main() {  
    int i=0, j=0, k=0;

    for(i=0; i < 10; i++) {
      for(j=0; j < 10; j++ ) {
        for(k=0; k < 10; k++) {
          Console.WriteLine("i, j, k: " + i + " " + j + " " + k);
          if(k == 3) goto stop;
        }
      }
    }

stop:
    Console.WriteLine("Stopped! i, j, k: " + i + ", " + j + " " + k);
 
  }
}

A general console application programs Day-3

Day3-Console applications Programs

listing 1
// Demonstrate the % operator.

using System;

class ModDemo {  
  public static void Main() {  
    int iresult, irem;
    double dresult, drem;

    iresult = 10 / 3;
    irem = 10 % 3;

    dresult = 10.0 / 3.0;
    drem = 10.0 % 3.0;

    Console.WriteLine("Result and remainder of 10 / 3: " +
                       iresult + " " + irem);
    Console.WriteLine("Result and remainder of 10.0 / 3.0: " +
                       dresult + " " + drem);
  }  
}

listing 2
/*
   Demonstrate the difference between prefix
   postfix forms of ++.
*/
using System;

class PrePostDemo {
  public static void Main() {  
    int x, y;
    int i;

    x = 1;
    Console.WriteLine("Series generated using y = x + x++;");
    for(i = 0; i < 10; i++) {

      y = x + x++; // postfix ++

      Console.WriteLine(y + " ");
    }
    Console.WriteLine();

    x = 1;
    Console.WriteLine("Series generated using y = x + ++x;");
    for(i = 0; i < 10; i++) {

      y = x + ++x; // prefix ++

      Console.WriteLine(y + " ");
    }
    Console.WriteLine();
 
  }
}

listing 3
// Demonstrate the relational and logical operators.

using System;

class RelLogOps {  
  public static void Main() {  
    int i, j;
    bool b1, b2;

    i = 10;
    j = 11;
    if(i < j) Console.WriteLine("i < j");
    if(i <= j) Console.WriteLine("i <= j");
    if(i != j) Console.WriteLine("i != j");
    if(i == j) Console.WriteLine("this won't execute");
    if(i >= j) Console.WriteLine("this won't execute");
    if(i > j) Console.WriteLine("this won't execute");

    b1 = true;
    b2 = false;
    if(b1 & b2) Console.WriteLine("this won't execute");
    if(!(b1 & b2)) Console.WriteLine("!(b1 & b2) is true");
    if(b1 | b2) Console.WriteLine("b1 | b2 is true");
    if(b1 ^ b2) Console.WriteLine("b1 ^ b2 is true");
  }  
}

listing 4
// Create an implication operator in C#.
using System;

class Implication {
  public static void Main() {  
    bool p=false, q=false;
    int i, j;

    for(i = 0; i < 2; i++) {
      for(j = 0; j < 2; j++) {
        if(i==0) p = true;
        if(i==1) p = false;
        if(j==0) q = true;
        if(j==1) q = false;
       
        Console.WriteLine("p is " + p + ", q is " + q);
        if(!p | q) Console.WriteLine(p + " implies " + q +
                    " is " + true);
        Console.WriteLine();
      }
    }
  }
}

listing 5
// Demonstrate the short-circuit operators.

using System;

class SCops {  
  public static void Main() {  
    int n, d;

    n = 10;
    d = 2;
    if(d != 0 && (n % d) == 0)
      Console.WriteLine(d + " is a factor of " + n);

    d = 0; // now, set d to zero

    // Since d is zero, the second operand is not evaluated.
    if(d != 0 && (n % d) == 0)
      Console.WriteLine(d + " is a factor of " + n);
   
    /* Now, try the same thing without short-circuit operator.
       This will cause a divide-by-zero error.  */
    if(d != 0 & (n % d) == 0)
      Console.WriteLine(d + " is a factor of " + n);
  }  
}

listing 6
// Side-effects can be important.

using System;

class SideEffects {  
  public static void Main() {  
    int i;

    i = 0;

    /* Here, i is still incremented even though
       the if statement fails. */
    if(false & (++i < 100))
       Console.WriteLine("this won't be displayed");
    Console.WriteLine("if statement executed: " + i); // displays 1

    /* In this case, i is not incremented because
       the short-circuit operator skips the increment. */
    if(false && (++i < 100))
      Console.WriteLine("this won't be displayed");
    Console.WriteLine("if statement executed: " + i); // still 1 !!
  }  
}

listing 7
//  Use bitwise AND to make a number even.
using System;

class MakeEven {
  public static void Main() {
    ushort num;
    ushort i;  

    for(i = 1; i <= 10; i++) {
      num = i;

      Console.WriteLine("num: " + num);

      num = (ushort) (num & 0xFFFE); // num & 1111 1110

      Console.WriteLine("num after turning off bit zero: "
                        +  num + "\n");
    }
  }
}

listing 8
//  Use bitwise AND to determine if a number is odd.
using System;

class IsOdd {
  public static void Main() {
    ushort num;

    num = 10;

    if((num & 1) == 1)
      Console.WriteLine("This won't display.");

    num = 11;

    if((num & 1) == 1)
      Console.WriteLine(num + " is odd.");

  }
}

listing 9
// Display the bits within a byte.
using System;

class ShowBits {
  public static void Main() {
    int t;
    byte val;

    val = 123;
    for(t=128; t > 0; t = t/2) {
      if((val & t) != 0) Console.Write("1 ");
      if((val & t) == 0) Console.Write("0 ");
    }
  }
}

listing 10
//  Use bitwise OR to make a number odd.
using System;

class MakeOdd {
  public static void Main() {
    ushort num;
    ushort i;  

    for(i = 1; i <= 10; i++) {
      num = i;

      Console.WriteLine("num: " + num);

      num = (ushort) (num | 1); // num | 0000 0001

      Console.WriteLine("num after turning on bit zero: "
                        +  num + "\n");
    }
  }
}

listing 11
// Use XOR to encode and decode a message.

using System;

class Encode {
  public static void Main() {
    char ch1 = 'H';
    char ch2 = 'i';
    char ch3 = '!';

    int key = 88;

    Console.WriteLine("Original message: " +
                      ch1 + ch2 + ch3);

    // encode the message
    ch1 = (char) (ch1 ^ key);
    ch2 = (char) (ch2 ^ key);
    ch3 = (char) (ch3 ^ key);

    Console.WriteLine("Encoded message: " +
                      ch1 + ch2 + ch3);

    // decode the message
    ch1 = (char) (ch1 ^ key);
    ch2 = (char) (ch2 ^ key);
    ch3 = (char) (ch3 ^ key);
 
    Console.WriteLine("Decoded message: " +
                      ch1 + ch2 + ch3);
  }
}

listing 12
// Demonstrate the bitwise NOT.
using System;

class NotDemo {
  public static void Main() {
    sbyte b = -34;
    int t;

    for(t=128; t > 0; t = t/2) {
      if((b & t) != 0) Console.Write("1 ");
      if((b & t) == 0) Console.Write("0 ");
    }
    Console.WriteLine();

    // reverse all bits
    b = (sbyte) ~b;

    for(t=128; t > 0; t = t/2) {
      if((b & t) != 0) Console.Write("1 ");
      if((b & t) == 0) Console.Write("0 ");
    }
  }
}

listing 13
// Demonstrate the shift << and >> operators.
using System;

class ShiftDemo {
  public static void Main() {
    int val = 1;
    int t;
    int i;

    for(i = 0; i < 8; i++) {
      for(t=128; t > 0; t = t/2) {
        if((val & t) != 0) Console.Write("1 ");
        if((val & t) == 0) Console.Write("0 ");
      }
      Console.WriteLine();
      val = val << 1; // left shift
    }
    Console.WriteLine();

    val = 128;
    for(i = 0; i < 8; i++) {
      for(t=128; t > 0; t = t/2) {
        if((val & t) != 0) Console.Write("1 ");
        if((val & t) == 0) Console.Write("0 ");
      }
      Console.WriteLine();
      val = val >> 1; // right shift
    }
  }
}

listing 14
// Use the shift operators to multiply and divide by 2.
using System;

class MultDiv {
  public static void Main() {
    int n;

    n = 10;

    Console.WriteLine("Value of n: " + n);

    // multiply by 2
    n = n << 1;
    Console.WriteLine("Value of n after n = n * 2: " + n);

    // multiply by 4
    n = n << 2;
    Console.WriteLine("Value of n after n = n * 4: " + n);

    // divide by 2
    n = n >> 1;
    Console.WriteLine("Value of n after n = n / 2: " + n);

    // divide by 4
    n = n >> 2;
    Console.WriteLine("Value of n after n = n / 4: " + n);
    Console.WriteLine();

    // reset n
    n = 10;
    Console.WriteLine("Value of n: " + n);

    // multiply by 2, 30 times
    n = n << 30; // data is lost
    Console.WriteLine("Value of n after left-shifting 30 places: " + n);

  }
}

listing 15
// Prevent a division by zero using the ?.
using System;

class NoZeroDiv {
  public static void Main() {
    int result;
    int i;

    for(i = -5; i < 6; i++) {
      result = i != 0 ? 100 / i : 0;
      if(i != 0)
        Console.WriteLine("100 / " + i + " is " + result);
    }
  }
}

listing 16
// Prevent a division by zero using the ?.
using System;

class NoZeroDiv2 {
  public static void Main() {
    int i;

    for(i = -5; i < 6; i++)
      if(i != 0 ? true : false)
        Console.WriteLine("100 / " + i +
                           " is " + 100 / i);
  }
}

SharePoint tenant opt-out for modern lists is retiring in 2019

We're making some changes to how environments can opt out of modern lists in SharePoint. Starting April 1, 2019, we're going to be...