Friday, May 22, 2009

Indexer

Defining an indexer allows you to create classes that act like "virtual arrays." Instances of that class can be accessed using the [] array access operator. Defining an indexer in C# is similar to defining operator [] in C++, but is considerably more flexible. For classes that encapsulate array- or collection-like functionality, using an indexer allows the users of that class to use the array syntax to access the class.

Example

The following example shows how to declare a private array field, myArray, and an indexer. Using the indexer allows direct access to the instance b[i]. The alternative to using the indexer is to declare the array as a public member and access its members, myArray[i], directly.

// cs_keyword_indexers.cs
using System;
class IndexerClass
{
private int [] myArray = new int[100];
public int this [int index] // Indexer declaration
{
get
{
// Check the index limits.
if (index <>= 100)
return 0;
else
return myArray[index];
}
set
{
if (!(index <>= 100))
myArray[index] = value;
}
}
}

public class MainClass
{
public static void Main()
{
IndexerClass b = new IndexerClass();
// Call the indexer to initialize the elements #3 and #5.
b[3] = 256;
b[5] = 1024;
for (int i=0; i<=10; i++)
{
Console.WriteLine("Element #{0} = {1}", i, b[i]);
}
}
}

Abstract Class in OOPs

Hi,

i find so many times developers confusing with the term Abstract or abstraction in OOPS. Here's a perfect description and definition of abstraction in OOPs. Hope it will help u better understanding of Abstract and OOPs.

Abstraction
Abstraction is another good feature of OOPS. Abstraction means to show only the necessary details to the client of the object. Do you know the inner details of the Monitor of your PC? What happen when you switch ON Monitor? Does this matter to you what is happening inside the Monitor? No Right, Important thing for you is weather Monitor is ON or NOT. When you change the gear of your vehicle are you really concern about the inner details of your vehicle engine? No but what matter to you is that Gear must get changed that’s it!! This is abstraction; show only the details which matter to the user.
Let’s say you have a method "CalculateSalary" in your Employee class, which takes EmployeeId as parameter and returns the salary of the employee for the current month as an integer value. Now if someone wants to use that method. He does not need to care about how Employee object calculates the salary? An only thing he needs to be concern is name of the method, its input parameters and format of resulting member, Right?
So abstraction says expose only the details which are concern with the user (client) of your object. So the client who is using your class need not to be aware of the inner details like how you class do the operations? He needs to know just few details. This certainly helps in reusability of the code.
As I have generally seen developers are not very much comfortable with the database programming. Let’s say you are designing a class that is used to interact with the database and to perform some of database operations. Now client of your class need not to be aware of database programming, he just need to be aware of some of the details of your class and easily can perform the database operations exposed by your class without deep knowledge of database programming.
The best thing of abstract is that this decouples the user of the object and its implementation. So now object is easy to understand and maintain also. As if there is any change in the process of some operation. You just need to change the inner details of a method, which have no impact on the client of class.



============================




Abstraction means that you have some class that is more common than others that extend it. By example, if you have classes Triangle and Rectangle:
class Triangle
{
public double a;
public double b;
public double c;

public double Area
{
get { return triangle's area }
}
}

class Rectangle
{
public double a;
public double b;

public double Area
{
get { return rectangle's area }
}
}
These classes have something in common - they have property called Area plus also sides (a and b) but I don't see any reason to make sides abstract. Let's add also Circle and we have no point of sides at all. Now let's generalize these classes and let's create one more common class called Shape. Also, let's make it abstract class so this class cannot be created separately - it can be created only through extending.
public abstract class Shape
{
public double Area();
}
And now let's write previous classes so they extend Shape class.
class Triangle : Shape
{
public double a;
public double b;
public double c;

public double Area
{
get { return triangle's area }
}
}

class Rectangle : Shape
{
public double a;
public double b;

public double Area
{
get { return rectangle's area }
}
}
So, what's the win, you may ask? Okay, not of these classes use Shape as their base class. So does Circle. In the context where we don 't care about specific properties of object we can handle all extended objects as Shape. By example, let's calculate total area of Shapes in list.
List shapes = ListShapes() // contains circles, triangles and rectangles
double area = 0;

foreach(Shape shape in shapes)
area += shape.Area;

// do something useful with area here
Without extending Shape class we should write separate loop for each shape type. And also we have to have separate methods to get different types of shapes. Now we had only one method to list shapes and one loop to handle them as one - we didn't cared about lengths of sides or radiuses or any other specific properties. Same way you can make many other abstractions. By example, you can add coordinates to Shape class if shape's positioning is required.




========================


Abstraction in the object oriented world generalizes the characteristics and behaviours. The following classes describe this

public class Mammal

{

public string m_color; // Abstract Charactersistic 1

public string m_height; // Abstract Charactersistic 2

public string m_weight; // Abstract Charactersistic 1

public Mammal()

{

}

public void Move()

{

// Abstract behaviour

}

}

// Elephant is a Mammal hence it extends abstract class Mammal

public class Elephant : Mammal

{

public string m_color = "gray"; // Gray color

public int m_height = 120; // 10 feet or 120 inches

public int m_weight = 1500; // 1500 pounds

public Elephant()

{

}

public void Move()

{

// Implement the Elephant Walks Behaviour

}

}

// A whale is also a mammal hence it extends the Mammal class

public class Whale: Mammal

{

public string m_color = "darkgray"; // Dark Gray color

public int m_height = 60; // 5 feet or 60 inches

public int m_weight = 2000; // 2000 pounds

public Whale()

{

}

public void Move()

{

// Implement the way a Whale swims

}

}

Saturday, March 21, 2009

Multiple Selection DropDownList for asp.net

Hi,

I have developed Multiple selection dropdownlist Custom Control. Below are the preview of control. This is a Custom Control which you can use in asp.net.

Features :

  • Allow multiple selection.
  • Support Design Time Data Binding.
  • Quick Search through the list.
  • Select / Unselect all items
  • Persist value after postback.
  • Multiple control can be use in the same page.

Download Here









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
















-----------
















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







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








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























Download Here




Regards
Deepak Rai
+919823138839
deep.k.rai@gmail.com




Friday, March 6, 2009

Dropdownlist width in IE

DEMO


In IE, the dropdown-list takes the same width as the dropbox (i hope i am making sense) whereas in Firefox the dropdown-list's width varies according to the content.

This basically means that i have to make sure that the dropbox has to be wide enough to display the longest selection possible. this makes my page look very ugly :(

Is there any workaround for this problem? How can i uses CSS to set different widths for dropbox and the dropdownlist?




Creating your own drop down list is more of a pain than it's worth. You can use some javascript to make the IE drop down work.

Check out this solution here: http://www.hedgerwow.com/360/dhtml/ui_select_with_fixed_width/demo.php

http://www.hedgerwow.com/360/dhtml/ui_select_with_fixed_width/ie-select-width-fix.js

It uses a bit of the YUI library and a special extension for fixing IE select boxes.

You will need to include the following and wrap your elements in a

Put these before the body tag of your page:


<script src="http://us.js2.yimg.com/us.js.yimg.com/lib/common/utils/2/yahoo_2.0.0-b3.js" type="text/javascript">
script>
<script src="http://us.js2.yimg.com/us.js.yimg.com/lib/common/utils/2/event_2.0.0-b3.js" type="text/javascript">
script>
<script src="http://us.js2.yimg.com/us.js.yimg.com/lib/common/utils/2/dom_2.0.2-b3.js" type="text/javascript">
script>
<script src="ie-select-width-fix.js" type="text/javascript">
script>
<script>
// for each select box you want to affect, apply this:
var s1 = new YAHOO.Hack.FixIESelectWidth( 's1' ); // s1 is the ID of the select box you want to affect
script>


Creating your own drop down list is more of a pain than it's worth. You can use some javascript to make the IE drop down work.

Check out this solution here: http://www.hedgerwow.com/360/dhtml/ui_select_with_fixed_width/demo.php

http://www.hedgerwow.com/360/dhtml/ui_select_with_fixed_width/ie-select-width-fix.js

It uses a bit of the YUI library and a special extension for fixing IE select boxes.

You will need to include the following and wrap your elements in a

Put these before the body tag of your page:

<script src="http://us.js2.yimg.com/us.js.yimg.com/lib/common/utils/2/yahoo_2.0.0-b3.js" type="text/javascript">
script>
<script src="http://us.js2.yimg.com/us.js.yimg.com/lib/common/utils/2/event_2.0.0-b3.js" type="text/javascript">
script>
<script src="http://us.js2.yimg.com/us.js.yimg.com/lib/common/utils/2/dom_2.0.2-b3.js" type="text/javascript">
script>
<script src="ie-select-width-fix.js" type="text/javascript">
script>
<script>
// for each select box you want to affect, apply this:
var s1 = new YAHOO.Hack.FixIESelectWidth( 's1' ); // s1 is the ID of the select box you want to affect
script>

Post acceptance edit:

You can also do this without the YUI library and Hack control. All you really need to do is put an onmouseover="this.style.width='auto'" onmouseout="this.style.width='100px'" (or whatever you want) on the select element. The YUI control gives it that nice animation but it's not necessary. This task can also be accomplished with jquery and other libraries (although, I haven't found explicit documentation for this)

-- amendment to the edit: IE has a problem with the onmouseout for select controls (it doesn't consider mouseover on options being a mouseover on the select). This makes using a mouseout very tricky. The first solution is the best I've found so far.

Exporting Euro symbol in excel from asp.net.

I was working with exporting GridView to Excel. GridView contains one column that has currency symbols. After exporting to excel all currency were displaying correctly but Euro() symbols got converted to something other characters like this "รข‚¬" . After searching on internet i got solution. below is the solution.


public void ExportExcel(string filename, GridView gv)
{
Response.ClearContent();
Response.AddHeader("content-disposition", "attachment; filename=" + filename + ".xls");
Response.ContentType = "application/vnd.ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
gv.RenderControl(htw);
string output = sw.ToString();
//////////////////////
byte[] euroByte = new byte[1];
euroByte[0] = (byte)128;
bool isAfterFirst = false;
foreach (string item in output.Split('€'))
{
if (isAfterFirst)
{
Response.OutputStream.Write(euroByte, 0, 1);
}
Response.Write(item);
Response.Flush();
isAfterFirst = true;
}
Response.Write("\r\n");
//////////////////////
Response.End();
}

Wednesday, March 4, 2009

Count the record returned by SQLDataSource

protected void SqlDataSource3_Selected(object sender, SqlDataSourceStatusEventArgs e)
{
GVNO.Text = "0";
string gridViewTotalRowCount = e.AffectedRows.ToString();
GVNO.Text = gridViewTotalRowCount;

}