473,386 Members | 1,598 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,386 software developers and data experts.

OO Upcasting issue

I haven't been in OO all that long but ...
I would like to upcast to a new object type and add functionality
(methods only).
The runtime cast opertion fails in the sample below. Is there a way
to do it explicitly? Conceptually it is straightforward in cases such
as this where no data is added or shielded from the base class in the
derived class.

(This is only a sample. I realize that the method could be put in the
base class but the actual scenario involves a base class from the .NET
framework)

public class Props
{
public int NumberB;
public int NumberC;
}// public class Props

public class CustomProps : Props
{
public int BxC()
{
return NumberB * NumberC;
}
}

Props prop = new Props();
prop.NumberB = 4;
prop.NumberC = 5;

//this fails
CustomProps custprop = (CustomProps)prop;
int result = custprop.BxC ();
Nov 13 '05 #1
1 5321
100
Hi Fred,
I'm not aware of any oo languages, which can do that. You cannot upcast in
this case because run-tyme type of the obejcts is Props not CustomProps.
Speeking of c# you cannot even overload the cast operation in this case
because upcast and downcast are predifined casting operation and they cannot
be overloaded.

I just came up with this idea. It si not exactly what you want, though.
Anyway, it can lead you to a better solution.

public class Props
{
public int NumberB;
public int NumberC;

public Props(int B, int C)
{
NumberB = B;
NumberC = C;
}
}// public class Props

public class CustomProps : Props
{
public int BxC()
{
return NumberB * NumberC;
}

public CustomProps(int B, int C): base(B, C)
{
}
}

public class CustomPropsUpcastHelper
{
private Props props;
public CustomPropsUpcastHelper(Props p)
{
props = p;
}

public static implicit operator CustomProps(CustomPropsUpcastHelper
helper)
{
return new CustomProps(helper.props.NumberB, helper.props.NumberC);
}

public static explicit operator CustomPropsUpcastHelper(Props props)
{
return new CustomPropsUpcastHelper(props);
}
}
and you can use this like:

Props props = new Props(10, 20);
CustomProps customProps = (CustomPropsUpcastHelper)props;
Console.WriteLine(customProps.BxC());
Console.ReadLine();

HTH

B\rgds
100
"Fred." <fw*****@VenturiPartners.com> wrote in message
news:70**************************@posting.google.c om...
I haven't been in OO all that long but ...
I would like to upcast to a new object type and add functionality
(methods only).
The runtime cast opertion fails in the sample below. Is there a way
to do it explicitly? Conceptually it is straightforward in cases such
as this where no data is added or shielded from the base class in the
derived class.

(This is only a sample. I realize that the method could be put in the
base class but the actual scenario involves a base class from the .NET
framework)

public class Props
{
public int NumberB;
public int NumberC;
}// public class Props

public class CustomProps : Props
{
public int BxC()
{
return NumberB * NumberC;
}
}

Props prop = new Props();
prop.NumberB = 4;
prop.NumberC = 5;

//this fails
CustomProps custprop = (CustomProps)prop;
int result = custprop.BxC ();

Nov 13 '05 #2

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

Similar topics

2
by: lawrence | last post by:
Lets suppose I have an class called DatastoreSelect. It has a method called getNextRow(). This method is not abstract, it is fully implemented. It also has a method called setInfoToBeSought, which...
2
by: Judith | last post by:
Hi there everyone I've got: class A { ... }; class B : public A { ... }
7
by: George Hester | last post by:
Please take a look at this google artcle: http://groups.google.com/groups?hl=en&lr=&frame=right&th=55d6f4b50f5f9382&seekm=411f370d%241%40olaf.komtel.net#link9 The op was having trouble with...
4
by: juli jul | last post by:
Hello, I did the following with Class A which is a parent and B inherits from it: B b1=new B(); A a1=new A(); a1=b1; Is it an upcasting in c#? Thank you!
4
by: Amod | last post by:
I want to call a child class constructor using the base class instance ... whats the xact mechanism to perform this function ? Regards, Amod
5
by: Mark | last post by:
I can't seem to get this subclass to upcast to a baseclass... see comments below #include <cstdlib> #include <iostream> #include "Base.h" #include "Sub1.h" using namespace std;
3
by: mati-006 | last post by:
Hi, I think the code will be the best way to explain what I mean: #include "arglib/arg_shared.h" class base { public: base() {} virtual ~base() {} };
4
by: majsta | last post by:
Hello, I have the following code. #include <vector> #include <iostream> class Foo { public: Foo(){} virtual void print() const { std::cout << "foo" << std::endl;} };
0
by: Juha Nieminen | last post by:
If I'm not mistaken, general OOP wisdom says that upcasting should usually be avoided if possible. I have a situation, however, where I can't think of a better way than upcasting for this specific...
13
by: SAL | last post by:
Hello, I'm trying to include a popup in the ItemTemplate of a gridview row. The ItemTemplate for the field contains a textbox and when the user clicks in the textbox I want a popup panel to show...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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
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,...

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.