Downcasting permits an object of a superclass type to be treated as an object of any subclass type.
Questions tagged [downcast]
589 questions
0
votes
1 answer
Android Superclass to Subclass casting
I'm using Android Studio to develop for Android.
I tried this
MarginLayoutParams mlp;
LayoutParams lpms = new LayoutParams(5, 5);
mlp = (MarginLayoutParams) lpms; //Throws Class cast exception
But it causes ClassCastException. Why?…

Kiryl Tkach
- 3,118
- 5
- 20
- 36
0
votes
4 answers
Java downcasting and is-A has-A relationship
HI,
I have a down casting question, I am a bit rusty in this area.
I have 2 clasess like this:
class A{ int i; String j ; //Getters and setters}
class B extends A{ String k; //getter and setter}
I have a method like this, in a Utility helper…

msharma
- 475
- 3
- 12
- 23
0
votes
1 answer
adding method to derived class and calling it from base pointer
Let's say I have the following class hierarchy:
class A
{
public:
virtual ~A();
};
class B : public A
{};
class AUser
{
public:
virtual ~AUser();
protected:
A* m_pA;
};
class BUser : public AUser
{};
Now I want to add a method to B…

Dvir Yitzchaki
- 487
- 4
- 13
0
votes
5 answers
Downcasting C# .NET
Here is my example:
public class Person
{
public string Name { get; set; }
}
public class Client : Person
{
public string LastName { get; set; }
}
public class Test
{
Person p = new Person();
Client c = (Client)p; //throws…

Fabio Martins
- 55
- 2
- 9
0
votes
1 answer
C++ - Finding the proper design for this
I'm writing a script interpreter and i first need to tokenize a string containing the source code. For that i've identified different things :
Identifiers (variable names) ;
Symbols (+, -, etc... including "alphabetic" operators such as "return")…

Virus721
- 8,061
- 12
- 67
- 123
0
votes
2 answers
Activity Class Downcasting
I have 2 different activities that can "instantiate" the same fragment.
Inside the fragment I need to save a reference to the container activity.
MyActivity1 extends FragmentActivity
MyActivity2 extends FragmentActivity
The reference is stored…

Jumpa
- 4,319
- 11
- 52
- 100
0
votes
4 answers
Prohibit downcasts of base class
Downcasts are a code smell. Implementing a base class; is there a way in C# to prevent the base class or interface from being downcasted by inheriting classes? The capability to derive from the base class should be preserved.
Example
interface IFoo…

ominug
- 1,422
- 2
- 12
- 28
0
votes
1 answer
Casting object of base class to derived class
I find myself in a tight spot. I am building some logic on top of an existing project given to me in a jar. Hence I don't have the ability to modify these classes.
I want to write additional methods to an existing class to make it feature rich .…

Chiseled
- 2,280
- 8
- 33
- 59
0
votes
1 answer
SWIFT - Downcast an array to a string
@IBOutlet var cityField: UITextField!
@IBOutlet var message: UILabel!
@IBAction func buttonPressed(sender: AnyObject) {
self.view.endEditing(true)
var urlString = "http://www.weather-forecast.com/locations/" +…

Abdou23
- 363
- 1
- 4
- 11
0
votes
4 answers
is implicit downcasting using new OK?
What do you think of this piece of C++ code :
Polygon* p;
if(shape=="Rectangle")
p = new Rectangle();
else if(shape=="Triangle")
p = new Triangle();
else
exit(EXIT_FAILURE);
where Rectangle and Triangle derive from a base-class…

ultrahamster
- 105
- 1
- 8
0
votes
1 answer
iOS Swift: Downcasting AnyObject
I know a number of downcasting from Cocoa to Swift questions have been asked and there are some bugs, but I have tried a lot of the methods found here and I cannot get them to work, hoping someone can lend a hand.
I am new to programming. I am…

Nate Birkholz
- 2,739
- 4
- 20
- 29
0
votes
1 answer
Entity Framework 6.1 Discriminator TPH
I am looking to implement Table-per-Hierarchy using EF6 similar to the instructions found here:
example.
I have an abstract base class of User with the following derived types:
Student
Contact
Instructor
When I examine the database table Users the…

CyberUnDead
- 147
- 5
- 14
0
votes
2 answers
How can I safely downcast this?
Edit: I got this to work (see in answers below) in VS2012, but it still doesn't properly downcast in Xcode.
I am trying to downcast from an unsigned long to an int in C++, but data loss seems inevitable:
unsigned long bigInt= randomBigNumber;
int x…

btalbot
- 167
- 9
0
votes
1 answer
Switching between different types of pointers to objects
So far I have been using dynamic casting. But this comes with it's pros and cons. It seems that is a good thing NOT to use this too much. The examples on this topic, that I have found, are usually with classes that have little differences. But in my…

AlexSavAlexandrov
- 858
- 2
- 13
- 34
0
votes
3 answers