473,404 Members | 2,187 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,404 software developers and data experts.

issue type casting derived object to base object

Hi all,

I have the following objects and namespaces

namespace MyNameSpace.Borrowers
{
public class Borrower()
{

}
}
namespace MyNameSpace.NewSystem.Borrowers
{
public class Borrower() : MyNameSpace.Borrowers.Borrower
{

}
}
Now I have the following code:

MyNameSpace.NewSystem.Borrowers.Borrower derivedObject = some value...

MyNameSpace.Borrowers.Borrower baseObject = (derivedObject as
MyNameSpace.Borrowers.Borrower)
I am getting an issue because the variable baseObject still points to an
instance of MyNameSpace.NewSystem.Borrowers.Borrower instead of
MyNameSpace.Borrowers.Borrower.

Am I missing something? Any ideas?

TIA!
Sep 7 '07 #1
2 1606
>MyNameSpace.NewSystem.Borrowers.Borrower derivedObject = some value...
>
MyNameSpace.Borrowers.Borrower baseObject = (derivedObject as
MyNameSpace.Borrowers.Borrower)
A cast shouldn't be required. The compiler can figure out that a
direct assignment is valid by looking at the inheritance hierarchy.

>I am getting an issue because the variable baseObject still points to an
instance of MyNameSpace.NewSystem.Borrowers.Borrower instead of
MyNameSpace.Borrowers.Borrower.

Am I missing something? Any ideas?
What kind of "issue"? Casting doesn't change the type of the object,
only which subset of its members you have access to.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Sep 7 '07 #2
<pa***@community.nospamwrote:

<snip>
Now I have the following code:

MyNameSpace.NewSystem.Borrowers.Borrower derivedObject = some value...

MyNameSpace.Borrowers.Borrower baseObject = (derivedObject as
MyNameSpace.Borrowers.Borrower)
I am getting an issue because the variable baseObject still points to an
instance of MyNameSpace.NewSystem.Borrowers.Borrower instead of
MyNameSpace.Borrowers.Borrower.

Am I missing something? Any ideas?
Well, for a start you should *really* try not to use the same class
name in multiple places, particularly not in the same inheritance
hierarchy. That's a recipe for confusion.

Now, as for your question: casting and using "as" don't change the type
of the object. They just let you cast down so that when you know more
than the compiler (e.g. "I know that variable x is of type object, but
actually the value is a reference to a string") you can give that
information.

You can't change the actual type of an object.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Sep 7 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Jakob Bieling | last post by:
Hi, Suppose I have a base class and a derived class as follows: class base { public: void somefunc (); };
7
by: Joseph Cook | last post by:
I was wondering if there was any way to do the following: If I have a class Base, and multiple Derived classes which all have some different behavior on some function foo()...is there any good...
7
by: Santi | last post by:
I have two classes: Product and Fruit which inherits from Product. If I try to narrow the reference to the base type by a cast, I always get a reference to the inherited type. For example: ...
5
by: Chris Capon | last post by:
Is there any way to cast a base class object to a derived class datatype when the derived class adds no new fields nor alters the object allocation in any way? The scenario would be where you...
23
by: René Nordby | last post by:
Hi there, Is there anyone that knows how to do the following? I have a class A and a class B, that 100% inherits from class A (this means that I don't have other code in class B, than...
2
by: Curtis Ransom | last post by:
I have a base class, call it house, and two inherited classes: townhome and condo. I would like to write a function that could return either a townhome or a condo. Here is how I am trying to...
11
by: Frederic Rentsch | last post by:
Hi all, If I derive a class from another one because I need a few extra features, is there a way to promote the base class to the derived one without having to make copies of all attributes? ...
6
by: Generic Usenet Account | last post by:
I ran a small experiment involving RTTI and dynamic casting. Basically what I did was to cast a base class pointer to a derived type (yes, I know that is not kosher). I then performed...
9
by: Taras_96 | last post by:
Hi everyone, I was experimenting with static_cast and reinterpret cast #include <iostream> struct A1 { int a; }; struct A2 { double d; }; struct B : public A1, A2
10
by: Dom Jackson | last post by:
I have a program which crashes when: 1 - I use static_cast to turn a base type pointer into a pointer to a derived type 2 - I use this new pointer to call a function in an object of the...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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,...

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.