Converting a variable from one type to another

learn more… | top users | synonyms

2
votes
3answers
156 views

OOD: Java inheritance and access to child methods via casting

I have several classes Parent and Child1 ... Child9 implemented in Java. Parent is an abstract class, containing all the common variables of the child classes (a lot, which is the main reason why I ...
0
votes
2answers
234 views

Optimization ended up in casting an object at each method call

I've been doing some optimization for the following piece of code : public void DrawLine(int x1, int y1, int x2, int y2, int color) { _bitmap.DrawLineBresenham(x1, y1, x2, y2, color); } After ...
1
vote
1answer
94 views

Ignoring the generic part of a type while an object is being passed

I have a message class that holds the name of a destination, and a generic variable acting as the message payload public class Message<T> { public string Destination public T Payload ...
2
votes
2answers
184 views

Having an inherited function return the derived type instead of the base type

I am writing two classes in C#: A Matrix class that represents a general Matrix with n-by-m dimensions A SquareMatrix class that inherits from Matrix and has the constraint of being n-by-n The ...
1
vote
3answers
70 views

Property-level value transformation for indirect object casting

Does any programming language exist to support the explicit, property-level object copy? For example, assume this code: public class Student { public string Name { get; set; } public ...
1
vote
3answers
196 views

Make Return Type an Interface - Problem with Initialization

I would like to make the return type of my method an interface rather than a class for similar reasons stated in c# List or IList, however I am having trouble figuring out how to initialize the ...
1
vote
2answers
179 views

A vector of pointers to different derived classes of the same base class

I am writing a game in C++ which has different enemy types. I have defined a base Enemy class and I also have derived classes enemy1, enemy2 etc. Now for updating these enemies in each iteration of ...
0
votes
2answers
766 views

Why doesn't Ruby have implicit conversion of Fixnum into String?

Ruby normally makes things easy. However, it doesn't allow implicitely converting a number to a string: 2.0.0p247 :010 > "a"+1 TypeError: no implicit conversion of Fixnum into String Why is this ...
6
votes
6answers
654 views

How to avoid downcasting?

My question is about a special case of the super class Animal. 1. My Animal can moveForward() and eat(). 2. Seal extends Animal. 3. Dog extends Animal. 4. And there's a special creature that also ...
2
votes
1answer
53 views

Handling Types for Real and Complex Matrices in a BLAS Wrapper

I come from a C background and I'm now learning OOP with C++. As an exercise (so please don't just say "this already exists"), I want to implement a wrapper for BLAS that will let the user write ...
0
votes
2answers
255 views

Question about casting a class in Java with generics

In Java 6 Class<? extends ArrayList<?>> a = ArrayList.class; gives and error, but Class<? extends ArrayList<?>> b = (Class<? extends ...
-1
votes
3answers
917 views

Is converting/casting a boolean to int a good idea? [closed]

Note: Even though this contains Python, it's not specifically about Python. It's more generally about expecting a certain behaviour from a language which is not necessarily the same across ...
6
votes
6answers
1k views

Would a “downcast if block” be a reasonable language feature?

Consider the following "if cast": class A { public void f1() { ... } } class B : A { public void f2() { ... } } A a = foo(); // might return A or B if ( a is B ) { // Inside block, ...
1
vote
1answer
49 views

Removing dependencies on subclass-specific behavior

I have a Message class which can contain multiple types of payloads (or sometimes no payload), each derived from a common Payload class. However, this becomes problematic because the Message class ...
5
votes
2answers
321 views

Is there a name for the number of values a variable can take? [closed]

For example, a bit or a boolean can be either 0 or 1 so the number 2 is associated with it. Similarly, for a byte which is 8 bits, the maximum number of different assignments would be 2^8. Is there a ...
16
votes
9answers
20k views

Type Casting variables in PHP: Is there a practical example?

PHP, as most of us know, has weak typing. For those who don't, PHP.net says: PHP does not require (or support) explicit type definition in variable declaration; a variable's type is determined ...
1
vote
1answer
258 views

When is type coercion a good idea?

When do you want (on purpose) to coerce type in JavaScript? I mean, beyond comparing strings and numbers, when does it benefit something you're building? Examples of type coercion: "The answer is " ...
16
votes
4answers
1k views

What is casting supposed to mean?

When coding in low level languages like C I find that casting sometimes means 'reinterpret these bytes as if it had always been of this other type' and at other times as 'convert this value ...
1
vote
1answer
186 views

Loose typing not applied to objects

I have very little experience working with classes and object. I work in a loosely typed language, PHP. I was working with a SimpleXML object and ran into a problem where I was trying to do math with ...
7
votes
8answers
6k views

c style casts or c++ style casts

So, what do you use? int anInt = (int)aFloat; or int anInt = static_cast<int>(aFloat); // and its brethren And, more importantly, why?
-3
votes
1answer
226 views

Casting from string to integer and the vica versa [closed]

I am trying to cast in Java from string to integer and the other way around but the compiler is complaining about this. My question is: Is this the matter of compiler or Java programming language ...
1
vote
2answers
1k views

Type parameterized methods to avoid casting

In a library I am developing I have a factory method that looks a bit like: public <T> T instantiate(Type targetType) { return ...; } I am using a type parameterized method to avoid that ...
0
votes
2answers
166 views

Boolean-Integer Typecasting to Replace Conditional

When choosing a value based off of 2 boolean values in this format var foo:int; if (X){ foo = 50; } else if (Y){ foo = -50; } else { foo = 0; } I discovered that I can condense this ...
7
votes
2answers
480 views

implement cons function in Java - type safety question

I am working on a small functional library written in Java, which mimics the a functional style of programming. I am stuck with a undesirable type cast in one of my method definitions and would love ...
1
vote
2answers
822 views

C++ Typecasting VS performance

Let's say we're designing a video game. We have some sprites on the map and we want to call some method of the particular sprite at some particular position. We are using one broadly-known C++ ...
4
votes
2answers
272 views

Alternatives to type casting in your domain

In my Domain I have an entity Activity which has a list of ITasks. Each implementation of this task has it's own properties beside the implementation of ITask itself. Now each operation of the ...
2
votes
1answer
2k views

Java default Integer value is int

My code looks like this import java.util.Scanner; public class StudentGrades { public static void main(String[] argv) { Scanner keyboard = new Scanner(System.in); byte q1 = ...
8
votes
3answers
2k views

Is there a difference between casting and converting types in imperative programming languages?

The question came up in a discussion at StackOverflow. Is there a clean distinction between the two concepts cast and convert (concerning the type of an object), or are these two words describing ...