Option Strict

C

Charlie Smith

Hi all,
I have been following this ng lately and appreciate the level of
understanding I see as well as the dedication to helping others. Many
thanks for all the useful information I have picked up.

I have seen the recommendation for 'option strict' many times and
realized that I had not set it on in my current project. When I did I
discovered a number of small things and one that has me stumped. I am
sure that is a result of my lack of understanding of VB.NET, so I hope
you can help. Here is the code:

Private Sub InitializeCombo()
Dim objDataRow As DataRow
Dim mIndex As Integer
Dim mRowID As Integer
Dim objItem As ComboBoxItem

Me.ComboBoxUser.Items.Clear()

For mIndex = 0 To localData.Tables(0).Rows.Count - 1

objDataRow = localData.Tables(0).Rows.Item(mIndex)

objItem = New
ComboBoxItem((Int32.Parse(objDataRow.Item("lID").ToString)),
(objDataRow.Item("lUser").ToString))

Me.ComboBoxUser.Items.Add(objItem)

Next

Me.ComboBoxUser.SelectedIndex = 0

End Sub



Dim objItem As ComboBoxUser = Me.ComboBoxUser.SelectedItem

the last line is in several places and makes perfect sense to me, but
option strict tells me "Option Strict On disallows implicit
conversions from System.Object to StorageMinder.ComboBoxItem"

since the ComboBoxUser.Items collection was built with ComboBoxItem
objects, can't I expect it to return same?

Thanks,

Charlie Smith
 
T

Tom Spink

Option Strict forces you to explicitally cast all types, etc... try this:

Dim objItem As ComboBoxUser = CType(Me.ComboBoxUser.SelectedItem,
ComboBoxUser)

--
HTH,
-- Tom Spink, Über Geek

Please respond to the newsgroup,
so all can benefit

" System.Reflection Master "

==== Converting to 2002 ====
Remove inline declarations
 
H

Herfried K. Wagner [MVP]

* "Tom Spink said:
Option Strict forces you to explicitally cast all types, etc... try this:

Dim objItem As ComboBoxUser = CType(Me.ComboBoxUser.SelectedItem,
ComboBoxUser)

Why not use 'DirectCast' here?
 
C

Charlie Smith

Tom Spink said:
Option Strict forces you to explicitally cast all types, etc... try this:

Thanks Tom, that works fine. Still seems a bit strange to me, but
maybe I'm just getting old (my daughter keeps telling me I'm "older
than dirt").

Charlie
 
J

Jay B. Harlow [MVP - Outlook]

Charlie,
As Herfried asked, I would recommend using DirectCast instead of CType. As
CType is used to "convert" a type, where DirectCast is used to Cast a type.
Casting simply says allow this assignment if I am really that type. Where as
"convert" says find a function to convert the object from the type it really
is to a type that will work. CType will physically change an Integer into a
String, while DirectCast will see that Integer is not a string and give an
error.

The reason DirectCast is needed is that the SelectedItem property is of type
Object, being Object SelectedItem may not be a ComboBoxUser object. The
compiler is not going to assume that the SelectedItem is a ComboBoxUser
object, you need to tell it is with DirectCast or CType...

DirectCast is telling the compiler you know that SelectedItem really
contains a ComboBoxUser, so go ahead & allow the assignment. At runtime if
SelectedItem is not really a ComboBoxUser you will still get an exception,
as you effectively lied to the compiler ;-)! You can use TypeOf to prevent
this exception.
Dim objItem As ComboBoxUser
If TypeOf Is ComboBoxUser Then
objItem = DirectCast(Me.ComboBoxUser.SelectedItem, ComboBoxUser)
End If

Hope this helps
Jay
 
T

Tom Spink

Aha! I knew it.... I was waiting for it, as soon as I posted it..... Nice
one Herfried ;-)

Herfried is of course correct, it would be much more efficient to use
DirectCast here, as opposed to CType, as long as you're sure only
ComboBoxUser's are the objects in the list.

--
HTH,
-- Tom Spink, Über Geek

Please respond to the newsgroup,
so all can benefit

" System.Reflection Master "

==== Converting to 2002 ====
Remove inline declarations
 
C

Charlie Smith

Thanks everyone! As usual the response is exceptional, I appreciate
the depth of the learning experience here, happy newe year to all.
Charlie
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top