C#

Moderators: None (Apply to moderate this forum)
Number of threads: 2722
Number of posts: 5749

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Static and Dynamic binding Posted by progcsharp on 22 May 2004 at 5:27 AM
Whats the difference between static and dynamic binding? I fail to understand. Could someone please explain with an example? Thanks.

Report
Re: Static and Dynamic binding Posted by Baldusarius on 22 May 2004 at 9:20 AM
: Whats the difference between static and dynamic binding? I fail to understand. Could someone please explain with an example? Thanks.
:
:

It can all be boiled down to when the actions that take place during execution are known.

Example 1:
public class MyClass
{
   public void DoSomething(){...}
}

public class MyOtherClass
{
   public MyOtherClass()
   {
      MyClass mc = new MyClass();
      mc.DoSomething();
   }
}
The call to DoSomething was bound statically to the instance of MyClass that we had created on the previous line. This is known at compile time, and therefore is the only thing that can happen when this code runs.

Example 2:
public abstract class WidgetBase
{
   public abstract void DoSomething();
}

public class ShinyWidget : WidgetBase
{
   public override void DoSomething()
   {
      // implementation
   }
}

public class DullWidget : WidgetBase
{
   public override void DoSomething()
   {
      // implementation
   }
}

public class MyOtherClass
{
   public void DoSomethingWithAWidget(WidgetBase widget)
   {
      widget.DoSomething();
   }
}
When someone instantiates MyOtherClass, they will call DoSomethingWithAWidget and pass in an instance of ShinyWidget or DullWidget. We have no idea which one is going to be passed in, so we can't know at compile time which implementation of DoSomething is going to be called. This is dynamic binding.

The concept of static/dynamic binding is certainly not only applicable to abstract classes. This is only one example. The same concept applies to virtual methods, in many cases.
MyNonAbstractBaseClass bc = new ImInheritedFromMNABC();
bc.VirtalMethodName();
This would use dynamic binding as well.



P.S. I am looking forward to the next question in your study guide. Ever think about posting the entire list to save time?
Report
Re: Static and Dynamic binding Posted by progcsharp on 22 May 2004 at 9:45 AM
Thank you so much for the detailed answer. Yep, I am studying C#. And the study guide sucks! You guys have been terrific with clarifying questions.

Here comes my next question which I noticed in the next chapter in the guide. Is it possible to pass arguments to a class declaration? Do you have any examples? Thanks in advance.


: : Whats the difference between static and dynamic binding? I fail to understand. Could someone please explain with an example? Thanks.
: :
: :
:
: It can all be boiled down to when the actions that take place during execution are known.
:
: Example 1:
: public class MyClass
: {
:    public void DoSomething(){...}
: }
: 
: public class MyOtherClass
: {
:    public MyOtherClass()
:    {
:       MyClass mc = new MyClass();
:       mc.DoSomething();
:    }
: }
The call to DoSomething was bound statically to the instance of MyClass that we had created on the previous line. This is known at compile time, and therefore is the only thing that can happen when this code runs.
:
: Example 2:
: public abstract class WidgetBase
: {
:    public abstract void DoSomething();
: }
: 
: public class ShinyWidget : WidgetBase
: {
:    public override void DoSomething()
:    {
:       // implementation
:    }
: }
: 
: public class DullWidget : WidgetBase
: {
:    public override void DoSomething()
:    {
:       // implementation
:    }
: }
: 
: public class MyOtherClass
: {
:    public void DoSomethingWithAWidget(WidgetBase widget)
:    {
:       widget.DoSomething();
:    }
: }
When someone instantiates MyOtherClass, they will call DoSomethingWithAWidget and pass in an instance of ShinyWidget or DullWidget. We have no idea which one is going to be passed in, so we can't know at compile time which implementation of DoSomething is going to be called. This is dynamic binding.
:
: The concept of static/dynamic binding is certainly not only applicable to abstract classes. This is only one example. The same concept applies to virtual methods, in many cases.
: MyNonAbstractBaseClass bc = new ImInheritedFromMNABC();
: bc.VirtalMethodName();
This would use dynamic binding as well.
:
:
:
: P.S. I am looking forward to the next question in your study guide. Ever think about posting the entire list to save time?
:

Report
Re: Static and Dynamic binding Posted by ing_gigio on 22 May 2004 at 10:20 AM
: Thank you so much for the detailed answer. Yep, I am studying C#. And the study guide sucks! You guys have been terrific with clarifying questions.
:
: Here comes my next question which I noticed in the next chapter in the guide. Is it possible to pass arguments to a class declaration? Do you have any examples? Thanks in advance.
:
:

We just do what we can ;)

Yes, u can pass how many arguments u want to class.
When u instantiate a class the first thing the compiler does is to call the constructor of that class. If u haven't defined one yet, it will create a DEFAULT constructor following these rules:

numeric types set to 0
char type set to null character
bool set to false
reference fields set to null

U can pass arguments to a new instantiated class overriding defining a ur own constuctor and instantiating the class using the new one. Hope an example will clarify


public void class MyClass
{
int Number;
char Character;
bool TrueOrFalse;

public void MyClass(int UrNumber, char UrChar, bool UrBool)
{
Number = UrNumber
Character = UrChar;
TrueOrFalse = UrBool;

}

public void MyClass(int UrNum)
{
Number = UrNum;
}
}

public void AnotherClass
{
MyClass Bill = new MyClass();
MyClass Tom = new MyClass(12, "d", true);
MyClass Peter = new MyClass(12+3);
}


Notice that I specified two different constructors in MyClass.

In AnotherClass u instantiate 3 classes (Bill, Tom and Peter) and u use a different constructor for every one of them. What the compiler do to choose which one to select is looking at the signature (the parameters passed). What defines uniquely a signature is the order and the type of the parameters passed. (that means u cannot have two constructors passing both 2 int).

Creating Bill u call the default constructor obtaining these values:
Bill.Number = 0
Bill.Character = null character
Bill.TrueOrFalse = false

(note that u don't have to define the default constructor)

Creating Tom u call the first constructor (the one with 3 parameters) and give its variables the following values:
Tom.Number = 12
Tom.Character = d
Tom.TrueOrFalse = true

Creating Peter u use the second constructor u defined. What u have is to set the variables u explcitly chose to the value passed with the parameter and have the others set with the default constructor rules
Peter.Number = 15
Peter.Character = null character
Peter.TrueOrFalse = false

Plz note that u can pass as a parameter an expression too, which can be also of a type calculated run-time
i.e x+y
where x and y are variables u declared and assigned a value before

Keep on practicing...and maybe change manual ;)

ing_gigio
Report
Re: Static and Dynamic binding Posted by progcsharp on 22 May 2004 at 11:14 AM
As a follow up of your answer I have the following question:
Can I have a class a private member of another class?


: : Thank you so much for the detailed answer. Yep, I am studying C#. And the study guide sucks! You guys have been terrific with clarifying questions.
: :
: : Here comes my next question which I noticed in the next chapter in the guide. Is it possible to pass arguments to a class declaration? Do you have any examples? Thanks in advance.
: :
: :
:
: We just do what we can ;)
:
: Yes, u can pass how many arguments u want to class.
: When u instantiate a class the first thing the compiler does is to call the constructor of that class. If u haven't defined one yet, it will create a DEFAULT constructor following these rules:
:
: numeric types set to 0
: char type set to null character
: bool set to false
: reference fields set to null
:
: U can pass arguments to a new instantiated class overriding defining a ur own constuctor and instantiating the class using the new one. Hope an example will clarify
:
:
: public void class MyClass
: {
: int Number;
: char Character;
: bool TrueOrFalse;
:
: public void MyClass(int UrNumber, char UrChar, bool UrBool)
: {
: Number = UrNumber
: Character = UrChar;
: TrueOrFalse = UrBool;
:
: }
:
: public void MyClass(int UrNum)
: {
: Number = UrNum;
: }
: }
:
: public void AnotherClass
: {
: MyClass Bill = new MyClass();
: MyClass Tom = new MyClass(12, "d", true);
: MyClass Peter = new MyClass(12+3);
: }
:
:
: Notice that I specified two different constructors in MyClass.
:
: In AnotherClass u instantiate 3 classes (Bill, Tom and Peter) and u use a different constructor for every one of them. What the compiler do to choose which one to select is looking at the signature (the parameters passed). What defines uniquely a signature is the order and the type of the parameters passed. (that means u cannot have two constructors passing both 2 int).
:
: Creating Bill u call the default constructor obtaining these values:
: Bill.Number = 0
: Bill.Character = null character
: Bill.TrueOrFalse = false
:
: (note that u don't have to define the default constructor)
:
: Creating Tom u call the first constructor (the one with 3 parameters) and give its variables the following values:
: Tom.Number = 12
: Tom.Character = d
: Tom.TrueOrFalse = true
:
: Creating Peter u use the second constructor u defined. What u have is to set the variables u explcitly chose to the value passed with the parameter and have the others set with the default constructor rules
: Peter.Number = 15
: Peter.Character = null character
: Peter.TrueOrFalse = false
:
: Plz note that u can pass as a parameter an expression too, which can be also of a type calculated run-time
: i.e x+y
: where x and y are variables u declared and assigned a value before
:
: Keep on practicing...and maybe change manual ;)
:
: ing_gigio
:

Report
Re: Static and Dynamic binding Posted by ing_gigio on 22 May 2004 at 11:40 AM
: As a follow up of your answer I have the following question:
: Can I have a class a private member of another class?
:

what do u exactly mean with "have" ? U mean using as an external or having in its body as one of the methods?

In the first case u cannot do anything... Maybe u can use delegation...but it's somthing I'm still studyng..I'll let u know soon ;)

What I want to say is that u cannot have a class MyClass implementing a private method named for instance OnePrivateMethod and write something like this

public class User
{
MyClass Cippalippa = new MyClass();
cippalippa.OnePrivateMethod;
}

defining a method private u forbid any access to it.

The only exception to this is if the class which is calling is a derived class.
public class UserTwo : MyClass
{
}

In this case u have that UserTwo can both use the method of the parent class

public class UserTwo:MyClass
{
base.OnePrivateMethod;
}

or use the method as part of its body

public class UserTwo:MyClass
{
this.OnePrivateMethod;
}

This is because with inheritance the derived class inherites all the variables and method of the parent class and can implements some new ones in addition. (this idea behind it is to have a more easy-to-understand code and help u writing less...u don't have to write twice the same stuff! ;)
Report
Re: Static and Dynamic binding Posted by Dummy2 on 25 Oct 2010 at 2:55 AM
Check this link related to dynamic binding in C#




 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.