implicit-cast

Implicit casting in VB.NET

The question is intended for lazy VB programmers. Please. In vb I can do and I won't get any errors. Example 1 Dim x As String = 5 Dim y As Integer = "5" Dim b As Boolean = "True" Example 2 Dim a As EnumType = 4 Dim v As Integer = EnumType.EnumValue Example 3 Private Sub ButtonClick(sender As Object, e As EventArgs) Dim btn ...

Varying behavior for possible loss of precision

In Java, when you do int b = 0; b = b + 1.0; You get a possible loss of precision error. But why is it that if you do int b = 0; b += 1.0; There isn't any error? ...

Auto-(un)boxing fail for compound assignment

Thanks to the implicit casting in compound assignments and increment/decrement operators, the following compiles: byte b = 0; ++b; b++; --b; b--; b += b -= b *= b /= b %= b; b <<= b >>= b >>>= b; b |= b &= b ^= b; And thanks to auto-boxing and auto-unboxing, the following also compiles: Integer ii = 0; ++ii; ii++; --ii; ii--; ii += i...

Implicit cast from now() to a date field

Hello, I have an issue with MySQL 5.1. A datetime data type isn't implicitly casted to match a date column. SELECT * FROM my_table WHERE my_date_field = NOW() This request doesn't return any rows using MySQL 5.1, but works well with version 5.0. If we use CURDATE() instead of NOW() it works both in MySQL 5.0 and MySQL 5.1. If the ca...

Safe & Simple Access to Explicit Interface Members in C#

When I am working with explicit interface implementations in C#, it often becomes necessary to cast an object to one of its interfaces in order to access a member of that interface. Because of the improved reliability and maintainability afforded by compile time type checking, I have always preferred to use implicit conversions to perfor...

C# enum to string auto-conversion?

Is it possible to have the compiler automatically convert my Enum values to strings so I can avoid explicitly calling the ToString method every time. Here's an example of what I'd like to do: enum Rank { A, B, C } Rank myRank = Rank.A; string myString = Rank.A; // Error: Cannot implicitly convert type 'Rank' to 'string' string myStrin...

Implicit array casting in C#

Hi, I have the following classes with an implicit cast operator defined: class A { ... } class B { private A m_a; public B(A a) { this.m_a = a; } public static implicit operator B(A a) { return new B(a); } } Now, I can implicitly cast A to B. But why can't I implicitly cast A[] to B[...

C# Implicit operators and ToString()

Hello, I'm creating my own type for representing css values (like pixels eg. 12px ). To be able to add/subtract/multiply/... my type and ints I've defined two implicit operators to and from int. Everything works great except one thing.. If I write: CssUnitBase c1 = 10; Console.WriteLine(c1); I get "10" instead of "10px" - implicit co...

inconsistent behavior with string+=int in c#

I'm looking at some code golf in LINQPad and wondering why: int c; string o; o+=c;//this works o+=P==2?"."+c:c;//this doesn't o+=P==2?"."+c:""+c;//this does mostly why the first one works and the second one throws a "no implicit conversion between 'string' and 'int'" error. ...

How do you directly type cast a boxed struct in C#?

I have a namespace of structs which represent various units of measure (Meters, Feet, Inches, etc.) ... anout 12 in total, generated courtesy of T4 templates :) . Each struct carries implicit casting operators to support casting the value to any other measurement value-type, so the following sytax is legal: var oneThousandMeters = new ...

Is possible to get automatic cast from user-defined type to std::string using cout?

As in the question, if I define a string operator in my class: class Literal { operator string const () { return toStr (); }; string toStr () const; }; and then I use it: Literal l1 ("fa-2bd2bc3e0"); cout << (string)l1 << " Declared" << endl; with an explicit cast everything goes right, but if I remove the (string) the c...

Why can't I downcast pointer to members in template arguments?

If I make a pointer-to-base-member, I can convert it to a pointer-to-derived-member usually, but not when used within a template like Buzz below, where the first template argument influences the second one. Am I fighting compiler bugs or does the standard really mandate this not work? struct Foo { int x; }; struct Bar : public Foo ...

C# Implicit casting to List<T> returns runtime error

I have a class similar to the following that uses an internal List: public class MyList<T> : IEnumerable<T> { private List<T> _lstInternal; public MyList() { _lstInternal = new List<T>(); } public static implicit operator List<T>(MyList<T> toCast) { return toCast._l...