casting

Casting a interface derived class to another derived class

最后都变了- 提交于 2019-12-31 07:19:29
问题 I am trying to accomplish a way to cast a given object that is derived from an interface to a different type that is also derived from the same interface. This to prevent having to completely rebuild the object. interface IItem { } class FryingPan : IItem { } class CookingPan : IItem { } But here is the catch, I don't 'know' what type I am casting to. I only know by a type variable. This is set from a combobox. Type SelectedItem { get; set; } Can this be done ( preferably without having to

Creating an array of concrete instances of a generic class

吃可爱长大的小学妹 提交于 2019-12-31 04:09:16
问题 I have a generic class Position<E> and a concrete class Card . How do I create an array of type Position<Card> ? I tried: Position<Card>[] suitPositions = (Position<Card>[]) new Object[5]; but I get a ClassCastException. But when I try: Position<String>[] suitPositions = (Position<String>[]) new Object[5]; it works just fine. 回答1: Use the raw type to create the array: Position<String>[] suitPositions = new Position[5]; 来源: https://stackoverflow.com/questions/35821682/creating-an-array-of

What does C# do when typecasting a letter to an int?

☆樱花仙子☆ 提交于 2019-12-31 04:04:13
问题 We've to implement an encryption for an external interface. The owner of the interface has given documentation of how to preform the same encryption on our side. However, this documentation is in C# and we work in PHP. Most of the parts we understand except for where they seem to typecast a hash to an int. Their code reads: // hashString exists and is a md5 a like string int[] keyBuffer = new int[hashString.length]; for (int i=0; i<hashString.length; i++) { keyBuffer[i] = (int)hashString[i];

Dynamic Method Dispatch in java

风格不统一 提交于 2019-12-31 03:42:27
问题 class A{ int a=10; public void show(){ System.out.println("Show A: "+a); } } class B extends A{ public int b=20; public void show(){ System.out.println("Show B: "+b); } } public class DynamicMethodDispatch { public static void main(String[] args) { A aObj = new A(); aObj.show(); //output - 10 B bObj = new B(); bObj.show(); //output - 20 aObj = bObj; //assigning the B obj to A.. aObj.show(); //output - 20 aObj = new B(); aObj.show(); //output - 20 System.out.println(bObj.b); //output - 20 /

Unable To Cast object type 'System.String[*]' to type 'System.String[]'

天涯浪子 提交于 2019-12-31 03:33:05
问题 Hi everyone i had a issue with a code in C# .NET, I'm using a DLL for connect to OPC Servers the DLL that was used in a VB.NET project and works with no problem at all. I'm trying to show a list of available Servers in a ListBox, the code used in VB.NET (and works) is this one: Dim AllOPCServers As Object AllOPCServers = AnOPCServer.GetOPCServers ' Load the list returned into the List box for user selection Dim i As Short For i = LBound(AllOPCServers) To UBound(AllOPCServers)

In java if “char c = 'a' ” why does “c = c + 1” not compile?

你说的曾经没有我的故事 提交于 2019-12-31 03:03:44
问题 I tried to compile the following code: public static void main(String[] args){ for (char c = 'a'; c <='z'; c = c + 1) { System.out.println(c); } } When I try to compile, it throws: Error:(5, 41) java: incompatible types: possible lossy conversion from int to char The thing is, it does work if I write c = (char)(c + 1) , c += 1 or c++ . I checked and the compiler throws a similar error when I try char c = Character.MAX_VALUE + 1; but I see no way that the value of 'c' can pass 'char' type

Cast object to array - strange behaviour [duplicate]

廉价感情. 提交于 2019-12-31 02:59:08
问题 This question already has answers here : Array to Object and Object to Array in PHP - interesting behaviour (2 answers) Closed 6 years ago . I am casting an object to array and after that I am unable to access the resulting array by a key. This is the code print_r($new); $new = (array)$new; echo $new['EPPContactemail']; foreach($new as $attr=>$value) { echo "$attr => $value \n"; } And the output is EPPContact Object ( [id:EPPContact:private] => 6553377C74FC9899 [roid:EPPContact:private] =>

Why does this conversion doesn't work?

戏子无情 提交于 2019-12-31 02:43:12
问题 following code behaves strange (at least for me): int testValue = 1234; this.ConversionTest( testValue ); private void ConversionTest( object value ) { long val_1 = (long) (int) value; // works long val_2 = (long) value; // InvalidCastException } I don't understand why the direct (explicit) cast to long doesn't work. Can someone explain this behaviour? Thanks 回答1: The value parameter of your ConversionTest method is typed as object ; this means that any value types -- for example, int --

How would std::ostringstream convert to bool?

陌路散爱 提交于 2019-12-31 02:35:11
问题 I stumbled across this code. std::ostringstream str; /// (some usage) assert( ! str ); What does ostringstream signify when used in a bool context? Is this possibly an incorrect usage that happens to compile and run? 回答1: It tells you if the stream is currently valid. This is something that all streams can do. A file stream, for example, can be invalid if the file was not opened properly. As a side note, this functionality (testing a stream as a bool) is achieved by overloading explicit

Can anyone explain why strtotime('cast') returns a value?

筅森魡賤 提交于 2019-12-31 01:48:08
问题 Not really an issue (although it is conflicting with an if() statement we have), but when you type in strtotime('cast'), it returns an actual value that defaults to today's date. I was just wondering if anyone knew what significance the word cast has when it comes to time functions Thanks! 回答1: It maps to the timezone offset for "Australia/Adelaide". Example: echo date('Y-m-d H:i:s'), "\n", date('Y-m-d H:i:s', strtotime("cast")), "\n", date('Y-m-d H:i:s', strtotime("Australia/Adelaide"));