Permalink
Cannot retrieve contributors at this time
description | title | ms.date | f1_keywords | helpviewer_keywords | ms.assetid |
---|---|---|---|---|---|
Compiler Error CS0266 |
Compiler Error CS0266 |
07/20/2015 |
CS0266 |
CS0266 |
6cca5d6a-f3e0-482a-af25-af73bfe3e303 |
Compiler Error CS0266
Cannot implicitly convert type ‘type1’ to ‘type2’. An explicit conversion exists (are you missing a cast?)
This error occurs when your code tries to convert between two types that cannot be implicitly converted, but where an explicit conversion is available. For more information, see Casting and Type Conversions.
The following code shows examples that generate CS0266:
// CS0266.cs class MyClass { public static void Main() { // You cannot implicitly convert a double to an integer. double d = 3.2; // The following line causes compiler error CS0266. int i1 = d; // However, you can resolve the error by using an explicit conversion. int i2 = (int)d; // You cannot implicitly convert an object to a class type. object obj = new MyClass(); // The following assignment statement causes error CS0266. MyClass myClass = obj; // You can resolve the error by using an explicit conversion. MyClass c = (MyClass)obj; // You cannot implicitly convert a base class object to a derived class type. MyClass mc = new MyClass(); DerivedClass dc = new DerivedClass(); // The following line causes compiler error CS0266. dc = mc; // You can resolve the error by using an explicit conversion. dc = (DerivedClass)mc; } } class DerivedClass : MyClass { }
error CS0266: Cannot implicitly convert type ‘object’ to ‘int’. An
explicit conversion exists (are you missing a cast?)
int dd= 6000;
sqlCmdDefaultTime = new SqlCommand("myQuery", sqlCon);
sqlDefaultTime = sqlCmdDefaultTime.ExecuteReader();
while (sqlDefaultTime.Read())
{
dd= sqlDefaultTime[1];
}
how can i cast
asked Jan 17, 2012 at 12:57
2
Simple cast to int
:
dd = (int)sqlDefaultTime[1];
answered Jan 17, 2012 at 12:58
ken2kken2k
48k10 gold badges114 silver badges175 bronze badges
Try this…
int.TryParse(sqlDefaultTime[1].ToString(), out dd);
in the event that the parse is successful dd
will now be a new value.
Unless of course the object is an int already, the you can just cast it…
dd = (int)sqlDefaultTime[1];
answered Jan 17, 2012 at 12:59
musefanmusefan
47.8k21 gold badges134 silver badges185 bronze badges
Integer.parseInt();
a few others such as TryParse and TryParseExact provide more functionality and control.
dd= Integer.parseInt(sqlDefaultTime[1]);
should do it, provided you can guarantee the value being returned is always an int.
answered Jan 17, 2012 at 12:59
ChrisChris
2,47125 silver badges36 bronze badges
C# Compiler Error
CS0266 – Cannot implicitly convert type ‘type1’ to ‘type2’. An explicit conversion exists (are you missing a cast?)
Reason for the Error
You’ll get this error in your C# code when you try to convert between two types which cannot be implicitly converted. One of the simplest example of this error code is when you try to try to use the implicit conversion from double to int which is not allowed in C#.
For example, lets try to compile the below code snippet.
using System; namespace DeveloperPubNamespace using System; namespace DeveloperPubNamespace { class Program { public static void Main() { double input = 3.2; // This will result in the C# error code CS0266 int outPut = input; Console.WriteLine(outPut); } } }
You’ll receive the error code CS0266 when you try to build the above C# program because you are assigning the double value to an int variable and expecting the C# implicit conversion to take care of the conversion.
Error CS0266 Cannot implicitly convert type ‘double’ to ‘int’. An explicit conversion exists (are you missing a cast?) DeveloperPublish C:UsersSenthilsourcereposConsoleApp3ConsoleApp3Program.cs 11 Active
Solution
You can fix this error in your C# program by explicitly converting to the specified type. The above program can be fixed by explicitly converting the double to int.
using System; namespace DeveloperPubNamespace { class Program { public static void Main() { double input = 3.2; // This will result in the C# error code CS0266 int outPut = (int)input; Console.WriteLine(outPut); } } }
EnzoLu 0 / 0 / 0 Регистрация: 29.04.2020 Сообщений: 5 |
||||
1 |
||||
30.04.2020, 16:48. Показов 4176. Ответов 1 Метки нет (Все метки)
Есть код и в нём есть генерация рандомных цифр. ну и конечно же ошибка
0 |
I2um1 Злой няш 2136 / 1505 / 565 Регистрация: 05.04.2010 Сообщений: 2,881 |
||||
30.04.2020, 16:52 |
2 |
|||
Сообщение было отмечено EnzoLu как решение Решение
1 |
1 Answer
Looks to me like Product.Price is defined as an int rather than a double. We can’t know that, though, because we can’t see your class Product(). The error is extremely self-explanatory — it is telling you that you are feeding a double value to an int variable somewhere. I’m not one of the smack-down types on this board, but this is a Google question more than an SO question. IF you still need help, post your Product() class and everthing that gets sent to it.
answered Aug 27, 2016 at 1:56
1
-
I disagree. The question is «why do I get this error» and the answer is «because you’re feeding a double value to an int variable.» Asked, answered.
Aug 27, 2016 at 22:56