By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,997 Members | 1,260 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,997 IT Pros & Developers. It's quick & easy.

Type casting object type to user defined class or interface

P: n/a
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.CreateInstance("TypeName");
ITest objITest =(ITest) obj;

It is giving error explicit typecasting not allowed

can any body help on this

Nov 15 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Programmer <gu*******@beesys.com> wrote:
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.CreateInstance("TypeName");
ITest objITest =(ITest) obj;

It is giving error explicit typecasting not allowed


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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #2

P: n/a
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.CreateInstance("TypeName");
ITest objITest = obj; // Don't use a cast for like types

- Rashad Rivera
Department of State/NCC

"Programmer" <gu*******@beesys.com> wrote in message
news:06****************************@phx.gbl...
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.CreateInstance("TypeName");
ITest objITest =(ITest) obj;

It is giving error explicit typecasting not allowed

can any body help on this

Nov 15 '05 #3

P: n/a

thanxs

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

P: n/a
Rashad Rivera <rashadrivera@NO_____SPAM__.hotmail.com> wrote:
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.CreateInstance("TypeName");
ITest objITest = obj; // Don't use a cast for like types


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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #5

P: n/a
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::QuerryInterface(), 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] <sk***@pobox.com> wrote in message news:<MP************************@msnews.microsoft. com>...
Rashad Rivera <rashadrivera@NO_____SPAM__.hotmail.com> wrote:
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.CreateInstance("TypeName");
ITest objITest = obj; // Don't use a cast for like types


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

Nov 15 '05 #6

P: n/a
Rashad Rivera <ra**********@hotmail.com> wrote:
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::QuerryInterface(), in C#
you just directly cast with the interface alone:


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.CreateInstance("TypeName");
ITest objITest = obj; // Don't use a cast for like types

I'll be very surprised.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.