cast

Why does this dynamic_cast of auto_ptr fail?

#include "iostream" class A { private: int a; public : A(): a(-1) {} int getA() { return a; } }; class A; class B : public A { private: int b; public: B() : b(-1) {} int getB() { return b; } ...

Mis-aligned pointers on x86

Can someone provide an example were casting a pointer from one type to another fails due to mis-alignment? In the comments to this answer, bothie states that doing something like char * foo = ...; int bar = *(int *)foo; might lead to errors even on x86 if alignment-checking is enabled. I tried to produce an error condition after set...

LINQ to Data : Clever Type Recognition.

Working on Maths problems, I'm very fond of LINQ to Data. I would like to know if LINQ is smart enough to avoid a cast like .ToArray() when the IEnumerable I work with is already an array. See example below: /// <summary> Transforms an array of timeSeries into one ModelData. </summary> public static ModelData ToModelData(this IEnumerab...

When to use reinterpret_cast?

I am little confused with the applicability of reinterpret_cast vs static_cast. From what I have read the general rules are to use static cast when the types can be interpreted at compile time hence the word static. This is the cast the C++ compiler uses internally for implicit casts also. reinterpret_cast are applicable in two scenario...

Firebird determine if a string is all numbers

I have a VARCHAR field in a Firebird 2.0 table that can contain alphanumeric characters. I need to sort data on this field, sorting all values that contain only numbers as numbers, and sort all other values as 0. For example, if I have four values, "1", "2", "10", "string", I need to sort it as "string", "1", "2", "10". Default sort with...

Java generics

I'd like to implement a method that takes an Object as argument, casts it to an arbitrary type, and if that fails returns null. Here's what I have so far: public static void main(String[] args) { MyClass a, b; a = Main.<MyClass>staticCast(new String("B")); } public static class MyClass { } public static <T> T staticCast(Objec...

Confused by Java generics requiring a cast

I'm confused by the following code: import java.util.ArrayList; import java.util.LinkedList; import java.util.List; public class GenericsTest<T extends List> { public void foo() { T var = (T) new LinkedList(); } public static void main(String[] args) { GenericsTest<ArrayList> gt1 = new GenericsTest<ArrayList>(); ...

SSIS Derived Column Expression for Boolean-to-Char CAST

Hello, I am having a bit of a struggle with Expressions inside SSIS Derived Columns. My source field is a BOOLEAN data type. It's destination field is a SQL CHAR datatype. ** Note that I did not design either of the schemas. If I had my way, the data types would match up. Unfortunately, this is not my case! I found a great example of...

Cast JSON as a custom object?

Hello I was wondering if it is possible to cast my JSON string as a custom object? basically : var customObject:CustomObject = JSON.decode(evt.result as String) as CustomObject; Regards Adlertz ...

CAST incorrectly casting float to varchar

Group, I am sure this is user error somehow, but I am trying to CAST a column of numbers from a float to a varchar. It works great for anything under 7 digits, but if the number is 7 digits it turns it into scientific notation or what ever they call that. For example: 440000 displays 440000 1299125 displays 1.29913e+006 It looks like...

Best way to cast from Animal[] to Dog[]

If Dog enherits from Animal. And I have a Animal[], that I happen to know contains only dogs. What's the fastest/best way to get my hands on a Dog[] ? I've used new ArrayList(oldarray).ToArray(typeof(Dog)); so far, but that feels a bit clumsy, and I'm wondering if there is something more elegant. UPDATE: Using the .net 2.0 profile. ...

System.InvalidCastException: System.Data.DataViewManagerListItemTypeDescriptor

Am getting an exception randomly. I have a barcode scanner which enters barcodes into a table. The code is written in C#. I was able to get the exception twice at first; however, am no longer able to generate it. The exception is System.InvalidCastException: Unable to cast object of type 'System.Data.DataViewManagerListItemTypeDescriptor...

How to use the CAST function correctly in a MySql SELECT statement?

I'm converting an MSSQL DB to MySQL DB and I have a stored procedure that is using a cast function to convert from a datetime datatype to a varchar datatype. Whether or not this matters in php/mysql since php isn't strongly typed (and I dont know if it would matter or not) I really want to keep the SP a close to the orginal as possible s...

Cast to generic type in C#

I have a Dictionary to map a certain type to a certain generic object for that type. For example: typeof(LoginMessage) maps to MessageProcessor<LoginMessage> Now the problem is to retrieve this generic object at runtime from the Dictionary. Or to be more specific: To cast the retrieved object to the specific generic type. I need it t...

Equivalent of c#'s 'as' in Java?

Is it possibly in java to attempt a cast and get a null if the cast fails? ...

Adding a WCF Service (<> WCF Service Reference) gives "Specified Cast is Invalid"

When adding a second WCF service to an existing WCF project, or adding a first WCF service to a project gives me a dialog box "Specified Cast Is Invalid". WCF files are added to the project except interface file. Web.Config isn't updated neither. I think the problem started after updating VS.NET 2008 to VS.NET 2008 SP1. ...

Use ampersand in CAST in SQL

The following code snippet on SQL server 2005 fails on the ampersand '&': select cast('<name>Spolsky & Atwood</name>' as xml) Does anyone know a workaround? Longer explanation, I need to update some data in an XML column, and I'm using a search & replace type hack by casting the XML value to a varchar, doing the replace and updating ...

How do I convert IEnumerable to a custom type in C#?

I am using extension methods OrderBy and ThenBy to sort my custom collection on multiple fields. This sort does not effect the collection but instead returns and IEnumberable. I am unable to cast the IEnumerable result to my custom collection. Is there anyway to change the order of my collection or convert the IEnumerable result to my...

SQL converting Text fields

I'm bumping into somenull fields in a SQL2005 db. Some report Null and others have values like 1.00 or 713.00. I'd like a bullet proof way to convert the 'Null's to 0 and the '1.00' and '713.00' values into Money types. ...

Convert ListBox.ObjectCollection to String array in VB.NET

I'm programming an application in VB.NET in which I need to take the Items from a ListBox, which are an ListBox.ObjectCollection and convert into a String array to be passed as a parameter to a Sub procedure. How should I do it? ...