casting

Casting vs Converting an object toString, when object really is a string

橙三吉。 提交于 2019-12-17 06:34:07
问题 This isn't really an issue, however I am curious. When I save a string in lets say an DataRow, it is cast to Object. When I want to use it, I have to cast it ToString. As far as I know there are several ways of doing this, first is string name = (string)DataRowObject["name"]; //valid since I know it's a string and another one is: string name = DataRowObject["name"].ToString(); I am interested in what is the difference between both? Is the first more efficient? (This is just a speculation, in

How to determine the field value which can not convert to (decimal, float,int) in SQL Server

送分小仙女□ 提交于 2019-12-17 06:31:51
问题 I have a SQL Server database. One field has values which are like ID VALUE 1 NEGATIF 2 11.4 3 0.2 4 A RH(+) 5 ----- 6 >>>>> 7 5.6< 8 -13.9 I want to CONVERT VALUE field to decimal, of course convert-able fields. What kind of SQL statement can do this? How can I understand which value is raising error while converting? PS: I think this can solve WHERE VALUE LIKE '[a-z]' but how can I add more filter like [-+ ()] ? 回答1: Plain ISNUMERIC is rubbish Empty string, + , - and . are all valid So is +.

Why cannot cast Integer to String in java?

大憨熊 提交于 2019-12-17 06:25:14
问题 I found some strange exception: java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String How it can be possible? Each object can be casted to String, doesn't it? The code is: String myString = (String) myIntegerObject; Thanks. 回答1: Why this is not possible: Because String and Integer are not in the same Object hierarchy. Object / \ / \ String Integer The casting which you are trying, works only if they are in the same hierarchy, e.g. Object / / A / / B In this case,

Cannot convert from List<List> to List<List<?>>

寵の児 提交于 2019-12-17 06:14:10
问题 A raw list converts to List<?> just fine. Why can't a list of raw lists convert to a list of List<?> ? { // works List raw = null; List<?> wild = raw; } { // Type mismatch: cannot convert from List<List> to List<List<?>> List<List> raw = null; List<List<?>> wild = raw; } Backstory (to mitigate the xy problem): An API I'm using returns List<JAXBElement> . I happen to know that it is always List<JAXBElement<String>> . I plan to loop and build my own List<String> , but I was trying to fix (but

What does casting to `void` really do?

和自甴很熟 提交于 2019-12-17 05:04:59
问题 An often used statement like (void)x; allows to suppress warnings about unused variable x . But if I try compiling the following, I get some results I don't quite understand: int main() { int x; (short)x; (void)x; (int)x; } Compiling this with g++, I get the following warnings: $ g++ test.cpp -Wall -Wextra -o test test.cpp: In function ‘int main()’: test.cpp:4:13: warning: statement has no effect [-Wunused-value] (short)x; ^ test.cpp:6:11: warning: statement has no effect [-Wunused-value]

Why does “int[] is uint[] == true” in C#

血红的双手。 提交于 2019-12-17 05:01:28
问题 Can somebody clarify the C# is keyword please. In particular these 2 questions: Q1) line 5; Why does this return true? Q2) line 7; Why no cast exception? public void Test() { object intArray = new int[] { -100, -200 }; if (intArray is uint[]) //why does this return true? { uint[] uintArray = (uint[])intArray; //why no class cast exception? for (int x = 0; x < uintArray.Length; x++) { Console.Out.WriteLine(uintArray[x]); } } } MSDN's description does not clarify the situation. It states that

Dynamically create PHP object based on string

十年热恋 提交于 2019-12-17 04:54:48
问题 I would like to create an object in PHP based on a type defined by a string in a MySQL database. The database table has columns and sample data of: id | type | propertyVal ----+------+------------- 1 | foo | lorum 2 | bar | ipsum ...with PHP data types class ParentClass {...} class Foo extends ParentClass {private $id, $propertyVal; ...} class Bar extends ParentClass {private $id, $propertyVal; ...} //...(more classes)... Using only one query, I would like to SELECT a row by id and create an

Casting one C structure into another

浪尽此生 提交于 2019-12-17 04:52:20
问题 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? I tried the following but get these compiler errors: vector = acceleration; // "incompatible type" vector = (Vector3d)acceleration; // "conversion to non-scalar type requested" Of course I can

How to cast an Object to an int

大城市里の小女人 提交于 2019-12-17 04:39:35
问题 How can I cast an Object to an int in java? 回答1: If you're sure that this object is an Integer : int i = (Integer) object; Or, starting from Java 7, you can equivalently write: int i = (int) object; Beware, it can throw a ClassCastException if your object isn't an Integer and a NullPointerException if your object is null . This way you assume that your Object is an Integer (the wrapped int) and you unbox it into an int. int is a primitive so it can't be stored as an Object , the only way is

Why enums require an explicit cast to int type?

社会主义新天地 提交于 2019-12-17 04:06:11
问题 There is no data loss by doing this, so what's the reason for having to explicitly cast enums to ints? Would it not be more intuitive if it was implicit, say when you have a higher level method like: PerformOperation ( OperationType.Silent type ) where PerformOperation calls a wrapped C++ method that's exposed as such: _unmanaged_perform_operation ( int operation_type ) 回答1: There are two primary and inconsistent uses of enums: enum Medals { Gold, Silver, Bronze } [Flags] enum