casting

Java Casting method without knowing what to cast to

谁都会走 提交于 2020-01-02 04:08:05
问题 I was playing around with Java today, and I noticed something weird. Consider this code: String foo = cast("hi"); int bar = cast("1"); The cast() method is here: public static <T> T cast(Object value) { return (T) value; } Java seems to cast "hi" to a String, even if I didn't pass any hint that it would be a String , with the exception of the type. It does foo well, but fails on bar , because you can't case a String to an Integer. What is happening here? I have two guesses: The cast method is

Convert string to int inside WHERE clause of SQLITE statment

£可爱£侵袭症+ 提交于 2020-01-02 04:02:26
问题 I want to achieve this: SELECT * FROM linkledger WHERE toInt(reputation) > 100; but the function toInt doesnt exist? Is there one? I found this now but not working, which implies i have a more fundemental problem. BECAUSE THIS IS CORRECT SELECT * FROM linkledger WHERE cast(reputation AS INTEGER) > 100; 回答1: Use CAST(reputation AS INTEGER). 回答2: I will leave Anik Islam Abhi's answer as the correct one because it will apply in most cases but I just want it on record that I only managed to fix

Cast to concrete class and call method in Java

二次信任 提交于 2020-01-02 03:56:08
问题 Lets say we have a baseclass called A and some subclasses ( B , C , D , etc.). Most subclasses have the method do() but the baseclass does not. Class AA provides a method called getObject() , which will create an object of type B , or C or D , etc., but returns the object as type A . How do I cast the returned object to the concrete type and call its do() method, if this method is available? EDIT: I'm not allowed to change the implementation of Class A , the subclasses or AA , since im using

Java: Can I convert List of Object to List of String[] and vice versa?

自闭症网瘾萝莉.ら 提交于 2020-01-02 03:48:25
问题 Is this possible without going through the list and casting the objects? I also need to convert List<Object> to List<T> (T = predefined Object) if it's possible? Edit: for clarification, I'm trying to use List<Object> as a return type of a class method that is widely used in my code. 回答1: No. This is simply not a valid conversion, because not all Object s are String[] . You could have determined this for yourself in 2 lines of code. Edit It sounds like you need to write the method more

Specified cast is not valid Linq Query

杀马特。学长 韩版系。学妹 提交于 2020-01-02 03:37:10
问题 Hi i would like to query a table based on its primary key. i have tried both var deviceDetails = (from d in db.Devices where d.DeviceId == pointDetails.DeviceId select d).ToList(); EDIT d.DeviceId var deviceDetails = db.Devices.Single(d => d.DeviceId == pointDetails.DeviceId) I know these return different types but this is not the issue right now. This statement throws an invalidCastException and i dont know why. PointDetails.DeviceId is definitely a valid int. The exception is thrown even if

How to implement a compile-time check that a downcast is valid in a CRTP?

做~自己de王妃 提交于 2020-01-02 02:33:10
问题 I have a plain old CRPT (please don't get distracted by access restrictions - the question is not about them): template<class Derived> class Base { void MethodToOverride() { // generic stuff here } void ProblematicMethod() { static_cast<Derived*>(this)->MethodToOverride(); } }; that is as usual intended to be used like this: class ConcreteDerived : public Base<ConcreteDerived> { void MethodToOverride() { //custom stuff here, then maybe Base::MethodToOverride(); } }; Now that static_cast

Extension method and Explicit casting

丶灬走出姿态 提交于 2020-01-02 02:04:58
问题 I'm using class from some assembly(source code is not available), so it is not possible to change their's code I need to add extension method for explicit cast operator, is there any way to achieve that? (I have tried to add as regular extension method, but without success) public static explicit operator MembershipUser(this MembershipUser membership, User user) { return new MembershipUser("SimplyMembershipProvider", user.UserName, user.UserId, user.Email, null, null, user.IsApproved, user

Extension method and Explicit casting

不羁的心 提交于 2020-01-02 02:03:02
问题 I'm using class from some assembly(source code is not available), so it is not possible to change their's code I need to add extension method for explicit cast operator, is there any way to achieve that? (I have tried to add as regular extension method, but without success) public static explicit operator MembershipUser(this MembershipUser membership, User user) { return new MembershipUser("SimplyMembershipProvider", user.UserName, user.UserId, user.Email, null, null, user.IsApproved, user

Preserving PowerShell function return type

前提是你 提交于 2020-01-02 01:46:09
问题 In my PowerShell script I need to call a .NET method with the following signature: class CustomList : System.Collections.Generic.List<string> { } interface IProcessor { void Process(CustomList list); } I made a helper function to generate the list: function ConvertTo-CustomList($obj) { $list = New-Object CustomList if ($obj.foo) {$list.Add($obj.foo)} if ($obj.bar) {$list.Add($obj.bar)} return $list } Then I call the method: $list = ConvertTo-CustomList(@{'foo'='1';'bar'='2'}) $processor

Cannot cast from ArrayList<Parcelable> to ArrayList<ClSprite>

爷,独闯天下 提交于 2020-01-02 01:11:10
问题 In a piece of code I've written, I have this line: AllSprites = (ArrayList<ClSprite>) savedInstanceState.getParcelableArrayList("AllSprites"); I'm getting an error about an invalid cast from an ArrayList<Parcelable> to ArrayList<ClSprite> . Why isn't this legal? 回答1: It is fundamentally unsafe to cast an ArrayList<Derived> to an ArrayList<Base> or vice-versa. Doing so opens up a hole in the type system, and Java will throw a ClassCastException at runtime if you try this. The reason is that I