casting

Why does C# throw casting errors when attempting math operations on integer types other than int?

倾然丶 夕夏残阳落幕 提交于 2019-12-23 09:48:14
问题 Consider this static test class: public static class Test { public static ushort sum(ushort value1, ushort value2) { return value1 + value2 } } This causes the following compile error, with value1 + value2 underlined in red: Cannot implicitly convert type 'int' to 'ushort'. An explicit conversion exists (are you missing a cast)? Why? 回答1: Like C and C++ before it, integers are implicitly widened when used with many operators. In this case, the result of adding two ushort values together is

Cast parent class to child class

家住魔仙堡 提交于 2019-12-23 09:48:12
问题 I have Message class which I have extended and added new property. class ChildMessage: Message { prop... } While trying to add Message Class to ChildMessage list I get Null reference for added class. var myChildList =new List<ChildMessage> (); var myParentClass = new Message(); myChildList.add(myParentClass as ChildMessage) myChildList[0] == null //Always return null What is wrong with my code? What are the alternatives? 回答1: It is because a true instance of Message is not a ChildMessage ,

How to cast a swift array of tuples to NSMutableArray?

只谈情不闲聊 提交于 2019-12-23 09:43:37
问题 I have swift array of tuples [(String, String)] and would like to cast this array to NSMutableArray. I have tried this and it is not working: let myNSMUtableArray = swiftArrayOfTuples as! AnyObject as! NSMutableArray 回答1: Since swift types like Tuple or Struct have no equivalent in Objective-C they can not be cast to or referenced as AnyObject which NSArray and NSMutableArray constrain their element types to. The next best thing if you must return an NSMutableArray from a swift Array of

Casting errors when attempting to return an IQueryable<MyType>

丶灬走出姿态 提交于 2019-12-23 09:41:19
问题 I have a query that ought to return an IQueryable<MyType> . The code looks like this: public IQueryable<MyType> GetFooList() { var query = (from x in dbContext.TableX join y in dbContext.TableY on x.our_id equals y.our_id join z in dbContext.TableZ on y.our_id equals z.our_id join a in dbContext.TableA on z.other_id equals a.other_id where !string.IsNullOrEmpty(x.status) select new { (fields....) }) .AsQueryable(); IQueryable<MyType> result = (IQueryable<MyType>) query; return result; } In

Oracle Cast and MULTISET avaliable in POSTGRES

女生的网名这么多〃 提交于 2019-12-23 09:36:23
问题 I have been working on a query to generate xml from the oracle database database where "column" is a type CREATE OR REPLACE TYPE "column" AS OBJECT ("coulmnname" VARCHAR2 (30), "datatype" VARCHAR2 (30)) and col_list_t is of type CREATE OR REPLACE TYPE col_list_t AS TABLE OF "column" and SELECT CAST ( MULTISET ( SELECT "column" (B.COLUMN_NAME, B.DATA_TYPE) FROM all_tab_columns b, all_tables c ,all_tables a WHERE b.TABLE_NAME = a.TABLE_NAME AND b.table_name = c.TABLE_NAME AND B.OWNER = C.OWNER

Why does not the true match 'true' with double equals sign '==' in JavaScript? [duplicate]

耗尽温柔 提交于 2019-12-23 09:31:15
问题 This question already has answers here : Why does “true” == true show false in JavaScript? (3 answers) Closed 4 years ago . This small portion of code took a long time to be noticed. I thought if I do the following, it would be fine if('true' == true) { alert("Does not happen"); } But it does not pass the if condition. I thought the double equals == matches the value not the type as matching the type is the job of === . Now my questions are why wasn'the true typecast to 'true' or why is it

Using mapTo with futures in Akka/Scala

雨燕双飞 提交于 2019-12-23 09:29:52
问题 I've recently started coding with Akka/Scala, and I've run into the following problem: With a implicit conversion in scope, such as: implicit def convertTypeAtoTypeX(a: TypeA): TypeX = TypeX() // just some kinda conversion This works: returnsAFuture.mapTo[TypeX].map { x => ... } But this doesn't: returnsAFuture.mapTo[TypeX].onComplete { ... } The latter fails with a type cast exception. (i.e. TypeA cannot be cast to TypeX) Very confused. Why? I suspect it has something to do with Try, but I

Is it safe to catch an Exception object

℡╲_俬逩灬. 提交于 2019-12-23 09:27:46
问题 I use a Java lib that relies on exceptions. Simplified code below: try { val eventTime = eventTimeString.as[Date] } catch { case e: Exception => logger.error(s"Can't parse eventTime from $eventTimeString", e) // take action for the bad Date string. } In Java I would catch only the exception from parsing a string into a Date, letting the rest go uncaught since they could be fatal. Here my understanding is that catching Exception means to catch ANY non-fatal/non-serious exceptions. Since it is

Is it possible to completely avoid C-style casts in C++?

主宰稳场 提交于 2019-12-23 09:17:44
问题 I do not believe that it is possible to completely avoid C-style casts when writing C++. I was surprised to find out that I needed to use a C-style cast to avoid a compiler truncation warning: short value_a = 0xF00D; // Truncation warning in VS2008 short value_b = static_cast<short>(0xF00D); // Truncation warning in VS2008 short value_c = (short)0xF00D; // No warning! Are there other scenarios where there is no C++-style substitute for a C-style cast? 回答1: You are just trying to obfuscate

C# - Is there some way to cast a generic collection?

只愿长相守 提交于 2019-12-23 09:11:24
问题 I've been busy with C# 4.0 generics, and now I basically want to do something like this: public abstract class GenericTree<T> : Tree where T : Fruit { public Tree(IFruitCollection<T> fruits) : base(fruits) { } } The base Tree class looks like this: public abstract class Tree { private IFruitCollection<Fruit> fruits; public IFruitCollection<Fruit> GetFruits { get { return fruits; } } public Tree(IFruitCollection<Fruit> fruits) { this.fruits = fruits; } } Here is my first problem. The