none
4 code audit queries RRS feed

  • Question

  • Hello, just have few very quick questions, please help :)

    -----------------------------------------------------------------

    1. Which one is better and recommended to use?

    Dim MySel As String = Convert.ToString (ComboBox.SelectedIndex + 1)
    or
    Dim MySel As String = (ComboBox.SelectedIndex + 1).ToString

    -----------------------------------------------------------------

    How to correct the following codes?

    2.

    Me.Tag = Me.Tag + "\"
    Option Strict On prohibits operands of type Object for operator '+'

    ***

    3.

    Dim MyCounter As OleDbDataReader = CountUser.ExecuteReader
    MyCounter.Read()
    If MyCounter(0) = 0 Then ...
    Option Strict On disallows operands of type Object for operator '='. Use the 'Is' operator to test for object identity.

    ***

    4.

    Private Delegate Function QuestionBoxHandler(ByVal Message As String)
    Private Function ShowQuestionBox(ByVal Message As String)
    Option Strict On requires all Function, Property, and Operator declarations to have an 'As' clause.

    Sunday, February 17, 2019 2:31 PM

Answers

  • Yes, you're right, that was my fault, anyway, for this one, I am sure of the purpose:

    If Me.Invoke(New QuestionBoxHandler(AddressOf ShowQuestionBox), "Would you like blah?") = vbYes Then ...

    Here also = is forbidden, I replace = with Is but still:

    'Is' operator does not accept operands of type 'Microsoft.VisualBasic.MsgBoxResult'. Operands must be reference or nullable types.

    Please just tell me what to do for this one :)

    Perhaps if the goal is to present a yes/no dialog this might be a choice.

    Add this code block to a code module in your project.

    Namespace My
        <ComponentModel.EditorBrowsable(ComponentModel.EditorBrowsableState.Never)>
        Partial Friend Class MyDialogs
            ''' <summary>
            ''' Ask question with NO as the default button
            ''' </summary>
            ''' <param name="pText"></param>
            ''' <returns></returns>
            ''' <remarks></remarks>
            Public Function Question(pText As String) As Boolean
                Return (MessageBox.Show(pText,
                    Application.Info.Title,
                    MessageBoxButtons.YesNo,
                    MessageBoxIcon.Question,
                    MessageBoxDefaultButton.Button2) = MsgBoxResult.Yes)
            End Function
            Public Function Question(pText As String, pTitle As String) As Boolean
                Return (MessageBox.Show(pText, pTitle,
                    MessageBoxButtons.YesNo,
                    MessageBoxIcon.Question,
                    MessageBoxDefaultButton.Button2) = MsgBoxResult.Yes)
            End Function
            Public Function Question(pText As String, pTitle As String, pDefaultButton As MsgBoxResult) As Boolean
                Dim db As MessageBoxDefaultButton
                If pDefaultButton = MsgBoxResult.No Then
                    db = MessageBoxDefaultButton.Button2
                End If
                Return (MessageBox.Show(pText, pTitle,
                    MessageBoxButtons.YesNo,
                    MessageBoxIcon.Question, db) = MsgBoxResult.Yes)
            End Function
        End Class
    
        <HideModuleName()>
        Friend Module SpecialDialogs
            Private instance As New ThreadSafeObjectProvider(Of MyDialogs)
            ReadOnly Property Dialogs() As MyDialogs
                Get
                    Return instance.GetInstance()
                End Get
            End Property
        End Module
    End Namespace
    

    Usage

    Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            If My.Dialogs.Question("Do you like coffee") Then
                MessageBox.Show("Let's get some")
            Else
                MessageBox.Show("Must be a tea person")
            End If
        End Sub
    End Class


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by OSVBNET Monday, February 18, 2019 3:59 PM
    Sunday, February 17, 2019 4:18 PM
    Moderator

All replies

  • Number 1

    You should assert the value is not out of range.

    Dim value As String = ""
    Dim index = ComboBox1.SelectedIndex + 1
    If index <= ComboBox1.Items.Count - 1 Then
        value = ComboBox1.Items(index).ToString()
    End If

    Number 2, no need for Me.

    Tag = Tag.ToString() & "\"

    Number 3 v(see also my GitHub repository on working with MS-Access with VB.NET)

    Public Sub ForumQuestion(pCountry As String)
        Using cn As New OleDbConnection(ConnectionString)
            Using cmd As New OleDbCommand With {.Connection = cn}
                cmd.CommandText = "SELECT FirstName, LastName FROM Customers WHERE Country = @Country"
                cmd.Parameters.AddWithValue("@Country", pCountry)
                cn.Open()
                Dim reader As OleDbDataReader = cmd.ExecuteReader()
                If reader.HasRows Then
                    reader.Read()
                    Dim firstName As String = reader.GetString(0)
                End If
            End Using
        End Using
    End Sub

    Last one is something like this

    Private Delegate Function QuestionBoxHandler(ByVal Message As String) As Boolean
    
    Private Function ShowQuestionBox(ByVal Message As String) As Boolean
        Throw New NotImplementedException
    End Function


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites



    Sunday, February 17, 2019 3:01 PM
    Moderator
  • Thanks, for number 3, database operation is OK, I am just comparing:

    If MyCounter(0) = 0 Then ...

    It says that don't use = and use Is instead, so I am not sure if something like this is a good idea:

    If MyCounter(0) Is 0 Then ...

    Because it stills gives error:

    'Is' operator does not accept operands of type 'Integer'. Operands must be reference or nullable types.

    Sunday, February 17, 2019 3:43 PM
  • You should not be using MyCount(0) = 0 if the intent is to see if there are any results, instead use HasRows and if true perform a Read as per my reply.

    A Reader done right uses the logic I just recommended. I know you have done work with Option Strict Off which I have never done, always have Option Strict On so not sure where MyCount(0) = 0 is coming from.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Sunday, February 17, 2019 3:47 PM
    Moderator
  • Yes, you're right, that was my fault, anyway, for this one, I am sure of the purpose:

    If Me.Invoke(New QuestionBoxHandler(AddressOf ShowQuestionBox), "Would you like blah?") = vbYes Then ...

    Here also = is forbidden, I replace = with Is but still:

    'Is' operator does not accept operands of type 'Microsoft.VisualBasic.MsgBoxResult'. Operands must be reference or nullable types.

    Please just tell me what to do for this one :)

    Sunday, February 17, 2019 3:59 PM
  • Yes, you're right, that was my fault, anyway, for this one, I am sure of the purpose:

    If Me.Invoke(New QuestionBoxHandler(AddressOf ShowQuestionBox), "Would you like blah?") = vbYes Then ...

    Here also = is forbidden, I replace = with Is but still:

    'Is' operator does not accept operands of type 'Microsoft.VisualBasic.MsgBoxResult'. Operands must be reference or nullable types.

    Please just tell me what to do for this one :)

    Perhaps if the goal is to present a yes/no dialog this might be a choice.

    Add this code block to a code module in your project.

    Namespace My
        <ComponentModel.EditorBrowsable(ComponentModel.EditorBrowsableState.Never)>
        Partial Friend Class MyDialogs
            ''' <summary>
            ''' Ask question with NO as the default button
            ''' </summary>
            ''' <param name="pText"></param>
            ''' <returns></returns>
            ''' <remarks></remarks>
            Public Function Question(pText As String) As Boolean
                Return (MessageBox.Show(pText,
                    Application.Info.Title,
                    MessageBoxButtons.YesNo,
                    MessageBoxIcon.Question,
                    MessageBoxDefaultButton.Button2) = MsgBoxResult.Yes)
            End Function
            Public Function Question(pText As String, pTitle As String) As Boolean
                Return (MessageBox.Show(pText, pTitle,
                    MessageBoxButtons.YesNo,
                    MessageBoxIcon.Question,
                    MessageBoxDefaultButton.Button2) = MsgBoxResult.Yes)
            End Function
            Public Function Question(pText As String, pTitle As String, pDefaultButton As MsgBoxResult) As Boolean
                Dim db As MessageBoxDefaultButton
                If pDefaultButton = MsgBoxResult.No Then
                    db = MessageBoxDefaultButton.Button2
                End If
                Return (MessageBox.Show(pText, pTitle,
                    MessageBoxButtons.YesNo,
                    MessageBoxIcon.Question, db) = MsgBoxResult.Yes)
            End Function
        End Class
    
        <HideModuleName()>
        Friend Module SpecialDialogs
            Private instance As New ThreadSafeObjectProvider(Of MyDialogs)
            ReadOnly Property Dialogs() As MyDialogs
                Get
                    Return instance.GetInstance()
                End Get
            End Property
        End Module
    End Namespace
    

    Usage

    Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            If My.Dialogs.Question("Do you like coffee") Then
                MessageBox.Show("Let's get some")
            Else
                MessageBox.Show("Must be a tea person")
            End If
        End Sub
    End Class


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by OSVBNET Monday, February 18, 2019 3:59 PM
    Sunday, February 17, 2019 4:18 PM
    Moderator
  • Thank you :)
    Monday, February 18, 2019 3:59 PM
  • 4 should be:

    Private Delegate Function QuestionBoxHandler(ByVal Message As String) As DialogResult
    Private Function ShowQuestionBox(ByVal Message As String) As DialogResult
    ...
    Tuesday, February 19, 2019 12:33 PM