Prizes & Awards
My Profile
Active Members
TodayLast 7 Days
more...
|
Resources » Errors and Solutions » General »
Cannot Implicitly Convert Type ‘x’ Into ‘y’ Error in C#
|
This error usually indicates that you're trying to use two different variable types in the same expression.
For Exampe,
int age = 10; // Generates an error message. int factoredAge = 2.0 * age;
The problem here is that 2.0 is a variable of type double. The int age multiplied by the double 2.0 results in a double value. C# does not automatically store a double into the int variable factoredAge because information may be lost.
Some conversions are not obvious, as in the following example:
class MyClass { static public float FloatTimes2(float f) { \\ This generates a build time error. float result = 2.0 * f; return result; } }
You may think that doubling a float would be okay, but that's sort of the problem. 2.0 is not a float — it defaults to type double. A double times a float is a double. C# does not store a double value back into a float variabe.
The following version of FloatTimes2() works fine.
class MyClass { static public float FloatTimes2(float f) { \\ This works fine. float result = 2 * f; return result; } } The constant 2 is of type int. An int times a float is a float, which can be stored in the float variable result.
The implicit-conversion error message can also arise when performing operations on "unnatural" types. For example, you cannot add two char variables, but C# can convert char variables into int values for you when necessary to get the job done. This leads to the following:
class MyClass { static public void SomeMethod() { char c1 = 'a'; char c2 = 'b'; char c3 = c1 + c2; } } Adding two characters together makes no sense, but C# tries anyway. Because addition isn't defined for type char, it converts c1 and c2 into int values and then performs the addition. Unfortunately, the resulting int value cannot be converted back into a char without some help from an explicit cast to char.
Most, but not all, conversions are okay with an explicit cast. Thus, the following method works without problem:
class MyClass { static public float FloatTimes2(float f) { \\ This works OK with the explicit cast. float result = (float)(2.0 * f); return result; } } The result of 2.0 * f is still of type double, but the programmer has indicated that she specifically wants the result down-converted to a float, even in the unlikely event that it results in the loss of data.
A second approach would be to make sure that all constants are of the same type, as follows:
class MyClass { static public float FloatTimes2(float f) { \\ This works OK with the explicit cast. float result = 2.0F * f); return result; } } This version of the method uses a constant 2.0 of type float rather than the default double. A float times a float is a float. (Signify a float by appending ‘F’ or ‘f’ to a literal number like
|
Responses
|
No responses found. Be the first to respond and make money from revenue sharing program.
|
|