Click here to Skip to main content
15,915,078 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi all
I need to know what is the real difference between these two code snippets
and which is better?


VB
'create the connection and command objects
do while r.read = true
  memberid = convert.toint32(r.item(0))
  membername = convert.tostring(r.item(1))


and this:
VB
'create the connection and command objects
do while r.read = true
  memberid = r.item(0)
  membername = r.item(1)
Posted

1 solution

The return type of Item property of SqlDataReader is Object as explained here http://msdn.microsoft.com/en-us/library/y64yaah2.aspx[^]

When a Visual Basic project is created in Visual Studio, by default the Option Strict is off, hence the implicit conversion from Object to String or Integer are allowed, hence, the second set of statements could be compiled.

If the Option Strict is turned on On (by right clicking on the project, selecting the properties menu option, and in the compile Tab of the property pages), then the second set of statements will not be compiled and the following error will be displayed

Option Strict On disallows implicit conversions from Object to String, Integer as case may be.

In C Sharp the Explicit cast is a must.
So, I think, it is preferable to use the first set of statements.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900