0
\$\begingroup\$

I have the following case statements that has to be modified to fit these cases: Dec, Double, Int, Long, Short, Date, and String.

Please let me know if there is an easier way to handle this rather than repeating the following (slightly modified for each case):

Case GetType(Integer)
    If String.IsNullOrEmpty(c.Text) Then
        list.Add(CInt(0))
    Else
        Try
            list.Add(CInt(c.Text))
        Catch ex As Exception
            WriteAudit(ex, True)
            Dim intValue As Integer
            If Not Integer.TryParse(c.Text, intValue) Then
                c.Text = c.Text.Substring(0, c.Text.Length - 1)
                MessageBox.Show("Please Enter Only Integer Values Here")
            End If
        End Try
    End If
\$\endgroup\$
2
\$\begingroup\$

For value types (i.e. all your types except String) you can use the Convert.ChangeType method:

list.Add(Convert.ChangeType(c.Text, theType))
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.