Tuesday, July 22, 2008

Nested Classes

Some times you need to define a class only to serve other class and there are no other reasons to make it visible. The best way to implement this is through nested classes. A nested class is a class defined within the scope of another class.

Consider the following example:
public class Outer {
public Outer(int a) {
A = a;
}

public class Nested {
public void DisplayA(Outer o) {
System.Console.WriteLine(o.A);
}
}

private int A;
}


As it shows, it is entirely possible for the member of a class to be another user-defined type. If you read the above code snippet closely, you can see that the nested class' method DisplayA is trying to access the outer class private variable A. Nested classes have the advantage of access to all the members of the outer class.

The nested class in the example has a public access modifier and it can be instantiated within the scope of the outer class. For example:

Outer.Nested n1 = new Outer.Nested();

Wednesday, July 16, 2008

Creating objects in JavaScript

function myobject()
{
this.containedValue = 0;
this.othercontainedValue = 0;
this.anothercontainedValue = 0;
}

var mything = new myobject();

Tuesday, July 15, 2008

Output parameter and parameter array.

Output parameter and parameter array.

Out paramater is used for passed by reference. It is similar to ref keyword except ref requires the variable to be initialized before use.

Example
public static void Sum(int a, int b, out int c)
{
c = a + b;
}

int a;

Sum(10, 20, out a);

but params lets you to specify a method that taking invariable number of input parameters

Example

public static int Sum(params int [] Input)
{
int Total=0;
for (int i = 0; i < Input.Length; i++) Total += Input[i];
return Total;
}

int Total = Sum(100, 200, 200, 300);

Difference between passing a value object by reference and a reference object by value

Difference between passing a value object by reference and a reference object by value

Value types directly contain their data which are either allocated on the stack or allocated in-line in a structure.
Reference types store a reference to the value's memory address, and are allocated on the heap.
Reference types can be self-describing types, pointer types, or interface types.
Variables that are value types each have their own copy of the data, and therefore operations on one variable do not affect other variables. Variables that are reference types can refer to the same
object; therefore, operations on one variable can affect the same object referred to by another variable. All types derive from the System.Object base type

Friday, July 4, 2008

Active transactions for the current connection

@@TRANCOUNT

Returns the number of active transactions for the current connection.

The BEGIN TRANSACTION statement increments @@TRANCOUNT by 1. ROLLBACK TRANSACTION decrements @@TRANCOUNT to 0, except for ROLLBACK TRANSACTION savepoint_name, which does not affect @@TRANCOUNT. COMMIT TRANSACTION or COMMIT WORK decrement @@TRANCOUNT by 1.

BEGIN TRANSACTION
UPDATE authors SET au_lname = upper(au_lname)
WHERE au_lname = 'White'
IF @@ROWCOUNT = 2
COMMIT TRAN
IF @@TRANCOUNT > 0
BEGIN
PRINT 'A transaction needs to be rolled back'
ROLLBACK TRAN
END

Thursday, July 3, 2008

How to Get SQL Server Version

@@VERSION
Returns the date, version, and processor type for the current installation of SQL Server.The information returned by @@VERSION is similar to the product name, version, platform, and file data returned by the xp_msver stored procedure, which provides more detailed information.
SELECT @@VERSION

Tuesday, July 1, 2008

State Management Questions

1. What is ViewState?
ViewState allows the state of objects (serializable) to be stored in a hidden field on the page. ViewState is transported to the client and back to the server, and is not stored on the server or any other external source. ViewState is used the retain the state of server-side objects between postabacks.
2. What is the lifespan for items stored in ViewState?
Item stored in ViewState exist for the life of the current page. This includes postbacks (to the same page).
3. What does the "EnableViewState" property do? Why would I want it on or off?

It allows the page to save the users input on a form across postbacks. It saves the server-side values for a given control into ViewState, which is stored as a hidden value on the page before sending the page to the clients browser. When the page is posted back to the server the server control is recreated with the state stored in viewstate.
4. What are the different types of Session state management options available with ASP.NET?

ASP.NET provides In-Process and Out-of-Process state management. In-Process stores the session in memory on the web server. This requires the a "sticky-server" (or no load-balancing) so that the user is always reconnected to the same web server. Out-of-Process Session state management stores data in an external data source. The external data source may be either a SQL Server or a State Server service. Out-of-Process state management requires that all objects stored in session are serializable.