Type casting object type to user defined class or interface

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

    Type casting object type to user defined class or interface

    I am making a project in which i have one interface ITest,
    and a class which is implementing that interface.

    I am making object of that class using

    object obj=Activator.C reateInstance(" TypeName");
    ITest objITest =(ITest) obj;

    It is giving error explicit typecasting not allowed

    can any body help on this

  • Jon Skeet [C# MVP]

    #2
    Re: Type casting object type to user defined class or interface

    Programmer <gurjinder@bees ys.com> wrote:[color=blue]
    > I am making a project in which i have one interface ITest,
    > and a class which is implementing that interface.
    >
    > I am making object of that class using
    >
    > object obj=Activator.C reateInstance(" TypeName");
    > ITest objITest =(ITest) obj;
    >
    > It is giving error explicit typecasting not allowed[/color]

    What is the *exact* error message, and when are you getting it? Do you
    have more than one assembly involved? If so, you might be running into
    the problem described at http://www.pobox.com/~skeet/csharp/plugin.html

    --
    Jon Skeet - <skeet@pobox.co m>
    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

    If replying to the group, please do not mail me too

    Comment

    • Rashad Rivera

      #3
      Re: Type casting object type to user defined class or interface

      In this case you need to look at the work "explicit" and see that it is
      telling you not to cast it with the "(ITest)" because there is no need. Try
      this and it should work

      object obj=Activator.C reateInstance(" TypeName");
      ITest objITest = obj; // Don't use a cast for like types

      - Rashad Rivera
      Department of State/NCC

      "Programmer " <gurjinder@bees ys.com> wrote in message
      news:06d301c3db 2c$cbcd01c0$a40 1280a@phx.gbl.. .[color=blue]
      > I am making a project in which i have one interface ITest,
      > and a class which is implementing that interface.
      >
      > I am making object of that class using
      >
      > object obj=Activator.C reateInstance(" TypeName");
      > ITest objITest =(ITest) obj;
      >
      > It is giving error explicit typecasting not allowed
      >
      > can any body help on this
      >[/color]


      Comment

      • gurjinder kharbanda

        #4
        Re: Type casting object type to user defined class or interface


        thanxs



        *** Sent via Developersdex http://www.developersdex.com ***
        Don't just participate in USENET...get rewarded for it!

        Comment

        • Jon Skeet [C# MVP]

          #5
          Re: Type casting object type to user defined class or interface

          Rashad Rivera <rashadrivera@N O_____SPAM__.ho tmail.com> wrote:[color=blue]
          > In this case you need to look at the work "explicit" and see that it is
          > telling you not to cast it with the "(ITest)" because there is no need. Try
          > this and it should work
          >
          > object obj=Activator.C reateInstance(" TypeName");
          > ITest objITest = obj; // Don't use a cast for like types[/color]

          No it shouldn't. There's no way an implicit cast is going to exist from
          object to a particular interface, is there?

          --
          Jon Skeet - <skeet@pobox.co m>
          Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

          If replying to the group, please do not mail me too

          Comment

          • Rashad Rivera

            #6
            Re: Type casting object type to user defined class or interface

            There is no need. In the case you described above, you got an
            instance to the very interface you are attempting to cast too. But to
            answer your question, yes! But it depends on what language and what
            you are doing. In C++ you use the IUnknown::Querr yInterface(), in C#
            you just directly cast with the interface alone:

            namespace MyTest {
            public interface ITest {
            bool Print();
            }

            public class Test : ITest {
            public bool Print() {
            // do something
            }
            }
            }
            Test t = new Test();
            ITest i = t;
            i.Print(); // result is printed

            ....

            but in JScript 5.5 and less, you never have access to an interface
            directly:


            // assuming you registered with string named
            var t = new ActiveXObject(' MyTest.Test'); // OK
            var t2 = new ActiveXObject(' MyTest.ITest'); // ERROR, ITEST is not in
            REGISTRY

            Does this answer your question?
            - Rashad Rivera
            Department of State/NCC

            Jon Skeet [C# MVP] <skeet@pobox.co m> wrote in message news:<MPG.1a706 1a3e7069f10989e 31@msnews.micro soft.com>...[color=blue]
            > Rashad Rivera <rashadrivera@N O_____SPAM__.ho tmail.com> wrote:[color=green]
            > > In this case you need to look at the work "explicit" and see that it is
            > > telling you not to cast it with the "(ITest)" because there is no need. Try
            > > this and it should work
            > >
            > > object obj=Activator.C reateInstance(" TypeName");
            > > ITest objITest = obj; // Don't use a cast for like types[/color]
            >
            > No it shouldn't. There's no way an implicit cast is going to exist from
            > object to a particular interface, is there?[/color]

            Comment

            • Jon Skeet [C# MVP]

              #7
              Re: Type casting object type to user defined class or interface

              Rashad Rivera <rashadrivera@h otmail.com> wrote:[color=blue]
              > There is no need. In the case you described above, you got an
              > instance to the very interface you are attempting to cast too. But to
              > answer your question, yes! But it depends on what language and what
              > you are doing. In C++ you use the IUnknown::Querr yInterface(), in C#
              > you just directly cast with the interface alone:[/color]

              Yes, but you *do* need to directly cast.

              My point was that the sample code you gave (in C#) was never going to
              work, and your explanation was definitely wrong.

              If you can give *any* code (which doesn't alias "object" to some other
              type) where you can use your code:

              object obj=Activator.C reateInstance(" TypeName");
              ITest objITest = obj; // Don't use a cast for like types

              I'll be very surprised.

              --
              Jon Skeet - <skeet@pobox.co m>
              Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

              If replying to the group, please do not mail me too

              Comment

              Working...