casting

c++ cast vector<Inherited*> to vector<abstract*>

Hi! class Interface{}; class Foo: public Interface{}; class Bar{ public: vector<Interface*> getStuff(); private: vector<Foo*> stuff; }; how Do I implement the funtion getStuff() ? hope the question is clear.. Thanks! ...

Identical classes casting?

Suppose I have two classes A and B. Both are identical (same atributes, methods, etc), but they are named different. There's some safe way, in Java, to cast a B object as an A object? ...

error: cast from 'Foo*' to 'unsigned int' loses precision

I'm trying to cast a pointer to an int (or unsigned int) and no matter what I try it doesn't want to work. I've tried static_cast<intptr_t>(obj), reinterpret_cast<intptr_t>(obj), and various combinations of C style casts, intptr_t's, unsigned int's, and I'm including stdint.h. From what I've read, one of the many things I've tried shou...

Avoiding casting multiple times.

Hello guys. I have a method which receives a parameter of base type and performs some pre-processing depending on actual parameter type. Here is my code: public void OnMessageReceived(QuickFix42.Message message) { if (message is QuickFix42.ExecutionReport) { ProcessExecutionReport(message as QuickFix42.ExecutionReport);...

Casting object type in c#

I ran into an issue today and I wasn't entirely sure why it wouldn't work. The following code sample will crash: static void Main(string[] args) { int i32 = 10; object obj = i32; long i64 = (long)obj; } This will result in an InvalidCastException. Why does this not work? Is C# not smart enough to know that the object...

Casting from void* to an object array in c++

Hello I'm having problems getting this to work, class A { public: A(int n) { a = n; } int getA() { return a; } private: int a; }; int main(){ A* a[3]; A* b[3]; for (int i = 0; i < 3; ++i) { a[i] = new A(i + 1); } void * pointer = a; b = (A* [])pointer; // DOESNT...

Pass timestamp between Powerbuilder datawindow and SQLServer insert/update stored procedure

Hi, can anyone help me out. By implementing countermeasures in solving the concurrency problem I got troubled with passing timestamps (rowversion) between Powerbuilder 7 datawindow and SQLServer 2008 (both ways) using insert and update stored procedures. The connection is serviced by ODBC, not native. Most of my attempts result in cas...

Casting Command Line Arguments?

i have a command line executable program that accepts a configuration file as it's one argument for launch. the configuration file contains only 2 lines of code: #ConfigFile.cfg name=geoffrey city=montreal currently, the wrapper program that i'm building for this command line executable program writes the configuration file to disk a...

C# How to check is there any value before converting ToString()

Hi, I have some dataset with values and now I need to put that values into textbox but there is some decimal and double type values so I need to cast them toString() and it happens that sometime dataset values are empty so before casting toString() I need to check Is there any value ??? This is sample line: I need something like this...

Is there a better option for this code ?

Animal public abstract class Animal { String name; public Animal(String name) { this.name = name; } } Lion public class Lion extends Animal { public Lion(String name) { super(name); // TODO Auto-generated constructor stub } public void roar() { System.out.println("Roar"); } } Deer public class Deer extends Anima...

safe cast by a subclass type id in java

I have a base Entity class: public class Entity { abstract int getTypeID(); } The method int getTypeID() returns a number that is unique to that class: public class OneEntity extends Entity { int getTypeID() { return 1; // Actually defined in a constants class } } Now I want to be able to safely cast it and ...

How can I cast a gcroot<Object^> to IMyInterface in C++.net?

I'm having to do some weird things with gcroot, but I get the following error on the dynamic cast line: "cannot use 'dynamic_cast' to convert from 'gcroot' to 'IMyInterface^'. In C#, you could easily cast a generic object to any interface. You may get a runtime error if the object doesn't implement the interface but it would compile. ...

How can a pointer be implemented except storing an address?

Recently I answered another question asking for questions every decent C++ programmer should be able to answer. My suggestion was Q: How does a pointer point to an object? A: The pointer stores the address of that object. but user R.. disagrees with the A I propose to the Q - he says that The correct answer would be "it's implementati...

How to cast 'object' to Class type on IComparer.Compare method

I'm implementing an int IComparer.Compare(object x, object y); from the IComparer interface. I know the objects are of type Class1, and I know one of its members is class1Instance.myDate, of type DateTime. What I want to do is something along the lines of: DateTime.Compare( (Class1)x.myDate, (Class1)y.myDate); But casting this w...

Java Reference assignment with generic lists

I feel stupid asking this but I am. The line List<HasId> ids = list is giving a compile error in the following code: public class MyGarbageClass { public void myMethod(List<MyCompany> list){ List<HasId> ids = list; } interface HasId { int getId(); } class MyCompany implements HasId{ private...

Cannot cast MembershipUser to custom class (ASP.NET)

I'm using the default SqlMembershipProvider, but I've created a custom MembershipUser class (SoeMembershipUser) because I needed a "DisplayName" property. All the DisplayName does is look at the UserName and format it differently. When I try to cast a MembershipUser to a SoeMembershipUser user I get an InvalidCastException. Exact error ...

Why I have to cast this statement in order to get a result?

private final static int L1_MAX_SCORE = 30; private final static int L2_MAX_SCORE = 150; public void UpdateLevel(int score) { double progress; //returns 0.0 progress = score / (L2_MAX_SCORE - L1_MAX_SCORE) * 100; //works correctly. progress = (double) score / (L2_MAX_SCORE - L1_MAX_SCORE) * 100; Thanks. ...

java: combined instanceof and cast ?

(Please no advise that I should abstract X more and add another method to it.) In C++, when I have a variable x of type X* and I want to do something specific if it is also of type Y* (Y being a subclass of X), I am writing this: if(Y* y = dynamic_cast<Y*>(x)) { // now do sth with y } The same thing seems not possible in Java (or...

Casting enum value to int when binding to dropdown using reflection

I have some code that binds an enumeration to a dropdown list, however, I'd like build the dropdown to take the integer value from the enum for the value and the description attribute for the text. I tried defining the kvPairList as a int/string and casting enumValue and an (int) I also tried converting.toInt32 Ideas? <select name=...

Why [ (int)(object)10m; ] does throw "Specified cast is not valid" exception?

Why this explicit cast does throw Specified cast is not valid. exception ? decimal d = 10m; object o = d; int x = (int)o; But this works: int x = (int)(decimal)o; ...