More broken code from option strict fiasco..

bones

Well-known member
Joined
Aug 23, 2014
Messages
143
Programming Experience
Beginner
I thought I had things all working....WRONG

I have turned off Option Strict now..

This code copies data from a datagrid on the main form to a datagrid on the dataanalysis form... NOT ANYMORE.

It throws no errors and the confirmation messagebox pops up on the screen like it's supposed to if the data gets copied.

I can't see what's wrong with it...

VB.NET:
 Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        ' Copy captured data to the DataAnalysis Form Datagridview
        'Error Checking Start
        DataAnalysis.DGV1.AllowUserToAddRows = True
        If Me.RadioButton3.Checked = False And Me.RadioButton4.Checked = False Then
            MessageBox.Show("Select Test Type [Intake or Exhaust]", " Data Analysis Functions", _
            MessageBoxButtons.OK, MessageBoxIcon.Information)
            Exit Sub


        ElseIf Me.TextBox15.Text = ("") Then
            MessageBox.Show("Enter Starting Lift Value", " Data Analysis Functions", _
            MessageBoxButtons.OK, MessageBoxIcon.Information)
            Exit Sub


        ElseIf Me.TextBox16.Text = ("") Then
            MessageBox.Show("Enter Increment Lift Value", " Data Analysis Functions", _
            MessageBoxButtons.OK, MessageBoxIcon.Information)
            Exit Sub


        ElseIf Me.TextBox17.Text = ("") Then
            MessageBox.Show("The Set Lift Data Button Was Not Clicked", " Data Analysis Functions", _
            MessageBoxButtons.OK, MessageBoxIcon.Information)
            Exit Sub


        ElseIf DataGridView1.RowCount < (1) Then
            MessageBox.Show("No Data To Process", " Data Analysis Functions", _
            MessageBoxButtons.OK, MessageBoxIcon.Information)
            Exit Sub
        End If
        ' ^^^^Data Error Checking End


        'Capture Process Begins
        For Each r As DataGridViewRow In DataGridView1.Rows
            If r.IsNewRow Then Continue For


            DataAnalysis.DGV1.AllowUserToAddRows = True
            'r.Cells(0).Value is the current row's first column, r.Cells(1).Value is the second column, etc
            Dataanalysis.DGV1.Rows.Add({r.Cells(0).Value, r.Cells(1).Value, r.Cells(2).Value, r.Cells(3).Value})
        Next
        For Each r As DataGridViewRow In Dataanalysis.DGV1.Rows
            If r.IsNewRow Then Continue For
            Select Case True
                Case Me.RBtnPort1.Checked
                    If r.Cells(10) Is ("") Then
                        r.Cells(10).Value = Me.RBtnPort1.Text
                    End If
                Case Me.RBtnPort2.Checked
                    If r.Cells(10) Is ("") Then
                        r.Cells(10).Value = Me.RBtnPort2.Text
                    End If
                Case Me.RBtnPort3.Checked
                    If r.Cells(10) Is ("") Then
                        r.Cells(10).Value = Me.RBtnPort3.Text
                    End If
                Case Me.RBtnPort4.Checked
                    If r.Cells(10) Is ("") Then
                        r.Cells(10).Value = Me.RBtnPort4.Text
                    End If
                Case Me.RBtnPort5.Checked
                    If r.Cells(10) Is ("") Then
                        r.Cells(10).Value = Me.RBtnPort5.Text
                    End If
                Case Me.RBtnPort6.Checked
                    If r.Cells(10) Is ("") Then
                        r.Cells(10).Value = Me.RBtnPort6.Text
                    End If
            End Select
        Next


        numCols = Dataanalysis.DGV1.ColumnCount
        numRows = Dataanalysis.DGV1.RowCount
        currCell = Dataanalysis.DGV1.CurrentCell




        If currCell.ColumnIndex >= 0 Then
            If currCell.RowIndex < numRows - 1 Then
                Dataanalysis.DGV1.CurrentCell = Dataanalysis.DGV1.Item(0, currCell.RowIndex + 1)
            End If
        Else
            Dataanalysis.DGV1.CurrentCell = Dataanalysis.DGV1.Item(currCell.ColumnIndex, currCell.RowIndex + 1)
        End If
        Dataanalysis.DGV1.FirstDisplayedScrollingRowIndex = Dataanalysis.DGV1.RowCount - 1


        MessageBox.Show("Captured Data Copied To Data Anaylsis Form", "Data Analysis Functions", _
        MessageBoxButtons.OK, MessageBoxIcon.Information)
    End Sub
 
Have you debugged the code, i.e. have you stepped through it line by line and checked at each step that execution takes the course that you expect and that each variable and other relevant expression evaluates to the value that you expect?
 
If r.Cells(10) Is ("") Then
This code is checking if a DataGridViewCell is the same object as the empty string object.

You probably want to check if a cell string value equals an empty string:
If CStr(r.Cells(10).Value) = String.Empty Then

You could also check if a cell value is Nothing:
If r.Cells(10).Value Is Nothing Then

Comparison Operators (Visual Basic)
 
Thanks for the reply guys...much appreciated ... I know i'm a pain in the a$$...sorry for that. Here's what got it working ....

VB.NET:
 Private Sub Chart1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Chart1.Click
        'Show the Data Analysis Form
        'Dim dataanalysis As New DataAnalysis ..........remarked this line out that I had to add to get the form to open when I had Option Strict = ON....
        Dataanalysis.ShowDialog()


    End Sub

Naturally this leaves me with more question....

Why on earth did I need to add that line for that particular form when option strict was ON?
Why did removing it fix the problem when STRICT is OFF?

Here's the real killer for me. I have other forms that did not have the DIM formname as new formname and they opened without issue with STRICT = ON..... But that form would not...

So what's the solution here? Being new to this and floundering as much as I make progress, I really need to nail the basic concepts.... I NEVER struggled like this with VBA....
 
Last edited:
One more thing....How can I step through just a section of code... I can't get that to work. I tried it numerous times... When I use the step into button... it goes on from the beginning of the application and ultimately opens the main form...then that's it...can't go any further...

I opened the immediate window to try and track things but as soon as the main form runs...that's it until I end debug...

I want to be able to step through an entire sub...anywhere, on any form.... do I need to set an option somewhere for that?
 
One more thing....How can I step through just a section of code... I can't get that to work. I tried it numerous times... When I use the step into button... it goes on from the beginning of the application and ultimately opens the main form...then that's it...can't go any further...

I opened the immediate window to try and track things but as soon as the main form runs...that's it until I end debug...

I want to be able to step through an entire sub...anywhere, on any form.... do I need to set an option somewhere for that?

You have to place a breakpoint on the line at which you want execution to break and then you can step from there. Use the F9 key to place a breakpoint on the selected line or click in the left margin beside a line. Step Over will then step to the next line in the current block and Step Into will step into a method call if there is one.
 
You have to place a breakpoint on the line at which you want execution to break and then you can step from there. Use the F9 key to place a breakpoint on the selected line or click in the left margin beside a line. Step Over will then step to the next line in the current block and Step Into will step into a method call if there is one.

Thank You... any ideas on the .showdialog() scenario I posted above?
 
Thank You... any ideas on the .showdialog() scenario I posted above?

Maybe if you were to provide the original error message. They are rather handy diagnostic tools.

I think that it's probably time that you learned what Option Strict actually does and then you can make meaningful changes to your code instead of guessing and getting annoyed when they don't work. Option Strict controls two things, i.e. whether or not implicit narrowing conversions are allowed and whether or not late-binding is allowed. They are both allowed when Option Strict is Off and neither is allowed when it's On.

When converting between data types, there are widening conversions and narrowing conversions. A narrowing conversion is one where some data may be lost and a widening conversion is one where no data can be lost. An implicit conversion is one the the system does for you and an explicit conversion is one that you do yourself with an operator or method. With Option Strict On, all narrowing conversions must be done explicitly. For example, if you have a TextBox in which the user enters a number and you want to use that number in a mathematical calculation, you must explicitly convert the String that you get from the Text of the TextBox into the appropriate numeric type, e.g. Integer or Double.

That's pretty simple, isn't it? You always know what type of data you have and you SHOULD always know what type of data you want. If they are not the same then you have to convert them rather than relying on the system to do it for you. The reason is that the system can only make a best guess at what you want and that may not always be correct. Take responsibility for your own code and think about the type of all your data. It's really not that hard.

Late-binding occurs when you access a member of an object whose type can't be known until run time. For instance, let's say that you're using a DataGridView with Strings in the cells. Now, the Value property of a DataGridViewCell is type Object, because it has to be able to hold any type of object. That means that you cannot call the Substring method of your String object directly on the Value of the cell because Value is type Object and Object has no Substring method:
Dim substr As String = MyCell.Value.Substring(10)
Yes, the object will be a String at run time and that code would work at that time but the point is that the compiler can't know that at compile time and thus it is considered unsafe. Early-binding occurs when the compiler knows for a fact that a member exists because the type that it's called on has that member. Again, this means being explicit, i.e. you must cast that Object reference that you get from the Value property as type String so that the compiler knows that Substring is a valid member:
Dim substr As String = CStr(MyCell.Value).Substring(10)
gain, you just need to think about what type you've got and what type you want to use and be explicit in converting one to the other if they're different. It's actually quite simple and becomes second nature once you've worked with Option Strict On for a while, thus helping you produce more efficient and more robust code. If you haven't already, you should turn Option Strict On in the IDE Options dialogue so that it will be On by default for all future projects.
 

Latest posts

Back
Top