473,498 Members | 1,998 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem with casting

7 New Member
OK, I've not done this before so excuse me for being thick!

I have a class 'Client' which is stored in an external dll and I need to extend, just to add extra methods to it.

External.Client cl = new External.Client();

my local class, also called Client, is derived from this:

public class Client : External.Client
{
public void Save()
{
...
}
}

What I need to do is cast from an External.Client to my own local Client:

Client thiscl = (Client)cl;

but this throws a runtime error :

Unable to cast object of type 'External.Client' to type 'MyNamespace.Client'.

How can I do this?

Thanks
Sep 25 '07 #1
10 1102
r035198x
13,262 MVP
OK, I've not done this before so excuse me for being thick!

I have a class 'Client' which is stored in an external dll and I need to extend, just to add extra methods to it.

External.Client cl = new External.Client();

my local class, also called Client, is derived from this:

public class Client : External.Client
{
public void Save()
{
...
}
}

What I need to do is cast from an External.Client to my own local Client:

Client thiscl = (Client)cl;

but this throws a runtime error :

Unable to cast object of type 'External.Client' to type 'MyNamespace.Client'.

How can I do this?

Thanks
The cast won't work because the two objects are inconvertible.
Sep 25 '07 #2
Plater
7,872 Recognized Expert Expert
There is a way to do what you want, and I wish I knew it. (I've been looking for it myself in a mannor of speaking)

They do it with the various Stream objects, Image->Bitmap and WebRequest->HttpWebRequest.


What you could do as a possible work-around, is have your custom class NOT extend that class, but have a public/private instance of it, and have the constructor take in the object.
Sep 25 '07 #3
snowbunny
7 New Member
The cast won't work because the two objects are inconvertible.
Thanks. Useful.

So.... at the risk of sounding bolshy.....if all I'm doing is taking one class and extending it with methods, not properties, how *should* I make it convertible?
Sep 25 '07 #4
snowbunny
7 New Member
There is a way to do what you want, and I wish I knew it. (I've been looking for it myself in a mannor of speaking)

They do it with the various Stream objects, Image->Bitmap and WebRequest->HttpWebRequest.


What you could do as a possible work-around, is have your custom class NOT extend that class, but have a public/private instance of it, and have the constructor take in the object.
Thanks, that's more useful :)
Unfortunately, I'm in the situation where I'd have to cast from each to the other, so that's probably not going to help.

There ya go, I tought this would be a simple "oi, thickie" type thing. Someone please tell me it is!
Sep 25 '07 #5
r035198x
13,262 MVP
Thanks, that's more useful :)
Unfortunately, I'm in the situation where I'd have to cast from each to the other, so that's probably not going to help.

There ya go, I tought this would be a simple "oi, thickie" type thing. Someone please tell me it is!
I can imagine your excitement at the responses.

If B extends A, then A objects are not convertible to B objects. It's a basic inheritance rule issue.
Sep 25 '07 #6
Plater
7,872 Recognized Expert Expert
Well do you NEED to cast to it, or can you get away with just running functions on it's public functions/properties?

Because then you could just make a static class that has your new functions that also take in that original class as a parameter?


If you need at the internals, you might have to re-write those DLL functions.


Unless you can figure something cheeky out with a UNION construct?
Sep 25 '07 #7
r035198x
13,262 MVP
Well do you NEED to cast to it, or can you get away with just running functions on it's public functions/properties?

Because then you could just make a static class that has your new functions that also take in that original class as a parameter?


If you need at the internals, you might have to re-write those DLL functions.


Unless you can figure something cheeky out with a UNION construct?
Or just write a method in your class that takes the parent class object as argument and manually copies all the stuff you want to an object of your class and returns that object of your class type.
Sep 25 '07 #8
alan4cast
7 New Member
(read the problem backwards - never mind)
Sep 25 '07 #9
snowbunny
7 New Member
Or just write a method in your class that takes the parent class object as argument and manually copies all the stuff you want to an object of your class and returns that object of your class type.
Yeah, I'd thought of doing that, but figured there must be a more elegant solution.
I'm *sure* I've read somewhere that you can cast A > B *and B > A, albeit with loss of data. Oh well, back to the drawing board!
Sep 26 '07 #10
r035198x
13,262 MVP
Yeah, I'd thought of doing that, but figured there must be a more elegant solution.
I'm *sure* I've read somewhere that you can cast A > B *and B > A, albeit with loss of data. Oh well, back to the drawing board!
No you cannot. You can only cast objects if they are *convertible*.
A subclass object is convertible to an object of it's base class but not vice versa.
Sep 26 '07 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

2
2470
by: ghostdog | last post by:
hi, i got this opengl/c++ code: <code> void render(CMesh *mesh){ ... float *pVertices; int *pIndices;
17
15813
by: Jon Slaughter | last post by:
I'm having a little trouble understanding what the slicing problem is. In B.S.'s C++ PL3rdEd he says "Becayse the Employee copy functions do not know anything about Managers, only the Employee...
10
2490
by: Ben | last post by:
Hi, I am a newbie with C and am trying to get a simple linked list working for my program. The structure of each linked list stores the char *data and *next referencing to the next link. The...
14
1562
by: Jason Sewall | last post by:
Hello, I'm using the intel C++ 8.0 compiler on windows and I'm working on some software that uses the following lines of code: gf->invdim = 1.0/(double)dim; printf("invdim: %f\n",...
6
1374
by: Carlo Marchesoni | last post by:
I have an ASP.NET/C# solution, where I can perfectly cast something I stored in the session object to a class of mine (BackEnd), as this: ->be = (BackEnd)Session;<- But if I try to do the same:...
2
4425
by: ajikoe | last post by:
Hi, I tried to follow the example in swig homepage. I found error which I don't understand. I use bcc32, I already include directory where my python.h exist in bcc32.cfg. /* File : example.c...
39
19552
by: Martin Jørgensen | last post by:
Hi, I'm relatively new with C-programming and even though I've read about pointers and arrays many times, it's a topic that is a little confusing to me - at least at this moment: ---- 1)...
11
3497
by: Vinod | last post by:
Hi, I am working in the project where VC6 code is ported to VC8 (VC++ .Net 2005) I got a problem when I cast a double value to unsigned int. Problem is I couldn’t get the proper value after...
7
2185
by: Ajeet | last post by:
hi I am having some difficulty in casting using generics. These are the classes. public interface IProvider<PROF> where PROF : IProviderProfile { //Some properties/methods }
4
3004
by: Wally Barnes | last post by:
Can someone help a poor C++ programmer that learned the language before there was a standard lib .. etc ? Basically I have two classes that look something like below: template <class T>...
0
7121
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6993
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
7197
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...
0
5456
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4899
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4584
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3088
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1411
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
287
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.