Dynamic Instantiation

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Fariba

    Dynamic Instantiation

    Sorry if this sounds like a newbie question.

    I have two classes in my app, A & B. each class has a public method called
    "EchoTime() ". In my UI ,user chooses wether to call EchoTime() from A or B
    and then I have to dynamically instantiate the appropriate class and call tis
    method.

    1) How can I do that?

    2) Is it the concept of late binding?

    Thanks

  • gmiley

    #2
    Re: Dynamic Instantiation

    One way to do this would be to have a base class or interface that has
    EchoTime() in it:

    public class TimeEcho
    {
    public void EchoTime()
    {
    // Do something
    }
    }

    Then you could have classes A & B defined inheriting from TimeEcho:

    public class A : EchoTime
    {

    }

    public class B : EchoTime
    {

    }

    Then depending on what the user selects:

    public void DoUsersChoice(s tring className)
    {
    EchoTime userSelectedCla ss =
    (EchoTime)Type. GetType(classNa me).GetConstruc tor(new Type[] {
    }).Invoke(new object[] { });
    userSelectedCla ss.EchoTime();
    }

    Fariba wrote:[color=blue]
    > Sorry if this sounds like a newbie question.
    >
    > I have two classes in my app, A & B. each class has a public method called
    > "EchoTime() ". In my UI ,user chooses wether to call EchoTime() from A or B
    > and then I have to dynamically instantiate the appropriate class and call tis
    > method.
    >
    > 1) How can I do that?
    >
    > 2) Is it the concept of late binding?
    >
    > Thanks[/color]

    Comment

    • gmiley

      #3
      Re: Dynamic Instantiation

      Sorry those classes should have been defined as:

      public class A : TimeEcho
      {

      }

      public class B : TimeEcho
      {

      }


      gmiley wrote:[color=blue]
      > One way to do this would be to have a base class or interface that has
      > EchoTime() in it:
      >
      > public class TimeEcho
      > {
      > public void EchoTime()
      > {
      > // Do something
      > }
      > }
      >
      > Then you could have classes A & B defined inheriting from TimeEcho:
      >
      > public class A : EchoTime
      > {
      >
      > }
      >
      > public class B : EchoTime
      > {
      >
      > }
      >
      > Then depending on what the user selects:
      >
      > public void DoUsersChoice(s tring className)
      > {
      > EchoTime userSelectedCla ss =
      > (EchoTime)Type. GetType(classNa me).GetConstruc tor(new Type[] {
      > }).Invoke(new object[] { });
      > userSelectedCla ss.EchoTime();
      > }
      >
      > Fariba wrote:[color=green]
      > > Sorry if this sounds like a newbie question.
      > >
      > > I have two classes in my app, A & B. each class has a public method called
      > > "EchoTime() ". In my UI ,user chooses wether to call EchoTime() from A or B
      > > and then I have to dynamically instantiate the appropriate class and call tis
      > > method.
      > >
      > > 1) How can I do that?
      > >
      > > 2) Is it the concept of late binding?
      > >
      > > Thanks[/color][/color]

      Comment

      • Fariba

        #4
        Re: Dynamic Instantiation

        Thanks for your reply,

        Which concept of oo this approach is refering to?

        Thanks

        "gmiley" wrote:
        [color=blue]
        > Sorry those classes should have been defined as:
        >
        > public class A : TimeEcho
        > {
        >
        > }
        >
        > public class B : TimeEcho
        > {
        >
        > }
        >
        >
        > gmiley wrote:[color=green]
        > > One way to do this would be to have a base class or interface that has
        > > EchoTime() in it:
        > >
        > > public class TimeEcho
        > > {
        > > public void EchoTime()
        > > {
        > > // Do something
        > > }
        > > }
        > >
        > > Then you could have classes A & B defined inheriting from TimeEcho:
        > >
        > > public class A : EchoTime
        > > {
        > >
        > > }
        > >
        > > public class B : EchoTime
        > > {
        > >
        > > }
        > >
        > > Then depending on what the user selects:
        > >
        > > public void DoUsersChoice(s tring className)
        > > {
        > > EchoTime userSelectedCla ss =
        > > (EchoTime)Type. GetType(classNa me).GetConstruc tor(new Type[] {
        > > }).Invoke(new object[] { });
        > > userSelectedCla ss.EchoTime();
        > > }
        > >
        > > Fariba wrote:[color=darkred]
        > > > Sorry if this sounds like a newbie question.
        > > >
        > > > I have two classes in my app, A & B. each class has a public method called
        > > > "EchoTime() ". In my UI ,user chooses wether to call EchoTime() from A or B
        > > > and then I have to dynamically instantiate the appropriate class and call tis
        > > > method.
        > > >
        > > > 1) How can I do that?
        > > >
        > > > 2) Is it the concept of late binding?
        > > >
        > > > Thanks[/color][/color]
        >
        >[/color]

        Comment

        • Fariba

          #5
          Re: Dynamic Instantiation

          I created an Interface called IEchoTime and made both classes implement the
          interface. When I use your code I get "object reference not set to an
          instance of an object"

          Your help is apppreciated.

          Thanks

          "gmiley" wrote:
          [color=blue]
          > Sorry those classes should have been defined as:
          >
          > public class A : TimeEcho
          > {
          >
          > }
          >
          > public class B : TimeEcho
          > {
          >
          > }
          >
          >
          > gmiley wrote:[color=green]
          > > One way to do this would be to have a base class or interface that has
          > > EchoTime() in it:
          > >
          > > public class TimeEcho
          > > {
          > > public void EchoTime()
          > > {
          > > // Do something
          > > }
          > > }
          > >
          > > Then you could have classes A & B defined inheriting from TimeEcho:
          > >
          > > public class A : EchoTime
          > > {
          > >
          > > }
          > >
          > > public class B : EchoTime
          > > {
          > >
          > > }
          > >
          > > Then depending on what the user selects:
          > >
          > > public void DoUsersChoice(s tring className)
          > > {
          > > EchoTime userSelectedCla ss =
          > > (EchoTime)Type. GetType(classNa me).GetConstruc tor(new Type[] {
          > > }).Invoke(new object[] { });
          > > userSelectedCla ss.EchoTime();
          > > }
          > >
          > > Fariba wrote:[color=darkred]
          > > > Sorry if this sounds like a newbie question.
          > > >
          > > > I have two classes in my app, A & B. each class has a public method called
          > > > "EchoTime() ". In my UI ,user chooses wether to call EchoTime() from A or B
          > > > and then I have to dynamically instantiate the appropriate class and call tis
          > > > method.
          > > >
          > > > 1) How can I do that?
          > > >
          > > > 2) Is it the concept of late binding?
          > > >
          > > > Thanks[/color][/color]
          >
          >[/color]

          Comment

          • Guest's Avatar

            #6
            Re: Dynamic Instantiation

            Why use reflection for such a small thing?
            Cant the code be written like this?

            if (classname == "A")
            {
            A obj = new A();
            obj.EchoTime();
            }
            else
            {
            B obj = new B();
            obj.EchoTime();
            }

            HTH
            Kalpesh

            Comment

            • Fariba

              #7
              Re: Dynamic Instantiation

              It is not small thing.I made it small so people can answer me.If I follow
              what you suggest (Like what to came to my mind at the beggining) , I have to
              duplicate lines of code for each condition ,don't you think it is not as
              pretty as you do it dynamically?

              Thanks

              "<Kalpesh/>" wrote:
              [color=blue]
              > Why use reflection for such a small thing?
              > Cant the code be written like this?
              >
              > if (classname == "A")
              > {
              > A obj = new A();
              > obj.EchoTime();
              > }
              > else
              > {
              > B obj = new B();
              > obj.EchoTime();
              > }
              >
              > HTH
              > Kalpesh
              >[/color]

              Comment

              Working...