views:

19

answers:

1

I am attempting to keep user inputs into our application as clean as possible. One way I am attempting to do this is not allowing incorrect data type in fields (not allowing alpha characters when expecting numeric values)

I found and implemented the following code:

Private Sub txtSocial_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtSocial.KeyPress
    Dim UserKeyPress As Char = e.KeyChar
    Dim isKey As Boolean = e.KeyChar.IsDigit(UserKeyPress)
    Dim isCont As Boolean = e.KeyChar.IsControl(UserKeyPress)
    If Not isKey And Not isCont Then
        e.Handled = True
    End If

The code works as desired however Visual Studio throws this warning:

Access of shared member, constant member, enum member or nested type through an instance; qualifying expression will not be evaluated.

I am not opposed to warnings, but I would like to know if there is whether I ignore the warning and move on, or is there a better way to accomplish my goals?

A: 

Instead of e.KeyChar.IsDigit, use Char.IsDigit. Same thing for IsControl. When accessing a static (shared) method like IsDigit, you can access it directly through the class instead of a specific instance: the instance (e.KeyChar in this case) would be ignored anyway, and this is what the warning is about.

interjay
Awesome no more warnings and I still have the same functionality.
Sean