Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB 2008 > Visual Basic 2008 Essentials
|
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
 
Old April 3rd, 2009, 11:29 AM
Authorized User
 
Join Date: Mar 2009
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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

 
Old April 3rd, 2009, 11:41 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

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.
================================================== =========
 
Old April 3rd, 2009, 02:13 PM
Authorized User
 
Join Date: Mar 2009
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by gbianchi View Post
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!
 
Old April 3rd, 2009, 02:28 PM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

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:
gbianchi (April 3rd, 2009)
 
Old April 4th, 2009, 07:59 AM
Authorized User
 
Join Date: Mar 2009
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by dparsons View Post
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!
 
Old April 4th, 2009, 09:15 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

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.
================================================== =========
 
Old April 6th, 2009, 10:28 AM
Authorized User
 
Join Date: Mar 2009
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Yes I have been inserting a number into the textbox.

Why, does it work when you try it? Am i just being stupid?

Cheers!
 
Old April 6th, 2009, 10:37 AM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

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."
===============================================
 
Old April 6th, 2009, 01:27 PM
Authorized User
 
Join Date: Mar 2009
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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
 
Old April 6th, 2009, 01:51 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

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.
================================================== =========





Similar Threads
Thread Thread Starter Forum Replies Last Post
Flow chart of Cobol program in dotNet SOANS General .NET 0 November 20th, 2007 11:29 PM
Program Flow Interrupted By IF...THEN Statement squeege321 Pro VB 6 10 March 23rd, 2007 11:38 AM
Flow of the Program ? pandian Java Basics 2 March 20th, 2006 11:07 PM
weird program flow with nested loops zayasv Intro Programming 2 November 17th, 2005 06:19 AM
weird program flow with nested functions zayasv VB.NET 0 October 26th, 2005 11:17 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.