How to cast int to enum in C++?
int i = 1; Test val = static_cast<Test>(i);
int i = 1; Test val = static_cast<Test>(i);
If X can really be cast to Y you should be able to use List<Y> listOfY = listOfX.Cast<Y>().ToList(); Some things to be aware of (H/T to commenters!) You must include using System.Linq; to get this extension method This casts each item in the list – not the list itself. A new List<Y> will be created … Read more
double num = 5; That avoids a cast. But you’ll find that the cast conversions are well-defined. You don’t have to guess, just check the JLS. int to double is a widening conversion. From ยง5.1.2: Widening primitive conversions do not lose information about the overall magnitude of a numeric value. […] Conversion of an int … Read more
Simply casting to List<TestB> almost works; but it doesn’t work because you can’t cast a generic type of one parameter to another. However, you can cast through an intermediate wildcard type and it will be allowed (since you can cast to and from wildcard types, just with an unchecked warning): List<TestB> variable = (List<TestB>)(List<?>) collectionOfListA;
if(!(child is IContainer)) is the only operator to go (there’s no IsNot operator). You can build an extension method that does it: public static bool IsA<T>(this object obj) { return obj is T; } and then use it to: if (!child.IsA<IContainer>()) And you could follow on your theme: public static bool IsNotAFreaking<T>(this object obj) { … Read more
You can use Double.parseDouble() to convert a String to a double: String text = “12.34”; // example String double value = Double.parseDouble(text); For your case it looks like you want: double total = Double.parseDouble(jlbTotal.getText()); double price = Double.parseDouble(jlbPrice.getText());
[float(i) for i in lst] to be precise, it creates a new list with float values. Unlike the map approach it will work in py3k.
You can cast your timestamp to a date by suffixing it with ::date. Here, in psql, is a timestamp: # select ‘2010-01-01 12:00:00′::timestamp; timestamp ——————— 2010-01-01 12:00:00 Now we’ll cast it to a date: wconrad=# select ‘2010-01-01 12:00:00’::timestamp::date; date ———— 2010-01-01 On the other hand you can use date_trunc function. The difference between them is … Read more
Your understanding is true. That sounds like trying to micro-optimize to me. You should use a normal cast when you are sure of the type. Besides generating a more sensible exception, it also fails fast. If you’re wrong about your assumption about the type, your program will fail immediately and you’ll be able to see … Read more
Try MyEnum.values()[x] where x must be 0 or 1, i.e. a valid ordinal for that enum. Note that in Java enums actually are classes (and enum values thus are objects) and thus you can’t cast an int or even Integer to an enum.