using delegates and events in c#

. Thursday, April 7, 2011
3 comments


What is a delegate ? 
Delegate is a function pointer means it can point to those function which are having identical signature with delegate.
you can also call delegate as a interface of methods as this acts same as interface in c# but that implemented for classes and this is implemented for methods.

Process to create a delegate:
1)declaration
2)instantiation
3)invocation

Example :

Consider the following code:

  delegate string StrDelegate();//declaration
   public class Custom
    {
        public string strmethod()
        {
            return "This is string ";
        }
    }
    class Program
        {
        static void Main(string[] args)
        {
            Custom objcustom = new Custom();
            StrDelegate strdelegate = new  StrDelegate(objcustom.strmethod); //instantiation
            string message=strdelegate(); //invocation
            Console.Write(message);
            Console.Read();
        }
       }
so what happened in this above code,well first we define a delegate with its signature,in this case its return type is string and no arguments. then we create instance of delegate which is strdelegate now this can point to (reference to) all those function which having same signature as in our case function is strmethod() and at the last we invoke this and receive this in message.

Advantages:
The main advantage of delegate is multi cast.
you can call number of function with matching singature with single instance of delegate but point to remember is that its return type should be void because there are many

methods and if we pass return type then each one will return some value and finally delegate instance will hold the last one method's value.

example

in our previous example i added the following :

        public void strmethodmulticast()
        {
           console.write("This is string multicast");

        }

in set of methods and

StrDelegate strdelegate = null;
            strdelegate += new StrDelegate(objcustom.strmethod);
            strdelegate += new StrDelegate(objcustom.strmethodmulticast);

this in Static Void Main

also changed the return type of delegate to void.


What are events 
event is a way for a class to provide notifications to clients of that class when some interesting thing happens to an object of that class.
you can also consider events as variable type of delegates ? why lets look at event declaration.

public event StrDelegate customevent;

Just need to put event keyword before delegate name and your event created.
events are handled by delegates which are called event handlers.

consider the following code:

 btnsubmit.Click+=new EventHandler(custommethod);

I created a button on form and its object 'btnsubmit' now avaiable on code behind as you know, now when click event raises in above it is being handled by EventHandler
delegate and method  'custommethod'  do the desire operation which is having the same signature as in EventHandler delegate (object sender//to raise event,Eventargs e// to receive other information)

public void custommethod(object sender, EventArgs e)
    {
        txtsubmit.Text = "This is called on button click";

    }

and hence on click event of button the above method is called which changes the text of txtsubmit.

dropdown not working in ie7 and ie8

. Tuesday, April 5, 2011
0 comments

If you have created dropdownlist and either its not visible or not visible onchange event of other dropdown
Then one of possible solution this is as follows:

Possible Cause :
if you use firebug and see html for dropdown you will notice that 'option' is not well formed in html for this dropdown.

One of Solution to Resolve this Problem:

If you have used something like this

for (i = 1; i <= day_length; i++)
{

doc.innerHTML += "<OPTION value=\"" + i + "\">" + i + "</OPTION>";
}
and adding items to dropdown (doc) dynamically with some values(eg: i in this case) replace this with


addOption(doc, i, i);

function addOption(selectbox,text,value )
{
var optn = document.createElement("OPTION");
optn.text = text;
optn.value = value;
selectbox.options.add(optn);
}

this add option adds all of those values which you want (eg: i ) and this will event work in ie 7 ,ie8.

It worked for me hope same would for you..
Cheers...

Error 3205 Too Many Backup Devices Specified.

. Thursday, March 31, 2011
0 comments

Cause:
This error occured because of you sqlserver is not compatiable with current backup version



Solution:

1)first run SELECT @@version and find the version

2)exit from current sqlserver and connect with other server for which your backup file(.bak) is compatiable.

RouteExistingFiles and IgnoreRoute in asp.net mvc

. Thursday, March 24, 2011
0 comments


How to Use RouteExistingFile and IgnoreRoute in asp.net mvc.

RouteExistingFile is a property of Routecollection class Which is True or false.
eg: routes.RouteExistingFiles = true

as you can see all css have been acting as route and hence no longer available for Home/
whenever you write this it routes the files present on your harddisk which in turns acts as a route.
for example if you have css in /content/file.css and you make RouteExistingFiles true then it will match
{controller}/{action} url for file.css and this will no longer available for existing pages.

Only two cases are possible to get file.css
1)if you directly hit that file eg:/content/file.css
2)if you use routes.Ignore route.

Case 1 is quite simple

Case 2 routes.IgnoreRoute is and extended method which accepts routes to be ignored and below will
ignore the route matching for css.
eg:
routes.IgnoreRoute("{Content}/{file.css}")
as shown in above figure now those files present on hard drive not acting as route and available for pages.

Thanks friends see you soon

Avoid css,Html errors-Asp.net

. Sunday, March 13, 2011
0 comments

Do you receiving css+ html errors when compiling asp.net application,and want to remove these validation errors its quite easy do following:

Goto Tools>options>css specific now uncheck the show errors
&      Tools>options>validations uncheck show errors for (netscape7,opera7,ie6) from dropdown.


Thanks

Interesting role of Polymorphism,Interface in C#

. Wednesday, February 23, 2011
2 comments

 Hi Guys This post is about implementing poylmorphism in c# and role of interface in c#

Polymorphism

Why?

Lets see by an example

public class Abstract
    {
    public  void  myfunction()
    {
    Console.Write("This is Abstract");
    }
  
}
 public class Inherited : Abstract
    {
        public override void myfunction()
        {
        Console.Write("This is Function Class");  
        }
}

 class Program
    {  
    
        static void Main(string[] args)
        {
            Abstract Abs = new Abstract();
            Abs = new Inherited();
            Abs.myfunction();
            Console.Read();
          }
  }

Now when you call this you will see the following  Output
"This is Function Class"

But with a warning:
Warning 1
'TestConsole.Function.myfunction()' hides inherited
member 'TestConsole.Abstract.myfunction()'. Use the
new keyword if hiding was intended. C:\Documents
and Settings\vivek\My Documents\Visual Studio
2008\Projects\TestConsole\TestConsole\Program.cs
23 22 TestConsole

which stated that if you inherit child class from parent class with having function of same signature the child
class function will be hided by parent class function.To avoid this hide of child class function by parent
class we need to use polymorphism.as follows:
in the abstract class use the following signature of function:
public virtual void  myfunction()

in the inherited class use the following singature of function:
public override void myfunction()

and now if you instantiate any of the class with abstract class's object it will override by inherited class function.

What is a interface in c# lets try it with example
I create a interface as follows:

interface Iabstract
    {
         void myfunction();
  
    }

Now i implement this with Inherited class as follows:
public class Inherited : Abstract, Iabstract
    {
        public override void myfunction()
        {
        Console.Write("This is Function Class");  
        }
    }

and call an instance of interface which references to inherited class in Main() as follows:
Iabstract objabc = new Inherited();

What should be the output?

"This is Function Class"

if you even remove the polymorphism mechanism from this still the interface instance will show the same output.

Why is it so? because interface is contract which calls functions of those classes on which its being implemented neither more nor less than that.

Hope you will enjoy this article...Thanks

425 data connection Error

. Monday, February 21, 2011
0 comments


If you stuck in issues like
*425 data connection can not be opened
*failed to display directory listing
or if you are unable to connect with ftpserver from ftpclient then try the following solution it will work.
1)Open settings from ftp server
2)then go into passive mode settings in left pane.
3)Now if radiobutton of default is checked then check
the next radio button to this asking for Insert the IP


Address and insert the localhost address(127.0.0.1) as shown above
4)Now click on Ok
5)your settings has been updated now
6)Now goto you ftp client and connect and it will work
Enjoy....

Note: This is understood that you have already configure users in ftp server and using its credentials and server ip address from your ftp client to connect.