Welcome to Dream.In.Code
Become a VB.NET Expert!

Join 150,082 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 1,795 people online right now. Registration is fast and FREE... Join Now!




InputBox Validation Problem and weird error

 
Reply to this topicStart new topic

InputBox Validation Problem and weird error, attempting to validate entered values in an InputBox, and keep getting

vypr07
18 Apr, 2008 - 10:44 PM
Post #1

New D.I.C Head
*

Joined: 15 Apr, 2008
Posts: 2


My Contributions
Hey all, I'm new to this site, and after looking around a couple days, haven't found the answer I was looking for, so maybe y'all can answer my question.

I'm working on a lab (array manipulation exercise) that seeks to receive 10 values through an InputBox, display them, (also providing options for getting the average, and sorting the values). My problem is that I am not exactly sure how to validate that the user enters correct data, and the main thing is that it keeps coming up with an error whenever the cancel button is clicked.

Here is my code, if y'all can help at all, I would greatly appreciate it... the lab's due on Sunday, but I've never not finished a program, so even if it's past Sunday, I would still like to know what I did wrong biggrin.gif

CODE
Dim intCounter As Integer
        Dim msgInput As DialogResult
        Dim strArray As String

        Try
            For intCounter = 0 To 9

                strInputVal = InputBox("Please enter a value:" & vbNewLine & "Remaining values to be entered: " & 10 - intCounter, "Input")
                If IsNumeric(strInputVal) Then
                    intValidVal = CInt(strInputVal)
                    intDigitArray(intCounter) = intValidVal
                Else
                    MsgBox("Error! Non-numeric value!")
                End If

                If strInputVal = Nothing Then
                    MsgBox("Please enter a value!")
                End If

            Next

        Catch ex As Exception
            MsgBox("AAAAAGGGHHH!!! there seems to be an error. yes. unfortunately. What the error is, I have no bloody clue. Thank you for using another fine vyprTECH product! :)")
        End Try




Geekin out in North Pole, Alaska, cool.gif

- vypr ph34r.gif
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: InputBox Validation Problem And Weird Error
19 Apr, 2008 - 05:02 AM
Post #2

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 9,483



Thanked: 161 times
Dream Kudos: 9075
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
Just some advice, you're using a ton of legacy libraries (VB6 and older), you're going to want to stick to the .Net libraries as the legacy ones wont be there forever. IsNumeric what is was supposed to do in VB6 and older, but it is rather buggy in the .Net languages, so in .Net you're going to want to use the TryParse Method instead.

Also, instead of using the legacy Cint, use either CType or DirectCast, as both are intrinsic .Net libraries.

Ive made some modifications to your code which is posted below. Since you never said what error you're receiving I did my best to cover any errors that could occur in your process. I included comments so you can read along to follow what I'm doing:


vb

Dim intCounter As Integer
Dim msgInput As DialogResult
Dim strArray As String
'Variable to hold the TryParse result
Dim converted As Boolean = False
'Variable to hold the converted value is successful
Dim userNum As Integer
Try
For intCounter = 0 To 9
'Prompt the user for a number
strInputVal = InputBox("Please enter a value:" & vbNewLine & "Remaining values to be entered: " & 10 - intCounter, "Input")

'Make sure the user entered a value
If Not String.IsNullOrEmpty(strInputVal) Then
'Use TryParse to convert user input to an integer
converted = Integer.TryParse(strInputVal,userNum)

'Check to status of the conversion
If Not Converted Then
'They entered a non-numeric vaulue, so
'we must chastise them for their mistake
MessageBox.Show("You must enter numeric data!")
Else
'Conversion was a success so add this
'to our array. In the code you've provided
'I dont see where you're initializing your
'array, but Im going to assume you're doing
'it correctly, or I'll have to chastise
'you for your sinful ways
intDigitArray(intCounter) = userNum
End If
Else
'User didnt enter a value, let them know
'the error of their ways
MessageBox.Show("Please enter a value!")
End If
Next

Catch ex As Exception
MessageBox.Show("ERROR: " + ex.Message)
End Try

User is offlineProfile CardPM
+Quote Post

vypr07
RE: InputBox Validation Problem And Weird Error
20 Apr, 2008 - 03:21 PM
Post #3

New D.I.C Head
*

Joined: 15 Apr, 2008
Posts: 2


My Contributions
QUOTE(PsychoCoder @ 19 Apr, 2008 - 06:02 AM) *

Just some advice, you're using a ton of legacy libraries (VB6 and older), you're going to want to stick to the .Net libraries as the legacy ones wont be there forever. IsNumeric what is was supposed to do in VB6 and older, but it is rather buggy in the .Net languages, so in .Net you're going to want to use the TryParse Method instead.

Also, instead of using the legacy Cint, use either CType or DirectCast, as both are intrinsic .Net libraries.

Ive made some modifications to your code which is posted below. Since you never said what error you're receiving I did my best to cover any errors that could occur in your process. I included comments so you can read along to follow what I'm doing:


vb

Dim intCounter As Integer
Dim msgInput As DialogResult
Dim strArray As String
'Variable to hold the TryParse result
Dim converted As Boolean = False
'Variable to hold the converted value is successful
Dim userNum As Integer
Try
For intCounter = 0 To 9
'Prompt the user for a number
strInputVal = InputBox("Please enter a value:" & vbNewLine & "Remaining values to be entered: " & 10 - intCounter, "Input")

'Make sure the user entered a value
If Not String.IsNullOrEmpty(strInputVal) Then
'Use TryParse to convert user input to an integer
converted = Integer.TryParse(strInputVal,userNum)

'Check to status of the conversion
If Not Converted Then
'They entered a non-numeric vaulue, so
'we must chastise them for their mistake
MessageBox.Show("You must enter numeric data!")
Else
'Conversion was a success so add this
'to our array. In the code you've provided
'I dont see where you're initializing your
'array, but Im going to assume you're doing
'it correctly, or I'll have to chastise
'you for your sinful ways
intDigitArray(intCounter) = userNum
End If
Else
'User didnt enter a value, let them know
'the error of their ways
MessageBox.Show("Please enter a value!")
End If
Next

Catch ex As Exception
MessageBox.Show("ERROR: " + ex.Message)
End Try




Thanks for the help, I did have a question though.. I'm currently taking this class through DeVry Online, so are they using like an old curriculum or something? because this is the way they've been teaching the class.. apparently I'm using 'legacy' libraries which, given your comments, are old hat, and about to be discarded... what would be a good resource for learning the .Net libraries? Granted, I'm going into game design, so I'll probably be working more with C++ than VB, but any tips would be appreciated.

Thanks,

vypr
User is offlineProfile CardPM
+Quote Post

ferrari12508
RE: InputBox Validation Problem And Weird Error
20 Apr, 2008 - 06:36 PM
Post #4

D.I.C Lover
Group Icon

Joined: 2 Nov, 2007
Posts: 1,112



Thanked: 2 times
Dream Kudos: 150
My Contributions
I am no where near psycho in experience or any other aspect, but really any up to date book you buy should use the new VB libraries.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 11:36PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month