casting

Casting generics and the generic type

戏子无情 提交于 2020-01-03 10:54:48
问题 Consider, I have the following 3 classes / interfaces: class MyClass<T> { } interface IMyInterface { } class Derived : IMyInterface { } And I want to be able to cast a MyClass<Derived> into a MyClass<IMyInterface> or visa-versa: MyClass<Derived> a = new MyClass<Derived>(); MyClass<IMyInterface> b = (MyClass<IMyInterface>)a; But I get compiler errors if I try: Cannot convert type 'MyClass<Derived>' to 'MyClass<IMyInterface>' I'm sure there is a very good reason why I cant do this, but I can't

Type Casting an Object using a “Type” Object in C#

只谈情不闲聊 提交于 2020-01-03 07:26:09
问题 This one has proven to be a little tricky for me so far. I am wondering if it is possible to type cast an object using a System.Type object. I have illustrated below what I mean: public interface IDataAdapter { object Transform(object input); Type GetOutputType(); } public class SomeRandomAdapter : IDataAdapter { public object Transform(object input) { string output; // Do some stuff to transform input to output... return output; } public Type GetOutputType() { return typeof(string); } } //

What's this usage of the variable cast to void in function body?

一世执手 提交于 2020-01-03 06:51:32
问题 I am just learning to code embedded C. I see some code like below. The function is defined like this: void printDebug(const char d1[]){(void)d1;} And it is used like this: printDebug("BLE_UART_EVENT"); I don't understand its purpose. It gives me an impression of a callable char array? 回答1: It's not calling char array, it's just explicitly converting the char array to void . (And the evaluated result is discarded immediately.) I think it's just used to prohibit the compiler warning of unused

Error: illegal cast: from 'int' to 'union'

只愿长相守 提交于 2020-01-03 01:34:10
问题 I'm getting the error, illegal cast: from 'int' to 'FIELDS' while initializing the structure variables here:- SOCKET_LOG_DATA socket_log_data() : fields(0), socket_number(0) {} How should I resolve it? typedef PACKED struct PACKED_SUFFIX { UINT16 loss_reason : 1; UINT16 unused : 15; } LOSS_REASON; typedef union PACKED_SUFFIX { LOSS_REASON loss; UINT16 all_fields; } FIELDS; typedef PACKED struct PACKED_SUFFIX SOCKET_LOG_DATA { FIELDS fields; UINT16 socket_number; // As per @Dietrich's &

Casting a TabItem to UserControl in WPF

微笑、不失礼 提交于 2020-01-02 14:03:43
问题 I have a Tab Control on my main screen. It has different tab items. For example: <TabControl Name="mainTab" Padding="0" Margin="70,80,350,49"> <!--Left,Top,Right, Bottom--> <TabItem GotFocus="TabItem_Animals_GotFocus"> <TabItem.Header> Animals </TabItem.Header> <ContentControl Margin="10"> <Frame Name="animalFrame" Source="AnimalWorkSpaceView.xaml"></Frame> </ContentControl> </TabItem> <TabItem GotFocus="TabItem_Calfs_GotFocus"> <TabItem.Header> Calfs </TabItem.Header> <ContentControl Margin=

PHP: cast to (array) and return-type: array is not the same?

房东的猫 提交于 2020-01-02 06:19:52
问题 I have following problem in PHP: print_r() says it's the same, gettype() says same type, but the last output works not for both cases although they should be the same! This looks very strange to me. code and output: $docdatau = get_object_vars(json_decode($docdata)); $docdatau2 = (array)json_decode($docdata); echo "1\n"; echo gettype($docdatau); echo "\n"; echo "--------------------------------------\n"; print_r($docdatau); echo "--------------------------------------\n"; echo "2\n"; echo

PHP: cast to (array) and return-type: array is not the same?

☆樱花仙子☆ 提交于 2020-01-02 06:19:11
问题 I have following problem in PHP: print_r() says it's the same, gettype() says same type, but the last output works not for both cases although they should be the same! This looks very strange to me. code and output: $docdatau = get_object_vars(json_decode($docdata)); $docdatau2 = (array)json_decode($docdata); echo "1\n"; echo gettype($docdatau); echo "\n"; echo "--------------------------------------\n"; print_r($docdatau); echo "--------------------------------------\n"; echo "2\n"; echo

PHP: cast to (array) and return-type: array is not the same?

試著忘記壹切 提交于 2020-01-02 06:18:07
问题 I have following problem in PHP: print_r() says it's the same, gettype() says same type, but the last output works not for both cases although they should be the same! This looks very strange to me. code and output: $docdatau = get_object_vars(json_decode($docdata)); $docdatau2 = (array)json_decode($docdata); echo "1\n"; echo gettype($docdatau); echo "\n"; echo "--------------------------------------\n"; print_r($docdatau); echo "--------------------------------------\n"; echo "2\n"; echo

cast unsigned char * (uint8_t *) to const char *

孤街浪徒 提交于 2020-01-02 06:15:09
问题 I've a function which take an uint8_t * argument : uint8_t* ihex_decode(uint8_t *in, size_t len, uint8_t *out) { uint8_t i, hn, ln; for (i = 0; i < len; i+=2) { hn = in[i] > '9' ? (in[i]|32) - 'a' + 10 : in[i] - '0'; ln = in[i+1] > '9' ? (in[i+1]|32) - 'a' + 10 : in[i+1] - '0'; out[i/2] = (hn << 4 ) | ln; } return out; } I use this function with : uint8_t data[SPM_PAGESIZE]; // SPM_PAGESIZE = 256 bytes uint8_t sysex_data[SPM_PAGESIZE/2]; ihex_decode(data, strlen(data), sysex_data); But in

Delphi (-XE) : casting to a record type with implicit conversion

倖福魔咒の 提交于 2020-01-02 04:40:10
问题 I have a record type with methods, representing an specific hardware measurement type, read from the instrument as a string. The record contains implicit coversion to (and from) a string. If I cast a string as a record type, it seems to work, but is this safe? That is, does casting a string to a record with implicit string conversion call the implicit conversion as per assigning a temporary value? var a: MeasurementRecord; // record type with implicit string conversion & decode methods b: