Results 1 to 3 of 3

Thread: [2005] Code warnings

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    May 2005
    Location
    London United Kingdom
    Posts
    334

    Resolved [2005] Code warnings

    Hi I have this code and function which shows no warnings in vs 2003 but showing warnings in vs 2005. This code shows the following warning(Access of shared member, constant member, enum member or nested type through an instance; qualifying expression will not be evaluated.)
    Code:
       Private Sub btnDelete_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDelete.Click
            Dim iResult As DialogResult = MessageBox.Show("Delete " & cboUsers.Text & "?", _
            "Confirm Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
            If iResult = DialogResult.Yes Then
                drUsers.Delete()
                cboUsers.Focus()
            End If
        End Sub
    This function also show this warning (Variable 'sErrorMessage' is used before it has been assigned a value. A null reference exception could result at runtime.)
    Code:
       Private Function ValidData() As Boolean
            Dim sErrorMessage As String
            If txtFirstName.Text = "" Then
                sErrorMessage = "Please First Name required"
            ElseIf txtSurname.Text = "" Then
                sErrorMessage = "Please Surname required"
            ElseIf txtUserName.Text = "" Then
                sErrorMessage = "Please User Name required"
            ElseIf txtPosition.Text = "" Then
                sErrorMessage = "Please Possition required"
            ElseIf txtEmployeeNo.Text = "" Then
                sErrorMessage = "Please Employee number reqired"
            End If
            If sErrorMessage = "" Then
                ValidData = True
            Else
                ValidData = False
                MessageBox.Show(sErrorMessage, "Data entry error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If
        End Function
    The bold words in the codes are underline in my code. Could any one help out. Thanks
    Last edited by wiadus; Apr 14th, 2007 at 07:06 AM.

  2. #2
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [2005] Code warnings

    On your first warning, put your cursor over the line the warning occurs and you'll see a red circle with a '!' in it, press it and there will be a link that will correct the line for you.

    On your second warning, you get it because when you declare the sErrorMessage variable, you dont give it a value, it will contain Nothing.
    And if the code doesnt enter any of the IF statements, it will still contain nothing when you do this:

    VB Code:
    1. If sErrorMessage = "" Then
    2.             ValidData = True
    3.         Else
    So to make this warning go away, simply assign a value to the sErrorMessage when you declare it:
    VB Code:
    1. Dim sErrorMessage As String = String.Empty
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Code warnings

    The reason that you get the first warning is because this line:
    Code:
    If iResult = DialogResult.Yes Then
    is actually referring to the DialogResult property of the current form. That's the fault of VS.NET 2003. If you do what Atheist says the first suggestion will be to change that to:
    Code:
    If iResult = Windows.Forms.DialogResult.Yes Then
    so you are qualifying the name to specify the System.Windows.Forms.DialogResult type rather than the Me.DialogResult property.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width