Returning Strongly Typed Column Values

Returning Strongly Typed Column Values Up to this point, you've retrieved column values from a DataReader only as generic objects of the class | Returning Strongly Typed Column Values Up to this point you ve retrieved column values from a DataReader only as generic objects of the class such objects are often referred to as being of the C object type . Note All classes in C are derived from the class. I ll rewrite the while loop shown in the example in the previous section to show how you store column values as objects of the class while object productID productsSqlDataReader productIDColPos object productName productsSqlDataReader productNameColPos object unitPrice productsSqlDataReader unitPriceColPos object unitsInStock productsSqlDataReader unitsInStockColPos object discontinued productsSqlDataReader discontinuedColPos productID productID productName productName unitPrice unitPrice unitsInStock unitsInStock discontinued discontinued This code results in the same output as Listing . All I did in Listing is to explicitly show that a DataReader returns a column value as an object of the class by default. When an object of the class is displayed by the method the object is first implicitly converted to a string and then displayed. That s fine for just displaying the column values but what if you want to perform some kind of calculation with a value To do that you must first cast the value to a specific type. The following example casts the unitPrice object to a decimal and then multiplies it by decimal newUnitPrice decimal unitPrice Note You add an m to the end of a literal number to indicate it is of the decimal type. Casting an object to a specific type works but it s not very elegant. It also goes against the one of the main benefits of a modern programming language use of strong typing. Strongly typing means that you pick the type of a variable or object when declaring it. The main .

Bấm vào đây để xem trước nội dung
TÀI LIỆU LIÊN QUAN
5    176    1
5    255    1
5    106    0
5    121    1
6    103    1
6    107    1
6    121    1
6    103    0
6    140    0
6    98    0
TÀI LIỆU MỚI ĐĂNG
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.