casting

Classes Hierarchy and Casting between Objects

泄露秘密 提交于 2019-12-12 12:26:59
问题 as far as i know it's not possible to cast an Object of a superclass into an Object of a subclass . This will compile but during runtime it will return an error. More specifically, given the following Hierarchy of Classes and Interfaces: . Alpha is a superclass for Beta and Gamma . Gamma is a superclass for Delta and Omega . Interface "In" is implemented by Beta and Delta In this scenario i define the following code: Delta r; Gamma q; Is this correct? r = (Delta) q; Can i cast q to type Delta

Unable to Cast() generic dictionary item to DictionaryEntry when enumerated via non-generic IDictionary

≯℡__Kan透↙ 提交于 2019-12-12 12:24:55
问题 I have some code that iterates over a non-generic IDictionary by first calling the LINQ Cast method. However I get an invalid cast exception when passing a generic dictionary implementation even though I'm specifically using it via the non-generic IDictionary interface. IDictionary dict = new Dictionary<object, object> {{"test", "test"}}; foreach (var item in dict) { Debug.Assert(item is DictionaryEntry); // works } dict.Cast<DictionaryEntry>().ToList(); // FAILS Why does the Cast method

Using melt / cast with variables of uneven length in R

◇◆丶佛笑我妖孽 提交于 2019-12-12 12:22:46
问题 I'm working with a large data frame that I want to pivot, so that variables in a column become rows across the top. I've found the reshape package very useful in such cases, except that the cast function defaults to fun.aggregate=length. Presumably this is because I'm performing these operations by "case" and the number of variables measured varies among cases. I would like to pivot so that missing variables are denoted as "NA"s in the pivoted data frame. So, in other words, I want to go from

Implicit conversion without assignment?

可紊 提交于 2019-12-12 12:19:24
问题 Preserved question - see Edit at the bottom I'm working on a small functional library, basically to provide some readability by hiding basic cyclomatic complexities. The provider is called Select<T> (with a helper factory called Select ), and usage is similar to public Guid? GetPropertyId(...) { return Select .Either(TryToGetTheId(...)) .Or(TrySomethingElseToGetTheId(...)) .Or(IGuessWeCanTryThisTooIfWeReallyHaveTo(...)) //etc. ; } and the library will take care of the short circuiting, etc. I

Strange length restriction with the DB2 LIKE operator

若如初见. 提交于 2019-12-12 11:36:02
问题 I found a funny issue with DB2 v9.7 and the SQL LIKE operator. Check this out: -- this works and returns one record select 1 from SYSIBM.DUAL where 'abc' like concat('a', 'bc') -- this doesn't work select 1 from SYSIBM.DUAL where 'abc' like concat(cast('a' as varchar(2001)), cast('bc' as varchar(2000))) -- It causes this error (from JDBC): -- No authorized routine named "LIKE" of type "FUNCTION" having compatible -- arguments was found.. SQLCODE=-440, SQLSTATE=42884, DRIVER=4.7.85 I've played

C: How to get rid of conversion error?

喜夏-厌秋 提交于 2019-12-12 11:24:21
问题 I have a project which uses gcc version 4.6.3, and I'm forced to compile with "-Wall -Werror -Wconversion". The following simple example shows an error I can't get rid of: #include <stdint.h> int main(void) { uint32_t u = 0; char c = 1; u += c; return (int)u; } Compiling it with the above flags gives: test.c:7:8: error: conversion to ‘uint32_t’ from ‘char’ may change the sign of the result [-Werror=sign-conversion] Ok, fine. Just add a typecast, right? Nope. Changing line 7 to u += (uint32_t

Time complexity of casting lists to tuples in python and vice versa

假如想象 提交于 2019-12-12 11:22:26
问题 what is the time complexity of converting a python list to tuple (and vice versa): tuple([1,2,3,4,5,6,42]) list((10,9,8,7,6,5,4,3,1)) O(N) or O(1), i.e. does the list get copied or is something somewhere internally switched from writable to read-only? Thanks a lot! 回答1: It is an O(N) operation, tuple(list) simply copies the objects from the list to the tuple. SO, you can still modify the internal objects(if they are mutable) but you can't add new items to the tuple. Copying a list takes O(N)

Dynamic cast using Type of object C#

旧城冷巷雨未停 提交于 2019-12-12 11:00:28
问题 I have one abstract class named A, and other classes (B, C, D, E, ...) that implements A I also have a list of A objects. I'd like to be able to cast dynamicly each of the object in that list to their "base" type (ie B, C, D, ...) to be able to call their constructor in an other method. Here is what I have done for now : abstract class A { } class B : A { } class C : A { } class D : A { } class E : A { } // ... class Program { static void Main(string[] args) { List<A> list = new List<A> { new

Vector Iterators Casting

耗尽温柔 提交于 2019-12-12 10:56:25
问题 Hey, In C++, I have a vector of type: vector<BaseClass*> myVector; In which, I insert (push_back) pointers of derived classes into it. Now, I want to pop back its elements so I do this: vector<ADlgcDev*>::iterator iter; for (iter = myVector.rbegin(); iter != myVector.rend(); iter++) { // but before I pop it, I need to shutdown it down // so I cast this // but this way, I'm unable to call the function (DerivedClass*(*iter))->Shutdown(); myVector.pop_back(); } but as mention in the comments

Why does [] === [] (and others) return false in javascript?

北城余情 提交于 2019-12-12 10:56:19
问题 The following comparisons all return false in javascript: []===[] []==[] {}==={} {}=={} [0]===[0] [0]==[0] However the following return true : [0]=='0' [0]==0 []==false //(and all other == that were exampled above) What is the reason for this? Especially the difference between [0]!=[0] and [0]==0 Fiddle: http://jsfiddle.net/vnBVj/ 回答1: This is due to the confusing rules, how javascript does type coercion. You can read about this in §11.9.3 of the EcmaScript 5 spec. Two Objects (which Arrays