Option Strict Question (version 1.1)

CoachBarker

Well-known member
Joined
Jul 26, 2007
Messages
133
Programming Experience
1-3
When I turn Option Strict On in a form, I received some errors, most of them I have been able to correct. I have a method to fill the controls in a form this is a shorten version of the code

VB.NET:
 ' Method Name: fill
    ' Purpose: To fill the controls on the form
    ' Parameters: None
    Public Sub fill()
        Try
            If dsForm.Tables(0).Rows.Count > 0 Then
                intglucoseLevelID = CInt(dsForm.Tables(0).Rows(dbCursor).Item(0))
                If Not dsForm.Tables(0).Rows(dbCursor).Item(0) Is DBNull.Value Then
                    Me.txtGlucoseLevelID.Text = CStr(dsForm.Tables(0).Rows(dbCursor).Item(0))
                End If
                ' ****************************************************************
                If Not dsForm.Tables(0).Rows(dbCursor).Item(1) Is DBNull.Value Then
                    Me.txtPatientID.Text = CStr(dsForm.Tables(0).Rows(dbCursor).Item(1))
                End If
                ' ****************************************************************
                If Not dsForm.Tables(0).Rows(dbCursor).Item(2) Is DBNull.Value Then
                    Me.txtDateEntered.Text = CStr(dsForm.Tables(0).Rows(dbCursor).Item(2))
                End If
                ' ****************************************************************
                If Not dsForm.Tables(0).Rows(dbCursor).Item(3) Is DBNull.Value Then
                    Me.txtGlucoseLevel.Text = CStr(dsForm.Tables(0).Rows(dbCursor).Item(4))
                End If
                ' Check boxes will be in the DB as either 0 (false) or 1 (true) - no nulls
                ' ****************************************************************
                If dsForm.Tables(0).Rows(dbCursor).Item(4) = 1 Then
                    Me.cbxDrankSoda.CheckState = CheckState.Checked
                End If
                ' ****************************************************************
            Else
                ' Check to see if there is a Glucose Level Record for this user
                Dim newWeek As String = Date.Now.AddDays(-Date.Now.DayOfWeek).ToShortDateString
                If (intInitialID > 0) Then
                    MessageBox.Show("This is the first record for the week beginning " & newWeek, "New Week")
                    Me.txtPatientID.Text = CStr(intInitialID)
                End If
            End If
            hasChanged = False
        Catch ex As Exception
            MessageBox.Show("An error occured in frmGlucose. Method: fill. Error: " & ex.ToString)
        End Try
    End Sub

The problem I can't resolve is with the Check Boxes, the first error is this;

FirstError.JPG

Resolved by using IS as directed, the second error is this;

SecondError.JPG

Not sure what to do here, any help

Thanks
CoachBarker
 
Last edited:
Try using the .equals method, i.e. "Item(4).equals(1)". Another option is if you know that that item is always going to be an integer, cast it before doing the comparison, i.e. "CType(dsForm.Tables(0).Rows(dbcursor).Item(4), integer) = 1".

Hope that helps,
Dane.
 
The DataRow.Item property is type Object because it can return any type of object at all. If your DataRow contains an Integer then you should cast the Item property value as type Integer:
VB.NET:
If CInt(dsForm.Tables(0).Rows(dbCursor).Item(4)) = 1 Then
 
Thanks, if that was the most obvious error why would the autocoorrect option be to replace the = with IS and not a casting error? Just wondering.

CoachBarker
 
Object type is a class and classes are reference types, the Object type can contain (box) any .Net type, both references and values. "Is" operator is used to compare reference types, "=" operator to compare value types. Since your expression "object = value" is strictly wrong the compiler has to make a choice about what part of the expression is correct and deduce from that what needs to be fixed about the other part.
Using a typed dataset would also have avoided this.
 
All through school we kept Option Strict off in our forms, it sure would have made more sense to have us keep it on, so that we would have run into these types of errors then. Wouldn't be running into these problems now, at least if I did I would be better able to handle them.

Thanks for the help and the explanation from everyone.

CoachBarker
 
I agree. The perception is that it's easier to teach beginners if you don't have to worry about cast and type conversions. That may be true but it aslo means that they are not learning a proper understanding of how their code actually works. I think it's better to learn to write code more slowly and learn how to write good code than it is to jump write in to writing code that is flaky and ill-understood. You still have to learn about proper typing at some point so learning to code before you understand types doesn't make a good developer any more quickly. It just means that you can write rubbish code sooner. Option Strict should have been On by default with the option to turn it Off. From what I've heard of some teachers I doubt many even know about it themselves.
 
Back
Top