casting

Excel RTD COM server - Cannot cast UpdateEvent (class) to IRTDUpdateEvent (interface)

Hello All This issue is related to a blog post by Kenny Kerr on "Excel RTD Servers: C# Interfaces", which can be found here, that should allow you to build an Excel RTD server without including a reference to any specific Excel type library; having to include a reference makes your RTD server Excel version specific (forwards compatible,...

printf too smart casting from char to int?

Why does the following call: printf("%d %d", 'a', 'b'); result in the "correct" 97 98 values? %d indicates the function has to read 4 bytes of data, and printf shouldn't be able to tell the type of the received arguments (besides the format string), so why isn't the printed number |a||b||junk||junk|? Thanks in advance. ...

How much processing and memory use does casting take in Java?

Hi all, I am considering whether it is better to have two pointers, one for each object sub class and super, or whether I should just use casting. How much system resources does this use: objectName.functionOne(); ((SubClass) objectName).functionOther(); Is it better than: SuperClass objectA = (SuperClass) getSameInstance(); SubCl...

Java: Type safety : A generic array of A is created for a varargs parameter

Consider this is given: interface A<T> { /*...*/ } interface B<T> extends A<T> { /*...*/ } class C { /*...*/ } void foo(A<T>... a) { /*...*/ } Now, some other code wants to use foo: B<C> b1 /* = ... */; B<C> b2 /* = ... */; foo(b1, b2); This gives me the warning Type safety : A generic array of A is created for a varargs parameter...

Get value from Dictionary<string, object> without unboxing?

Hey, I was wondering if it's possible to run the following code but without the unboxing line:- t.Value = (T)x; Or maybe if there is another way to do this kind of operation? Here is the full code:- public class ValueWrapper<T> { public T Value { get; set; } public bool HasValue { get; set; } public ValueWrapper() ...

Can a downcast to a derived class with no data members be done in C#?

I'm attempting to make a wrapper using inheritance. In reality I am working with de-serialization code that has very generic names and I am looking to save a few keystrokes over the method that wraps an inner object. public class Base { public string Foo { get; set; } } public class Derived : Base { public string Bar { get { retu...

Problems with casting LinQ result to strongly typed collection.

Hi guys. I guess this is trivial but not for me. I have a collection MembershipUserCollection. I wanted to perform some Linq query over it so I've used OfType<MembershipUser>() method and then applied Where() on it. However, I do not know how to cast back my results to MembershipUserCollection? This is my code: MembershipUserCollectio...

Casting one C structure into another

I have two identical (but differently named) C structures: typedef struct { double x; double y; double z; } CMAcceleration; typedef struct { double x; double y; double z; } Vector3d; Now I want to assign a CMAcceleration variable to a Vector3d variable (copying the whole struct). How can I do this? ...

Why can a generic list be casted to its type parameter

Why does the following code compile? Why is it allowed to cast a generic list to its type parameter if the parameter is an interface but not a super-interface of the generic? What does this mean? //Connection can be substituted by any interface List<Connection> list = null; Connection c = (Connection) list; ...

EntityFramework Casting issues

I am building my query using PredicateBuilder from LinqKit. it is great and does exactly what i am looking for. To make my code more reusable (tables and views) i created a generic predicate builder class: public class LocalPredicateBuilder<T> where T : IResort ... var predicate = PredicateBuilder.True<T>( which exposes BuildPre...

Casting to List(Of T) in VB.NET

In C# it's possible to cast to List<T> - so if you have: List<Activity> _Activities; List<T> _list; The following will work: _list = _Activities as List<T>; but the translated line with VB.NET which is: _list = TryCast(_Activities, List(Of T)) throws a compilation error. So I've had a good hunt around and experimented with LINQ ...

Operator-function + with two implicit casts doesn't work

hi, I'm trying to port some parts from ginac (www.ginac.de) to C#. But I encountered this: class Program { static void Main(string[] args) { symbol s = new symbol(); numeric n = new numeric(); ex e = s + n; // "Operator + doesn't work for symbol, numeric" } } class ex { //this should be alre...

static_cast on derived classes when base turns from not polymorphic to polymorphic

Hi All I am reviewing C++ casts operator and I have the following doubt: for polymorphic classes I I should use polymorphic_cast I should never use of static_cast since down-casting might carry to undefined behavior. The code compiles this case anyway. Now suppose that I have the following situtation class CBase{}; class CDerive...

Creating generic arrays in Java

public K[] toArray() { K[] result = (K[])new Object[this.size()]; int index = 0; for(K k : this) result[index++] = k; return result; } This code does not seem to work, it will through out an exception: java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to ... Could someone tell me how I can ...

Custom Linked List - Cannot cast object class to object

For this project I'm not allowed to use generic classes keep in mind. I have tried copying all my classes into another friends computer and he gets the same error. I cannot make calls like.. int counter = ((Movie) movieList.get(movListIndex)).getShowList().size(); Where getShowList() is a method in my Movie class that returns a Linke...

Why is "operator void" not invoked with cast syntax?

While playing with this answer by user GMan I crafted the following snippet (compiled with Visual C++ 9): class Class { public: operator void() {} }; Class object; static_cast<void>( object ); (void)object; object.operator void(); after stepping over with the debugger I found out that casting to void doesn't invoke Class:...

covariance in c#

Is it possible to cast a List<Subclass> to List<Superclass> in C# 4.0? Something along these lines: class joe : human {} List<joe> joes = GetJoes(); List<human> humanJoes = joes; Isn't this what covariance is for? if you can do: human h = joe1 as human; why shouldn't you be able to do List<human> humans = joes as List<human>...

Why does dynamic type work where casting does not?

My guess until now was that a dynamic type just "switches off" type checking during compilation and does something similar to a type cast when a message is invoked on a dynamic instance. Obviously something else is going on. The attached NUnit test case shows my problem: Using a dynamic type I can use a method only available in the conc...

MySQL: Casting confusion or bug?

I can't seem to find an explanation for this and I'm pretty sure that it has previously worked as expected. SELECT CAST(-1 AS UNSIGNED INTEGER); Expected: 0 Result: 18446744073709551615 Am I confused, has something changed, or is this a MySQL bug? ...

Interpret 0x string as hex in Python

I'm appending some hex bytes into a packet = [] and I want to return these hex bytes in the form of 0x__ as hex data. packet.append("2a") packet.append("19") packet.append("00") packet.append("00") packHex = [] for i in packet: packHex.append("0x"+i) #this is wrong return packHex How do I go about converting ('2a', '19', '00', ...