-
Apr 26th, 2014, 01:50 AM
#1
Thread Starter
Junior Member
Very simple beginner problem.
Hey VBForums. I have only recently started learning VB, only a few weeks experience on very minor projects etc.
I am making a program that tracks basketball scores.
Very simple.
Press button.
number in textbox increases by one.
Thats the idea.
But I have three buttons. One for a single point, a double and a triple.
So my code for one team is.
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles T1Score1.Click
Dim score1 As Integer = CInt(TextBox1.Text)
score1 += 1
TextBox1.Text = score1
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles T1Score3.Click
Dim score1 As Integer = CInt(TextBox1.Text)
score1 += 3
TextBox1.Text = score1
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles T1Score2.Click
Dim score1 As Integer = CInt(TextBox1.Text)
score1 += 2
TextBox1.Text = score1
End Sub
When I run the program and press a button to add the score I get an error message saying
"Conversion from string "" to type 'Integer' is not valid."
I don't really understand what the problem is. Can anybody explain it?
I did this thing earlier when I wrote the code incorrectly like this.
Dim score1 As Integer
score1 = (+2)
TextBox1.Text = score1
but that didn't work.
BUTTTT
When I ran the program and pressed it the buttons above +1 and +3 would work as intended. Thanks in advance if anybody can explain this problem. It's for an assessment task.
-
Apr 26th, 2014, 01:50 AM
#2
Thread Starter
Junior Member
Re: Very simple beginner problem.
-
Apr 26th, 2014, 01:51 AM
#3
Thread Starter
Junior Member
Re: Very simple beginner problem.
Thought this may be relevant.
-
Apr 26th, 2014, 02:48 AM
#4
Re: Very simple beginner problem.
That is VB.NET you posted in the VB6 section! I have requested the thread moved to the correct section.
when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
https://get.cryptobrowser.site/30/4111672
-
Apr 26th, 2014, 03:56 AM
#5
Re: Very simple beginner problem.
Changing CInt to Val will work.
-
Apr 26th, 2014, 04:14 AM
#6
Thread Starter
Junior Member
Re: Very simple beginner problem.
Originally Posted by Nightwalker83
That is VB.NET you posted in the VB6 section! I have requested the thread moved to the correct section.
Oh yeah.. Sorry about that haha. Well you can see that i'm very new to this then. :P
-
Apr 26th, 2014, 04:16 AM
#7
Re: Very simple beginner problem.
right at the very start of your code, put this line
This will disallow all implicit conversions. However, most usefully, when the program stops and throws an error, you can hover the mouse over the highlighted error and the IDE will give you a suggestion of how to fix the problem.
CInt is a hangover from VB6 and it does not work as you would expect. If your string contains characters that cannot be converted, such as spaces etc it will simply throw an error.
You should be using a vb.net equivalent such as Convert.ToInt32, MSDN example here.
It allows you to verify that the value can be converted and deal with errors much more easily than CInt
Alternatively, instead of using a textbox for numeric input, you could use a NumericUpDown which will only allow numeric input.
-
Apr 26th, 2014, 04:17 AM
#8
Thread Starter
Junior Member
Re: Very simple beginner problem.
Thank you so much Such a simple fix thank you so much. I've been searching everywhere.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|