Tech Support Guy banner
  • Please post in our Community Feedback thread for help with the new forum software! If you are having trouble logging in, please Contact Us for assistance.
Status
Not open for further replies.
1 - 7 of 7 Posts

·
Registered
Joined
·
305 Posts
Discussion Starter · #1 ·
I'm trying to insert dashes into the correct places in a phone number, but vb.net ignores my first statement and goes to do the next one.

Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
' declare variable
Dim phoneNumber As Integer = 1234567890

phoneNumber = phoneTextBox.Text
phoneLabel.Text = phoneNumber.ToString.Insert(3, "-")
phoneLabel.Text = phoneNumber.ToString.Insert(6, "-")


Result: 123456-7890
 

·
Registered
Joined
·
3,257 Posts
I don't do VB, but phoneNumber never changes - you write the string twice from scratch

you could try something more like

phoneLabel.Text = phoneNumber.ToString.Insert(3, "-")
phoneLabel.Text = phoneLabel.Text.Insert(6, "-")
 

·
Registered
Joined
·
3,086 Posts
There's no way to put a dash into an integer data type variable. So the thread title's a bit misleading. The first line is getting skipped because there's a type mismatch: you're trying to put a string (implied by the Text property) into a variable declared as an integer. In fact, I don't know why the compiler isn't throwing an exception there.

If it were me, I wouldn't ever consider the phone number an integer. I mean, it isn't a value you're using mathematically; it's a string composed of digits. Just dimension phoneNumber as a String.

chris.
 

·
Registered
Joined
·
305 Posts
Discussion Starter · #4 ·
There's no way to put a dash into an integer data type variable. So the thread title's a bit misleading. The first line is getting skipped because there's a type mismatch: you're trying to put a string (implied by the Text property) into a variable declared as an integer. In fact, I don't know why the compiler isn't throwing an exception there.

If it were me, I wouldn't ever consider the phone number an integer. I mean, it isn't a value you're using mathematically; it's a string composed of digits. Just dimension phoneNumber as a String.

chris.
Well, I tried using Dim phoneNumber As String = 1234567890 and I took away the .ToString from the the two insert methods, but it's still giving me the same result.
 

·
Registered
Joined
·
3,086 Posts
You have to put quotes around the value for one, and you can't dimension and assign a variable on the same line without using a special assignment operator (I think; I always do it on two lines).

Try this:

Code:
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
  Dim phoneNumber As String

  ' No reason to assign it the value 1234567890, just assign it the working value
  phoneNumber = phoneTextBox.Text

  phoneNumber = phoneNumber.Insert(3, "-")
  phoneNumber = phoneNumber.Insert(7, "-")

  phoneLabel.Text = phoneNumber

  [i]...other operations...[/i]
End Sub
Notice the second insert is at character 7 not 6 because the length of the string grew by one after the first insert. But Insert isn't a built-in String function in Visual Basic (that I know of). Is that a user-defined function? If not, what VB build are you using? Perhaps you're thinking of InsertString?

chris.
 

·
Registered
Joined
·
3,257 Posts
I guess I wasn't thinking clearly - thanks for the 6-->7 thing
phoneNumber = phoneTextBox.Text as an assignment is also something I didn't notice. :(
It still remains that he was writing the string twice from the same integer source (ie. overwriting the first insert of a dash) -- but I guess it's also obvious that I don't do VB :)

Although I don't do VB, I think something along the lines of
Dim myString As String = myInteger.ToString("(###) ### - ####")
should be possible? (here I tried to use brackets for the area code rather than a dash separation)
 

·
Registered
Joined
·
305 Posts
Discussion Starter · #7 ·
You have to put quotes around the value for one, and you can't dimension and assign a variable on the same line without using a special assignment operator (I think; I always do it on two lines).

Try this:

Code:
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
  Dim phoneNumber As String

  ' No reason to assign it the value 1234567890, just assign it the working value
  phoneNumber = phoneTextBox.Text

  phoneNumber = phoneNumber.Insert(3, "-")
  phoneNumber = phoneNumber.Insert(7, "-")

  phoneLabel.Text = phoneNumber

  [i]...other operations...[/i]
End Sub
Notice the second insert is at character 7 not 6 because the length of the string grew by one after the first insert. But Insert isn't a built-in String function in Visual Basic (that I know of). Is that a user-defined function? If not, what VB build are you using? Perhaps you're thinking of InsertString?

chris.
Worked like a charm. Thanks.
 
1 - 7 of 7 Posts
Status
Not open for further replies.
Top