I am VERY new to VB.Net programming and am having a problem with an Overload exception due to the way I built my strings/substrings. Here's the first part of my code causing the problem:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
PhoneNum = TextBox1.Text
Num1 = PhoneNum.Substring(0, 3)
Num2 = PhoneNum.Substring(4, 3)
Num3 = PhoneNum.Substring(8)
If Not IsNumeric(Num1) Then
MessageBox.Show( _
"The phone number you entered " & TextBox1.Text & " is not valid." _
& " ERROR: It must conatin only numbers.", "Error Message", MessageBoxButtons.OK, MessageBoxIcon.Error, _
MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly)
ElseIf Not IsNumeric(Num2) Then
MessageBox.Show( _
"The phone number you entered " & TextBox1.Text & " is not valid." _
& " ERROR: It must conatin only numbers.", "Error Message", MessageBoxButtons.OK, MessageBoxIcon.Error, _
MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly)
ElseIf Not IsNumeric(Num3) Then.....
If I do not enter enough characters to satisfy the substrings, I get the Overlaod exception. PLEASE ADVISE and Thanks in advace.
This is homework and and I'm supposed to be using SubString and Length methods as well as IndexOf to filter the "-". I think I got most of the coding fixed by replacing the .Substring with .IndexOf for the Num1, 2 & 3.
Dear Christos, before of any other thing I want to advise you that is possible I have misunderstood the problem. It should not be the first time that my english fail!
Anyway, if you need that the text of your textbox were composed by a minimum number of characters, because, if it is not so, at least Num3 will result empty, and then surely not numeric, why do you not test the entire number at the beginning before to assign the substring?
To filter "-", you can:
Dim BigNum as string=textbox1.text
BigNum.Replace("-","")
Obviously you can repeat for others characters usable in place of "-", if any.
But you can also use your approach, dividing the text in 3 substring, but only after you have tested if textBox1.text.lenght is sufficient
And then, you could simplify your code with a group test like this:
If (IsNumeric(Num1) = False Or IsNumeric(num2) = False Or IsNumeric(Num3) = False) Then
MessageBox.Show( _
"The phone number you entered " & TextBox1.Text & " is not valid." _
& " ERROR: It must conatin only numbers.", "Error Message", MessageBoxButtons.OK, MessageBoxIcon.Error, _
MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly)
Happy to see that perhaps tour problem are solved, anyway, before to realize that, I built a version of your program that seems to work. At now the job is already done and so I send it to you
Private Sub Prova()
Dim PhoneNum As String = TextBox1.Text
'Before, I need to be sure to have the minimum lenght required, or an error will occur when I will try to extract a substring from nothing!
If PhoneNum.Length < 12 Then
MsgBox("The phone number you entered " & TextBox1.Text & " is not valid." & " ERROR: It must contain 12 characters.", MsgBoxStyle.OKOnly)
Exit Sub
End If
'Assume that Textbox1.text is: 039-574-730128 (is my office number, don't dial it! :-)) )
Dim Num1 As String = PhoneNum.Substring(0, 3) 'Num1="039"
Dim Num2 As String = PhoneNum.Substring(4, 3) 'Num2="574"
Dim Num3 As String = PhoneNum.Substring(8) 'Num3="730128"
If (IsNumeric(Num1) = False Or IsNumeric(Num2) = False Or IsNumeric(Num3) = False) Then
MsgBox("The phone number you entered " & TextBox1.Text & " is not valid." & " ERROR: It must contain only numbers.", MsgBoxStyle.OKOnly, "Error !")
Exit Sub
'I prefer to use "Phonenum" in place of "TextBox1.Text", but it works also with "Textbox1.text"
ElseIf PhoneNum.Chars(3) <> "-" Then
MsgBox("The phone number you entered " & TextBox1.Text & " is not valid." & " ERROR: You must enter a dash after the area code.", MsgBoxStyle.OKOnly, "Error !")
Exit Sub
ElseIf PhoneNum.Chars(7) <> "-" Then
MsgBox("The phone number you entered " & TextBox1.Text & " is not valid." & " ERROR: You must enter a dash after the area code.", MsgBoxStyle.OKOnly, "Error !")
Exit Sub
Else
MsgBox("The phone number you entered " & TextBox1.Text & " is a valid phone number.", MsgBoxStyle.OKOnly, "Phone number validation message")
End If