Powered By Blogger

Friday, November 6, 2009

access specifiers in C# and VB.Net

Public, Private, Protected, Friend, Internal, Default


1. PUBLIC
As the name specifies, it can be accessed from anywhere. If a member of a class is defined as public then it can be accessed anywhere in the class as well as outside the class. This means that objects can access and modify public fields, properties, methods.

2. PRIVATE
As the name suggests, it can't be accessed outside the class. Its the private property of the class and can be accessed only by the members of the class.

3. FRIEND/INTERNAL
Friend & Internal mean the same. Friend is used in VB.NET. Internal is used in C#. Friends can be accessed by all classes within an assembly but not from outside the assembly.

4. PROTECTED
Protected variables can be used within the class as well as the classes that inherites this class.

5. PROTECTED FRIEND/PROTECTED INTERNAL
The Protected Friend can be accessed by Members of the Assembly or the inheriting class, and ofcourse, within the class itself.

6. DEFAULT
A Default property is a single property of a class that can be set as the default. This allows developers that use your class to work more easily with your default property because they do not need to make a direct reference to the property. Default properties cannot be initialized as Shared/Static or Private and all must be accepted at least on argument or parameter. Default properties do not promote good code readability, so use this option sparingly.

difference between ADO and ADO.NET

  • ADO works with connected data. This means that when you access data, such as viewing and updating data, it is real-time, with a connection being used all the time.
  • The ADO.NET object is a lightweight object. The ADO Recordset was a huge object in ADO.
  • ADO.NET breaks the functionality of the ADO object to multiple classes, thereby allowing a focused approach to developing code.
  • The ADO.NET DataReader is equivalent to the "firehose" cursor.
  • The DataSet is a disconnected cache with tracking and control binding functionality.
  • The DataAdapter provides the ability to completely customize how the central data store is updated with the changes to a DataSet.
  • ADO allows you to create client-side cursors only, whereas ADO.NET gives you the choice of either using client-side or server-side cursors.
  • ADO allows you to persist records in XML format, ADO.NET allows you to manipulate your data using XML as the primary means.


difference between Classic ASP and ASP.NET

Major difference:

* Classic ASP is Interpreted. ASP.NET is Compiled. If code is changed, ASP.NET recompiles,
otherwise does'nt.

*Classic ASP uses a technology called ADO to connect and work with databases. ASP.NET uses the ADO.NET technology (which is the next generation of ADO).

Others:

*
In classic ASP, there was no server controls. You have to write all html tags manually. ASP.NET offers a very rich set of controls called Server Controls and Html Controls.

*Validation Controls.

*Debugging benefits-In classic ASP, debugging is a tough task because of limited support due to the interpreted model. In contrast, not only ASP.NET improves the performance over the interpreted model but also provides debugging tools for component developers and pages compiled into classes.

*Multi Language Support-Only VBScript and Javascript were available for scripting in ASP where as, in ASP.NET there are no such restrictions. The .NET compliant language can be used with ASP.NET including several like C# and VB.NET, where both of them are server-sided languages.

Abstract Class vs Interface with example

Abstract Class

-An abstract class is a special kind of class that cannot be instantiated.
-An abstract class is only to be sub-classed (inherited from).
-It enforces certain hierarchies for all the subclasses.
- A class may inherit only one abstract class, but may implement multiple number of Interfaces.


Interface

-An interface is not a class
-Interface have only Signature.
-
It does not have Contructor,destructor,Fileds
-


Difference:

*A class may inherit only one abstract class, but may implement multiple number of Interfaces. Say a class named Car needs to inherit some basic features of a vehicle, it may inherit from an Aabstract class named Vehicle. A car may be of any kind, it may be a vintage car, a sedan, a coupe, or a racing car. For these kind of requirements, say a car needs to have only two seats (means it is a coupe), then the class Car needs to implement a member field from an interface, that we make, say ICoupe.

*Members of an abstract class may have any access modifier, but members of an interface are public by default, and cant have any other access modifier.

*Abstract class methods may OR may not have an implementation, while methods in an Interface only have a definition, no implementation.

*Interface is used to "implements"; whereas abstract class is used to "extends".

* Interface can "extends" another interface, abstract class can "extends" another class and "implements" multiple interfaces.

*************************************************************************************

Multiple inheritance:
---------------------

Abstract class- A class may inherit only one abstract class.

Interface - A class may inherit several interfaces.

Access Modfiers
--------------------

An abstract class can contain access modifiers for the subs, functions, properties

An interface cannot have access modifiers for the subs, functions, properties etc everything is assumed as public

Speed
-------


Abstract class- Fast

Interface -Requires more time to find the actual method in the corresponding classes.

************************************************************************************


Example:

Abstract Class

public abstract class Employee
{
//we can have fields and properties


//in the Abstract class

protected String id;
protected String lname;
protected String fname;

//properties


public abstract String ID
{
get;
set;
}

public abstract String FirstName
{
get;
set;
}

public abstract String LastName
{
get;
set;
}
//completed methods


public String Update()
{
return "Employee " + id + " " +
lname + " " + fname +
" updated";
}
//completed methods


public String Add()
{
return "Employee " + id + " " +
lname + " " + fname +
" added";
}
//completed methods


public String Delete()
{
return "Employee " + id + " " +
lname + " " + fname +
" deleted";
}
//completed methods


public String Search()
{
return "Employee " + id + " " +
lname + " " + fname +
" found";
}

//abstract method that is different


//from Fulltime and Contractor

//therefore i keep it uncompleted and

//let each implementation

//complete it the way they calculate the wage.


public abstract String CalculateWage();

}

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

Interface

public interface IEmployee
{

// protected String id;
// protected String lname;
// protected String fname;


//just signature of the properties

//and methods.

//setting a rule or contract to be

//followed by implementations.


String ID
{
get;
set;
}

String FirstName
{
get;
set;
}

String LastName
{
get;
set;
}

// cannot have implementation


// cannot have modifiers public

// etc all are assumed public

// cannot have virtual


String Update();

String Add();

String Delete();

String Search();

String CalculateWage();
}


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

we cannot call abstract class directly, so create a child class to inherit the abstract class:

Here,

Emp_Fulltime- Child Class.

Employee- Abstract Class.



public class Emp_Fulltime : Employee
{

public Emp_Fulltime()
{
}


public override String ID
{
get

{
return id;
}
set
{
id = value;
}
}

public override String FirstName
{
get

{
return fname;
}
set
{
fname = value;
}
}

public override String LastName
{
get

{
return lname;
}
set
{
lname = value;
}
}

//common methods that are

//implemented in the abstract class

public new String Add()
{
return base.Add();
}
//common methods that are implemented


//in the abstract class

public new String Delete()
{
return base.Delete();
}
//common methods that are implemented


//in the abstract class

public new String Search()
{
return base.Search();
}
//common methods that are implemented


//in the abstract class

public new String Update()
{
return base.Update();
}

//abstract method that is different


//from Fulltime and Contractor

//therefore I override it here.

public override String CalculateWage()
{
return "Full time employee " +
base.fname + " is calculated " +
"using the Abstract class...";
}
}

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

class - Emp_fulltime2

Interface - IEmployee



public class Emp_fulltime2 : IEmployee
{
//All the properties and


//fields are defined here!

protected String id;
protected String lname;
protected String fname;

public Emp_fulltime2()
{
//


// TODO: Add constructor logic here

//

}

public String ID
{
get

{
return id;
}
set
{
id = value;
}
}

public String FirstName
{
get
{
return fname;
}
set

{
fname = value;
}
}

public String LastName
{
get
{
return lname;
}
set
{
lname = value;
}
}

//all the manipulations including Add,Delete,


//Search, Update, Calculate are done

//within the object as there are not

//implementation in the Interface entity.

public String Add()
{
return "Fulltime Employee " +
fname + " added.";
}

public String Delete()
{
return "Fulltime Employee " +
fname + " deleted.";
}

public String Search()
{
return "Fulltime Employee " +
fname + " searched.";
}

public String Update()
{
return "Fulltime Employee " +
fname + " updated.";
}

//if you change to Calculatewage().


//Just small 'w' it will raise

//error as in interface

//it is CalculateWage() with capital 'W'.

public String CalculateWage()
{
return "Full time employee " +
fname + " caluculated using " +
"Interface.";
}
}

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



public void Call_Interface()

private void InterfaceExample_Click(object sender,
System.EventArgs e)
{
try

{

IEmployee emp;

Emp_fulltime2 emp1 = new Emp_fulltime2();

emp = emp1;
emp.ID = "2234";
emp.FirstName= "Rahman" ;
emp.LastName = "Mahmoodi" ;
//call add method od the object


MessageBox.Show(emp.Add().ToString());

//call the CalculateWage method

MessageBox.Show(emp.CalculateWage().ToString());


}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}

}

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

public void call_AbstractClass()

{
Employee emp;

emp = new Emp_Fulltime();


emp.ID = "2244";
emp.FirstName= "Maria" ;
emp.LastName = "Robinlius" ;
MessageBox.Show(emp.Add().ToString());

//call the CalculateWage method


MessageBox.Show(emp.CalculateWage().ToString());

}


Source:

Codeproject: http://www.codeproject.com/KB/cs/abstractsvsinterfaces.aspx

DotnetSpider: http://www.dotnetspider.com/resources/20897-What-Difference-Between-Interface-abstract.aspx

javascript for validation

function FilterNumberInput()
{

if(57<48) returnvalue="false;" keycode="="46)" returnvalue =" true;"><97)><65) returnvalue =" false;" keycode="="46||event.keyCode="="32||event.keyCode="="95)" returnvalue =" true;" keycode ="="" returnvalue =" false;" emailpat="/^(.+)@(.+)$/" specialchars="">@,;:\\\\\\\"\\.\\[\\]"
var validChars="\[^\\s" + specialChars + "\]"
var quotedUser="(\"[^\"]*\")"
var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
var atom=validChars + '+'
var word="(" + atom + "|" + quotedUser + ")"
var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")
var matchArray=emailStr.match(emailPat)
if (matchArray==null)
{
alert("Your Email Address must be valid!")
return false
}
var user=matchArray[1]
var domain=matchArray[2]
if (user.match(userPat)==null)
{
alert("Your Email Address must be valid!")
return false;
}

var IPArray=domain.match(ipDomainPat)
if (IPArray!=null)
{
for (var i=1;i<=4;i++) { if (IPArray[i]>255)
{
alert("Your Email Address must be valid!")
return false
}
}
return true
}
var domainArray=domain.match(domainPat)
if (domainArray==null)
{
alert("Your Email Address must be valid!")

return false
}
var atomPat=new RegExp(atom,"g")
var domArr=domain.match(atomPat)
var len=domArr.length
if (domArr[domArr.length-1].length<2>3)
{
alert("The Email Address must end in a three-letter domain, or two letter country.")
return false
}
if (len<2) regexp =" /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+="&%@!\-\/]))?/" retvalue =" inputString;" ch =" retValue.substring(0," ch ="="" retvalue =" retValue.substring(1," ch =" retValue.substring(0," ch =" retValue.substring(retValue.length-1," ch ="="" retvalue =" retValue.substring(0," ch =" retValue.substring(retValue.length-1," retvalue =" retValue.substring(0," ndecimal =" 0;" xtxt="textValue.value;" xtxt =" document.getElementById(" txtlen=" xTxt.length;" i =" 0;" x =" xTxt.substr(i," x ="="" ndecimal =" nDecimal"> 1)
{
alert("You have entered more than one decimal point!\nPlease only enter one!");
textValue.value="";
textValue.focus();
return false;
}
}
}

function IgnoreDecimal(textValue)

{
var nDecimal = 0;
var i;
var x;
var xTxt=textValue.value;
//var xTxt = document.getElementById("ctl00_ContentPlaceHolder1_txtBasicDuty").value;
var txtLen= xTxt.length;
for(i = 0; i < x =" xTxt.substr(i," x ="="" ndecimal =" nDecimal"> 0)
{
alert("You have entered decimal point!\nPlease ignore decimal point!");
textValue.value="";
textValue.focus();
return false;
}
}
}

CreditCard validation
---------------------------

public bool IsCreditCardValid(string cardNumber)
{
const string allowed = "0123456789";
int i;

StringBuilder cleanNumber = new StringBuilder();
for (i = 0; i < cardNumber.Length; i++) { if (allowed.IndexOf(cardNumber.Substring(i, 1)) >= 0)
cleanNumber.Append(cardNumber.Substring(i, 1));
}
if (cleanNumber.Length <> 16)
return false;

for (i = cleanNumber.Length + 1; i <= 16; i++) cleanNumber.Insert(0, "0"); int multiplier, digit, sum, total = 0; string number = cleanNumber.ToString(); for (i = 1; i <= 16; i++) { multiplier = 1 + (i % 2); digit = int.Parse(number.Substring(i - 1, 1)); sum = digit * multiplier; if (sum > 9)
sum -= 9;
total += sum;
}
return (total % 10 == 0);
}

--------------
public static bool IsDate(string strdt)
{
try
{
if (strdt.Trim() != "")
{
DateTime dt = Convert.ToDateTime(strdt);
}
return true;
}
catch
{
return false;
}
}


public static bool IsNumeric(string numberString)
{
try
{
if (numberString.Trim() != "")
{
double num = Convert.ToDouble(numberString);
}
return true;
}
catch
{
return false;
}
}