473,385 Members | 2,269 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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.CreateInstance("TypeName");
ITest objITest =(ITest) obj;

It is giving error explicit typecasting not allowed

can any body help on this

Nov 15 '05 #1
6 9392
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
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

thanxs

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 15 '05 #4
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Terje Slettebų | last post by:
Hi. I'm new here, and sorry if this has been discussed before; I didn't find it searching the PHP groups. (I've also read recommendations to cross-post to the other PHP groups, but if that is...
5
by: Tongu? Yumruk | last post by:
I have a little proposal about type checking in python. I'll be glad if you read and comment on it. Sorry for my bad english (I'm not a native English speaker) A Little Stricter Typing in Python...
4
by: Jacob Jensen | last post by:
This question has probably been asked a million time, but here it comes again. I want to learn the difference between the three type cast operators: static_cast, reinterpret_cast, dynamic_cast. A...
8
by: Chris Smith | last post by:
Experience posters, I am an experienced vb/vb.net developer but having a bit of trouble converting a bit of code to C#. I have 3 projects in one solution. Trying to create a plug-in type...
10
by: Bob | last post by:
This has been bugging me for a while now. GetType isn't availble for variables decalred as interface types, I have to DirectCast(somevariable, Object). In example: Sub SomeSub(ByVal...
7
by: Martin Robins | last post by:
I am currently looking to be able to read information from Active Directory into a data warehouse using a C# solution. I have been able to access the active directory, and I have been able to return...
15
by: shuisheng | last post by:
Dear All, Assume I have a class named Obj. class Obj { }; And a class named Shape which is derived from Obj. class Shape: public Obj
2
by: pallav | last post by:
I'm using an old sparse matrix C library in my C++ code and I'd like to know how to downcast a boost::shared_ptr to char *. The sparse matrix data structure is like this: struct...
0
by: Jeff Louie | last post by:
Robert.. You can use interfaces or an abstract base class to abstract out the functionality of the plug ins. You then program to the abstraction. In the case of an interface, you program to the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.