Thursday 5 June 2014

For Intermediates: C# Methods.(part-1)

For Intermediates: C# Methods.(part-1)

Definition:
A method is a code block containing a series of statements. Methods must be declared within a class or a structure. It is a good programming practice that methods do only one specific task. Methods bring modularity to programs. 


We  need to be able to break large programs into smaller chunks that are easy to handle. C# lets you divide your class code into chunks known as methods. Properly designed and implemented methods can greatly simplify the job of writing complex programs in c#.Methods change the state of the objects created. They are the dynamic part of the objects; data is the static part.

Example:
 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication6
{
    class Sample
{
public int a; // Nonstatic

public static int b;   // Static

public void InstanceMethod()   // Nonstatic
{
Console.WriteLine(“instance method”);
}

public static void ClassMethod()   // Static
{
Console.WriteLine(“ class method”);
}

}

        static void Main(string[] args)
{

        Sample ex = new Sample(); 
    ex.a = 1; 
    Sample.b=2;
}
}                           

// in the above code indicate it is comments and the code follows // will not be executed.
                                                                     
 In the above programming example to invoke a nonstatic — instance — method, you need an instance of the class. To invoke a static — class — method, you call via the class name, not an instance. The following code snippet assigns a value to the object data member a and the class, or static, member b. note in the above program static keyword is used to indicate static variable and static methods.
Sample ex = new Sample(); // Create an instance of class Example.
ex.a = 1; // Initialize instance member through instance.
Sample.b = 2; // Initialize class member through class

The following snippet defines and accesses InstanceMethod() and ClassMethod() in almost the same way:


Sample ex=new Sample();
ex.InstanceMethod();
Sample.classMethod();

The expression ex.InstanceMethod() passes control to the code contained within the method. C# follows an almost identical process for Sample.ClassMethod().
Output:

instance method
class method

After a method completes execution, it returns control to the point where it was called. That is, control moves to the next statement after the call.
Sample :
We start with a simple example.
using System;

public class Base
{
    public void viewInfo()
    {
        Console.WriteLine("Base class");
    }
}

public class SimpleMethod
{
    static void Main()
    {
        Base bs = new Base();
        bs.viewInfo();
    }
}

Each method must be defined inside a class or a struct. It must have a name. In our case the name is ViewInfo. The keywords that precede the name of the method are access specifier and the return type. Parentheses follow the name of the method. They may contain parameters of the method. Our method does not take any parameters.
static void Main()
{
   ...
}

Passing an argument to a method

The values you input to a method are method arguments, or parameters.Most methods require some type of arguments if they’re going to do somethingYou pass arguments to a method by listing them in the parentheses that follow the method name. Consider this small addition to the earlier Sample class:

public class Sample
{
public static void Output(string someString)
{
Console.WriteLine(“Output() was passed the argument: “ + someString);
}
}
I could invoke this method from within the same class, like this:

Output(“Hai”);

I would then see this not-too-exciting output:


Output() was passed the argument: Hai
----will continue

For Beginners: Excel Functions .If Function

For Beginners: Excel Functions .If Function

The if function is used to test a condition . one value will be returned if the condition is true and another value will be returned if the condition is false.

Syntax:
IF(logical-test, value-if-true, value-if-false)

In the above syntax logical test is the condition to be checked. Value-if- true is the value to be returned if the condition is true. Value-if-false  is the value to be returned if the condition is false.

Example:

=If(a2<=1000,”good budget”, ”bad budget”)
=if(d1>=40,”pass”, ”fail”)
=if(h1>15000,500,0)
 in the above examples a2, d1, h1 are cell addresses. 

And:

Using and function we can check more than one condition. If we use and along with if function it will return true only if all the conditions return true.

Example:

=If(AND(a1>=40,a2>=40),”pass”,”fail”)

In the above example, “pass” will be returned only if both a1 and a2 values are greater than or equal to 40. If even one condition is false,”fail” will be returned.



For Beginners : Printing webpages using java script:

For Beginners : Printing webpages using java script:

The following code is for  print webpages using javascript

<a href="javascript:window.print()"><img src="print.gif"></a>


The text in red is a JavaScript method. It looks like target link. But it is directing the link for print page dialog box. When we click the image (print.gif) the print dialog box will appear.

 from the printing dialog box we can select no of copies, print pages range and order of printing etc.
another simple method using button control:

<form>
<input type="button" value="Print current page" onClick="window.print()">
</form>

  When we click the button  the print dialog box will appear. After choosing the options in the dialog box we can print the page

Wednesday 4 June 2014

Disabling right click on webpages using javascript:

Disabling right click on webpages using javascript:
We can stop users right click on  web pages.here is a simple javascript code to avoid right click by users.
<script language="javascript">
document.onmousedown=disableclick;
message="Right Click not enabled";
Function disableclick(event)
{
  if(event.button==2)
   {
     alert(message);
     return false;   
   }
}
</script>
and
<body oncontextmenu="return false">
...
</body>


The above code will avoid showing context menu when a user right click on it.

Disable Back Functionality In Php Using Javascript

Disable Back Functionality In Php Using Javascript

There is a smart technique to disable the back functionality in any webpage. We can disable the back navigation by adding following bit of code in the webpage. Now the problem  here is that you have to add this code in all the pages where you want to avoid user to get back from previous page. For example  a user makes the navigation from page1 to page2. And we want to stop  user from page2 to go back to page1. In this case add following code will be added  in php  page1.


<HEAD>
<SCRIPT type="text/javascript">

    window.history.forward();

    function noBack() { window.history.forward(); }

</SCRIPT>

</HEAD>

<BODY onload="noBack();"
    onpageshow="if (event.persisted) noBack();" onunload="">



The above code will trigger history.forward event for page1. That is  if user presses Back button on page2, he will be sent to page1. But the history.forward code on page1 makes the user back to page2. Thus user will not be able to go back from page2 to page1.





Value & reference type in c#

Value & reference type in c# 


C# has two different categories of data type:

1. value type.
2. Reference type.


Value type in c# stores its value directly, where as reference type stores a reference to a value.

There are simple types of variables in c# like integer and float. These are simply value type .Reference types are similar to types accessed through pointers in c++.
  
These two different types of variables stored in different places in memory. Value types are stored in stack memory, and reference types are stored in managed heap memory. Value and reference types assignment has different effects.

Consider x and y are type of int

x=10;

y=x;

now x and y will have the value 10.

 Now we change x=20;

The change in x will not affect the value in y. y will retain the old vale 10.

Because int is a value type, x and y are different locations in memory. .

Now consider rectangle as reference type:
 Rectangle r1,r2;

r1=new Rectangle();
  
r1.length=10;
r2=r1;

 Console.writeLine(r2.length);

Above statement will print the output as:

10.
  
Now consider the following change.

r2.length=20;
  
Console.WriteLine(r1.length);
  
The above statement will give the output as:

20;

After executing the following line

r2=r1;

 There will be only one Rectangle object in memory . r1 , r2 both point to this Rectangle. Since both are referencing same memory location any change in one object will affect another object 

In c# basic data type bool is also  value type. In c# most complex data types including classes are reference data type.




Sunday 1 June 2014

Arrays in c language:

Arrays in c language:

Definition

An array is an aggregate data type that lets you access multiple variables through a single name by use of an index.

An ordinary variable cannot store more than one value at a time. Consider a situation of storing the marks of students of a class consists of sixty students. we need 60 variables . so the program will become very complex.

The solution to the above situation is to declare a variable that can store more than one value. In this situation we need 60 blocks of memory. A variable that can store more than one value is called an array.  In an array all the data should be of same data type . All the data are stored in contiguous memory location.

Array declaration:


To store integer value of 5 data example is
int a[5];.
a is actually an  user defined variable name . all the rules of naming an ordinary variable also apply to an array variable name.

Assigning data to an array example:


int a[5]={5,6,4,3,7};
the data are actually stored as
a[0]=5
a[1]=6
a[2]=4
a[3]=3
a[4]=7
note array index starts with zero and upper bound index is less than one of array length. In the above example, length of array is 5.

Getting input for an array example

for(i=0;i<5;i++)
{
scanf(“%d”,&a[i]);
}

Printing output for an array example is

for(i=0;i<5;i++)
{
printf(“%d\t ”, a[i]);
}

 sample program:

#include<stdio.h>
#include<conio.h>
void main()
{
int a[5],i;
clrscr();

/* getting input */
printf(“enter 5 values of a”);
for(i=0;i<5;i++)
{
scanf(“%d”,&a[i]);
}

/* printing output */
printf(“The datas of array a are”);
 for(i=0;i<5;i++)
{
printf(“%d\t ”, a[i]);
}

getch();
}