Sunday, September 14, 2008

Passing reference type ByVal or ByRef.

In .net framework there is two type set. One is reference type and another one is value type.

This post is about passing reference type by val or byref.

1. Passing ByVal.

Example:
class A
{
int i;
}


class bMain
{
public static void main()
{
A var = new A();
var.i = 10;
Console.WriteLine("Before Pass{0}",var.i);
TestFun(var);
Console.WriteLine("After Pass{0}",var.i);
}
public static void TestFun(A a)
{
a.i = 20;
}
}

output:
Before Passs : 10
After Pass: 20

Here you can see default behavior of reference type. When reference type pass by value then you can change member of that variable. But what happen when you try to reassign variable it self.

Please following code For TestFun

public static void TestFun(A a)
{
a = new A();
a.i = 20;
Console.WriteLine("Inside Fun: {0}",a.i);
}

Output:
Before Pass : 10
Inside Fun: 20
After Pass: 10

Because when reference type pass by value not actual reference is passed. Instead of that copy of reference is passed. When you try to update that parameter inself it will not take affect outside scope of function.

2. Passing By Ref.

class A
{
int i;
}


class bMain
{
public static void main()
{
A var = new A();
var.i = 10;
Console.WriteLine("Before Pass{0}",var.i);
TestFun(var);
Console.WriteLine("After Pass{0}",var.i);
}
public static void TestFun(ref A a)
{
a.i = 20;
}
}

output:
Before Pass : 10
After Pass :20

Now we update reference itself.
public static void TestFun(ref A a)
{
a = new A();
a.i = 20;
Console.WriteLine("Inside Fun {0}",a.i);
}
Output:
Before Pass: 10
Inside Fun: 20
After Pass: 20

Here actual reference is passed. So it will take affect outside function too.

You can get more detail at following location:
http://msdn.microsoft.com/en-us/library/0f66670z(vs.71).aspx#vclrfpassingmethodparameters_referencetypes

Tuesday, September 9, 2008

Bind Generic List To DataBound Control.

Suppose you have class called Person that represent person.
(This is specific to ASP.net 3.5 , C# 3.0)

public class Person
{
public Person()
{
this.Name = "";
this.Email = "";
}

public string Name
{
get;
set;
}
public string Email
{
get;
set;
}
}

Generic feature available in .NET Framework 2.0 onwards. So you can use that to create generic list.

Here we create List of Person using following code.

System.Collections.Generic.List lst =
new System.Collections.Generic.List()
{
new Person{Name = "XYZ" , Email = "xyz@xyz.com"},
new Person{Name = "123" , Email = "123@xyz.com"}
};

Bind to GridView.

GridView1.DataSource = lst;
GridView.DataBind();


Bind To ListBox

Listbox1.DataSource = lst;
Listbox1.DataBind();

Binding with gridview works perfect and display Name and Email as column. Same is not happen with ListBox.

ListBox Display item something like

Person
Person But it not display anything.

For that you have to do following thing.

ListBox1.DataSource =lst;
ListBox1.DataTextField = "Name";
ListBox1.DataValueField = "Email";
ListBox1.DataBind();

So It display name and ListBox1.SelectedValue contain appropiate Eamil Id.

Monday, September 8, 2008

Prevent site from IFRAME

Sometime it is neccessary to prevent site or perticular page opening in IFRAME. To do that use following code in page or site. (Someway every page on site).

<script language="javascript">
if(top != self)
top.location = self.location;
</script>

If some page written above code and still you want to open that page in IFRAME. You need to use following syntax. This is only supported in IE.

<iframe src="write your url that contain above code." width="500" Height="500" security="restricted">

If any one have firefox solution for this please inform me.