casting

C++ object and C style cast question

I have the following code compiled by gcc: #include <iostream> using namespace std; class Buffer { public: operator char *() { cout << "operator const * called" << endl; return buff; } private: char buff[1024]; }; int main(int, char**) { Buffer b; (char *)b; // Buffer::operator char * is called here return 0; } ...

casting in MySQL

I have a varchar field that looks like (sadly I have no control over the data): Fri, 30 Oct 2009 06:30:00 EDT Is there a way to cast this into a timestamp format so that I can then sort based on it? ...

Java: cast collection type to subtype

Suppose class B extends class A. I have a List<A> that I happen to know only contains instances of B. Is there a way I can cast the List<A> to a List<B>? It seems my only option is to iterate over the collection, casting one element at time, creating a new collection. This seems like an utter waste of resources given type erasure makes ...

Strange Java cast exception. Why can't I cast Long to a Float?

java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.Float Why is this a problem? The numbers that I'm trying to cast are decimals in the domain [-10.0, 10.0]. They start out as Object instances returned using JFormattedTextField.getValue(). But they must be converted to floats. stack trace: Exception in thread "AW...

Limiting strict off VB.NET

Hi I am exploring ways to implement something Visitor Patterns alike without all the decorating visit methods. Sofar I found out I could use Option Strict Off in VB.NET but it has some side effects. I have a set of Shape classes that inherit from a baseclass Shape. Assume we have the following class that Accept's shapes: Public Class Sh...

Error when typecasting a plugin instance in a signed assembly

I'm developing an application with plugins. I have a separate API assembly with all the API interfaces. The main application dynamically loads the plugins from dlls: object pi = Assembly.LoadFrom(plugin.AssemblyPath) .CreateInstance(plugin.ClassName); and then i'm casting it to the actual plugin type: IPlugin pluginIns...

Casting .resx classes to obtain ResourceSet

Hello, I am trying to populate a dropdownlist with data pulled from a .resx file. Instead of having 5 different functions I'd like to be able to pass in the name of the .resx file and cast it somehow so I can retrieve it using GetReourceSet. Here's what I'm currently doing: protected void populateCountryPickList(DropDownList whatDropD...

testing if a string can be cast as a integer in VB.NET

Is there a better way of testing if a string can be converted to an integer other than something like the following? Public Function IsInt(ByVal value As Object) As Boolean Try Dim temp As Integer = CInt(value) Return True Catch ex As Exception Return False End Try End Function by "better" I mean l...

Cast down with reflection at runtime

Considering following code public class A { public static void main(String[] args) { new A().main(); } void main() { B b = new B(); Object x = getClass().cast(b); test(x); } void test(Object x) { System.err.println(x.getClass()); } class B extends A { } } ...

Is there a Java equivalent to C#'s 'checked' keyword?

Yes, it's a trivial piece of code to write, but I still wonder if there's a built-in replacement. Here's the code: /** * Cast x to int, throw an exception if there's loss of information */ public static int safeLongToInt(long x) { int result = (int) x; if (result != x) throw new RuntimeException("long doesn't fit in an int: "...

My Interface and external Webserver

I have an external Web Service that returns back its own object, and I would like to get it to compile to my interfaces is this possible? (IGetPerson)testAPI.GetPersons(); Where testAPI is the external web service testAPI.GetPerson returns webservice.GetCarResponse, which is of course from the external webservice. I need to get the ...

How dynamic casts work?

Let's say I have type A, and a derived type B. When I perform a dynamic cast from A* to B*, what kind of "runtime checks" the environment performs? How does it know that the cast is legal? I assume that in .Net it's possible to use the attached metadata in the object's header, but what happen in C++? ...

Casting const void pointer to array of const char pointers properly in C.

I have a piece of C code that looks like this: const char (*foo)[2] = bar(); Now bar() is a function that returns a (const void *). How do I properly cast this const pointer? The code produces this warning from GCC : "initialization discards qualifiers from pointer target type". Here are some of my unsuccessful attempts: const char (...

Casting generic object array to two types

I've got a method that receives an Object[] and then performs actions on that array. At first I was passing in this array as an IEnumerable<T> however the T can be of two different types. The T's will always have the same properties, even thought they're different types. Is it possible to cast to a a type at runtime so that I can use...

Beginner: Extending a class in C#, am I doing it wrong?

Again disclaimer disclaimer still learning C# and OOP generally so I hope you'll be patient with me :) I am currently working with a CMS that has a class called FileVersion which basically contains a list of properties pertaining to a file such as filename, filetype, size in bytes, id, date uploaded, is-latest-version, etc. A list of F...

The as operator rejects the object even though the object appears to be of the correct type in the debugger.

The following code throws an exception. If there is no easy answer or stuff to check, I'll try to produce something that reproduces the error (though I don't know where to upload it). public static XMLobj Load(string FileName) { if (File.Exists(FileName) == false) { return null; } IRDnet.XMLobj def; XmlSerializer ...

Complicated C cast explanation

I'm trying to figure out what the following code in C does? ((void(*)())buf)(); where 'buf' is a char array. ...

tsql casting to money rounds up

When casting a varchar value to MONEY it is rounding the value to the nearest 0.10, how do I prevent this rounding up? UPDATE: I found the problem. In a subquery, the value is being CAST from varchar to FLOAT and then I was trying to CAST from FLOAT to MONEY. ...

What is the difference between these two ways of casting in Java?

What is the difference between these two ways of casting in Java? (CastingClass) objectToCast; CastingClass.class.cast(objectToCast); The source of Class#cast(Object) is as follows: public T cast(Object obj) { if (obj != null && !isInstance(obj)) throw new ClassCastException(); return (T) obj; } So, cast is basically a generic...

How can you cast between wchar_t* and an int?

I have a function which returns the inner text of an xml element. It returns it, however, as a const wchar_t*. I wish to return this value as an integer (And a float in some other cases). What is the best method for doing so? ...