VB.NET: Option Strict ON conversion

toytoy

Well-known member
Joined
Jul 16, 2004
Messages
46
Programming Experience
1-3
I heard that working on codes with Option Strict ON give me a better way to learn programming....
Say I have turn the Option Strict ON....
all the coding works well when the option Strict is OFF.....

1) How to convert the error "String to Integer" in this case
VB.NET:
Dim data As String
For Each nod As XmlNode In xdoc.SelectNodes("//time")
	data = nod.Attributes("hour").Value
	cboVHour.SelectedIndex = data
Next
2) Secondly, I have this String which take in Integer, how to solve the error "Integer to String" in the second case.
VB.NET:
Dim dd As Integer
dd = Now.Day
lblDay.Text = dd
3) Lastly, I have an Xml String and I wanted to display an additional day to it in the Xml String. How to solve thr error "String to Double" in this third case.
VB.NET:
 XmlString = <OK> dd + 1 </OK>
Thanks
 

Paszt

Staff member
Joined
Jun 3, 2004
Messages
1,500
Location
Raleigh, NC - USA
Programming Experience
Beginner
Turning Options Strict On is definitely something everyone should do, unless there's a very good reason (using lots of late binding for example). Having Option Strict On is a performance helper (by elimitating hidden type conversions) and catches lots of possible errors. In your first example for instance, if the XML file had a non-numeric value for some reason, the code would throw an exception (you should use Try/Catch blocks).

You'll want to use conversion functions to solve all these questions. Here's a good article that explains the best practives of those different conversion functions: Visual Basic .NET Internals. Look for a section called 'Conversion Functions, CType, DirectCast, and System.Convert'

VB.NET:
'1:
For Each nod As XmlNode In xdoc.SelectNodes("//time")
    cboVHour.SelectedIndex = CInt(nod.Attributes("hour").Value)
Next

'2:
Dim dd As Integer
dd = Now.Day
lblday.Text = dd.ToString

'3:
???
 
Top Bottom