 |
| Visual Basic 2008 Essentials If you are new to Visual Basic programming with version 2008, this is the place to start your questions. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the Visual Basic 2008 Essentials section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

April 3rd, 2009, 11:29 AM
|
|
Authorized User
|
|
Join Date: Mar 2009
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
problem with controlling program flow vb tutorial
Hi everyone, I am really confused, I am trying to teach myself the basics of visual basic and i am following tutorials and am stuck on lesson 9, link here http://www.vbtutor.net/vb2008/vb2008_lesson9.html
In the example 9.4 it is a programme designed to show pupils grades based on their mark, but when i copy the code into the button i get the error message 'mynumber is not declared'
this is the code
Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Mark As Integer
Dim Grade as String
Mark = TextBox1.Text
If myNumber >=80 Then
Grade="A"
ElseIf Mark>=60 and Mark<80 then
Grade="B"
ElseIf Mark>=40 and Mark<60 then
Grade="C"
Else
Grade="D"
End If
End Sub
I am really confused, because I tried to change mynumber to mark, because that seemed to make sense but when I do that i get the error message Conversion from string "" to type 'Integer' is not valid. and Mark = TextBox1.Text is highlighted.
Is there something silly I am doing wrong?
The tutorial doesnât say what labels and textboxes you need to make it work, so can someone explain what I am doing wrong?
Thankyou in advance
|
|

April 3rd, 2009, 11:41 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
|
|
Hello.
MyNumber should be Mark.
The other error is because you maybe has option extrict turned ON. (which is good :) )
to avoid the error, you need to cast the string contained in textbox1.text to an integer. You can do it using Cint, int.parse or any other function that convert a string into a number.
__________________
HTH
Gonzalo
================================================== =========
Read this if you want to know how to get a correct reply for your question.
(Took that from Doug signature and he Took that from Peter profile)
================================================== =========
My programs achieved a new certification :
WORKS ON MY MACHINE
================================================== =========
I know that CVS was evil, and now i got the proof.
================================================== =========
|
|

April 3rd, 2009, 02:13 PM
|
|
Authorized User
|
|
Join Date: Mar 2009
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Quote:
Originally Posted by gbianchi
Hello.
MyNumber should be Mark.
The other error is because you maybe has option extrict turned ON. (which is good :) )
to avoid the error, you need to cast the string contained in textbox1.text to an integer. You can do it using Cint, int.parse or any other function that convert a string into a number.
|
Thank you very much for this, but how do i do this? I am very new to visual basic and I dont even know what the objects you mentioned are, let alone how to use them!
Th==Cheers in advance, your help is much appreciated!
|
|

April 3rd, 2009, 02:28 PM
|
|
Wrox Author
|
|
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
|
|
What Gonzalo means is do something like this:
Mark = CInt(TextBox1.Text)
Mark = Convert.ToInt32(TextBox1.Text)
Mark = (Integer)TextBox1.Text
They all pretty much do the same thing.
hth.
-Doug
__________________
===============================================
Doug Parsons
Wrox online library: Wrox Books 24 x 7
Did someone here help you? Click  on their post!
"Easy is the path to wisdom for those not blinded by themselves."
===============================================
|
|
The Following User Says Thank You to dparsons For This Useful Post:
|
|
|

April 4th, 2009, 07:59 AM
|
|
Authorized User
|
|
Join Date: Mar 2009
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Quote:
Originally Posted by dparsons
What Gonzalo means is do something like this:
Mark = CInt(TextBox1.Text)
Mark = Convert.ToInt32(TextBox1.Text)
Mark = (Integer)TextBox1.Text
They all pretty much do the same thing.
hth.
-Doug
|
right, i tried replacing mark = textbox1.text with each of the statements above but I get this in the immediate window -
A first chance exception of type 'System.InvalidCastException' occurred in Microsoft.VisualBasic.dll
am i just doing something silly. Should the grade just appear because it is a string or would it appear in a label or textbox because at the moment when i debug it, it runs but nothing happens when i put a mark in the textbox and click the button, i just get that message I mentioned
when i use Mark = Convert.ToInt32(TextBox1.Text i get the error messageA first chance exception of type 'System.FormatException' occurred in mscorlib.dll
and if i use Mark = '(Integer)TextBox1.Text' i get the error saying expression expected.
I really dont know why I am going wrong and why the tutorial is published with errors in it.
In my programme I have only got the button with the code and the text box for the mark, should I have somehting else? I dont suppose someone can look at the tutorial and see what is wrong with it please/
Thanks for all this help by the way, i am just such a novice!
|
|

April 4th, 2009, 09:15 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
|
|
Are you inserting a number into the textbox???
The tutorial is not bad. It's just lack the error control part, maybe because it will be teach to you later.
__________________
HTH
Gonzalo
================================================== =========
Read this if you want to know how to get a correct reply for your question.
(Took that from Doug signature and he Took that from Peter profile)
================================================== =========
My programs achieved a new certification :
WORKS ON MY MACHINE
================================================== =========
I know that CVS was evil, and now i got the proof.
================================================== =========
|
|

April 6th, 2009, 10:28 AM
|
|
Authorized User
|
|
Join Date: Mar 2009
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Yes I have been inserting a number into the textbox.
Why, does it work when you try it? Am i just being stupid?
Cheers!
|
|

April 6th, 2009, 10:37 AM
|
|
Wrox Author
|
|
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
|
|
Hmm. If you are entering a number into TextBox1 and it is throwing an exception there is something else at play.
You can try this:
visualbasic Code:
Dim outInteger as Integer If Integer.tryParse(TextBox1.Text, outInteger) Then 'Process data using hte outNumber variable Else 'Textbox1 does not contain a number End If
You could, of course, use the IsNumeric() function to test if a string is actually a number but I prefer tryParse since there isn't a built in IsNumeric function in C# ;]
Anyway, Integer.tryParse takes two parameters: a string which you want to try and parse into an integer, and an output parameter (in this case outInteger). The tryParse function returns a Boolean; if the first parameter (the string) is able to be parsed into an Integer it returns True otherwise it returns False. Finally, if tryParse succeeds the second parameter (the output parameter which is outInteger) will then contain the integer value of whatever was contained within in the first parameter.
You would then use this output parameter to do any processing related to the integer value.
hth.
-Doug
__________________
===============================================
Doug Parsons
Wrox online library: Wrox Books 24 x 7
Did someone here help you? Click  on their post!
"Easy is the path to wisdom for those not blinded by themselves."
===============================================
|
|

April 6th, 2009, 01:27 PM
|
|
Authorized User
|
|
Join Date: Mar 2009
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
None of the errors appear any more in the immediate window but nothing happens when i run the programme. this is really confusing me, maybe im not cut out for visual basic! thanks for being so patient with me and trying to help though!
I have a label with mark in, the textbos, and the button and this is the code I am using
Code:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Mark As Integer
Dim Grade As String
Dim outInteger As Integer
If Integer.TryParse(TextBox1.Text, outInteger) Then
'Process data using the outNumber variable
Else
'Textbox1 does not contain a number
End If
Mark = TextBox1.Text
If Mark >= 80 Then
Grade = "A"
ElseIf Mark >= 60 And Mark < 80 Then
Grade = "B"
ElseIf Mark >= 40 And Mark < 60 Then
Grade = "C"
Else
Grade = "D"
End If
End Sub
End Class
|
|

April 6th, 2009, 01:51 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
|
|
mmm.. you are at lesson 9. I didn't read the tutorial, but at least you learned how to use if..then..else..endif and you already has to know that ' means that the line is a comment, so will not execute.
Maybe the tutorial is to hard for you??? Do you at least has a background in programming or this is even your first time programming??
The code that Doug gave you lacks something to do if the number is parse correctly or not, he just gave you something to start to work it out the problem.
Do you have any background programming??? saying "I don't know VB" is not the same that saying "I don't know even how to do a hello word in any language".
__________________
HTH
Gonzalo
================================================== =========
Read this if you want to know how to get a correct reply for your question.
(Took that from Doug signature and he Took that from Peter profile)
================================================== =========
My programs achieved a new certification :
WORKS ON MY MACHINE
================================================== =========
I know that CVS was evil, and now i got the proof.
================================================== =========
|
|
 |