|
-
Apr 14th, 2007, 05:31 AM
#1
Thread Starter
Hyperactive Member
[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.
-
Apr 14th, 2007, 05:59 AM
#2
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:
If sErrorMessage = "" Then
ValidData = True
Else
So to make this warning go away, simply assign a value to the sErrorMessage when you declare it:
VB Code:
Dim sErrorMessage As String = String.Empty
-
Apr 14th, 2007, 06:23 AM
#3
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|