|
|
|
Forum Member
      
участник
Last Login: 14.09.2004 17:16
Сообщ.: 42,
Visits: 463
|
|
Пример из МСДН
i=(int)(sqlCommand1.ExecuteScalar());
Во время выполнения пишет ошибка приведения типов, но почему?
|
|
|
|
|
новичок
      
участник
Last Login: 11.04.2002 16:35
Сообщ.: 2,
Visits: 23
|
|
Вполне возможно, что результат работы функции просто напросто не содержит значение типа int. Читать надо про boxing/unboxing (упаковку/распаковку). Так вот распаковку можно провести только в тот тип, который был предварительно упакован. Пример из книг
int i = 5; object o = i; int j = (int)o; //правильно double d = (double)o; //неправильно, а правильно будет double d1 = (double)(int)o;
|
|
|
|
|
Junior Member
      
участник
Last Login: 30.04.2002 9:24
Сообщ.: 12,
Visits: 133
|
|
А я бы еще поглядел в дебагере не явлеятся ли результатом выполнения ExecuteScalar null.. В этом случае точно будет ошибка..
VoRoN www.aspnetmania.com
|
|
|
|
|
Forum Member
      
участник
Last Login: 14.09.2004 17:16
Сообщ.: 42,
Visits: 463
|
|
Это все понятно. Ругался он на запрос SELECT @@IDENTITY. Я поменял это на SELECT CAST(@@IDENTITY AS INT) и все стало нормально. Однако непонятно, ведь значение этой переменной - целое и так. И еще один момент заметил. Например в таблице test есть поле ftest MONEY. Делаю SELECT ISNULL(ftest, 0.0) FROM test. Ругается на эти значения, приходится делать также как и выше SELECT ISNULL(ftest, CAST(0.0 AS MONEY)). Неужели сам не приводит столбец к одному типу?
|
|
|
|
|
Forum Member
      
участник
Last Login: 20.03.2003 9:10
Сообщ.: 29,
Visits: 320
|
|
Data type conversion in VB .NET is very similar to earlier VB versions. When you convert types, you can use built-in functions to convert from one type to another. There are two types of data conversion: widening and narrowing.
- Widening: Conversion is able to maintain the original data value, without data loss. - Narrowing: Conversion attempts to convert data from a larger type to a smaller type (in bytes or precision) that may not be able to maintain the original value.
An example of narrowing conversion would be the following: Dim X as Single = 123.45 Dim Y as Integer Y = X In this case, the decimal places are lost; an integer value does not have precision, so it cannot hold numbers to the right of the decimal place. In VS .NET, if you turn Option Strict to ON the application doesn't compile unless you perform an explicit conversion between the data types. This is a good thing; the compiler in all earlier VB versions did not offer this catch, and bugs were easily introduced due to conversion errors
|
|
|
|