Foo - I need VB.net help #2

Bikeforums.net is a forum about nothing but bikes. Our community can help you find information about hard-to-find and localized information like bicycle tours, specialties like where in your area to have your recumbent bike serviced, or what are the best bicycle tires and seats for the activities you use your bike for.




View Full Version : I need VB.net help #2


EJ123
02-11-08, 08:15 PM
For VB 05 Express.

I need to make a calcuator .exe that will figure out addition, subtraction, multiplication, and division. It will have two text boxes a few labels and 4 buttons for the math functions.

I have notes, don't really help much, and I need to use the "If..Else" statements or the Case Select stuff. Yikes! I can't even figure out how to set my variables.

DO I start out with "Dim txtBox1 as Integer"? Took a guess, heh. Ug.

Also how do I hide labels? Or make them appear? lblmessage.show("blah")?


jaxgtr
02-11-08, 08:59 PM
you might need to get another book to help you out. I don't mess with the vb.net stuff much, last time was around the 2003 version, but the if..Else and Case Select are pretty simple stuff in the big scheme of programming.

http://www.dreamincode.net/forums/showtopic13645.htm

http://www.dreamincode.net/forums/showforum67.htm

Malistryx
02-12-08, 05:48 AM
There's actually a few tutorials online that show you how to make a calculator in VB.Net.

Try doing a google search for pretty much any VB.Net question and you'll get tons of results since it's used fairly often as a beginners programming language.


KingTermite
02-12-08, 09:05 AM
For VB 05 Express.

I need to make a calcuator .exe that will figure out addition, subtraction, multiplication, and division. It will have two text boxes a few labels and 4 buttons for the math functions.

I have notes, don't really help much, and I need to use the "If..Else" statements or the Case Select stuff. Yikes! I can't even figure out how to set my variables.

DO I start out with "Dim txtBox1 as Integer"? Took a guess, heh. Ug.

Also how do I hide labels? Or make them appear? lblmessage.show("blah")?

1) NO. Never make a variable with a name that looks like one data type and is actually another one. "Dim txtBox1 as Integer" is nothing but a confusing mess that will get you in to trouble.

2) Didn't you hide labels in the last assignment? Hide them until the user clicked a button?

3) It sounds like you are not doing a standard calculator, but a very simple one to force some concepts. Two text boxes (one for each number) and buttons for +,-,/,* correct?

My suggestion would be to have two texboxes. txtNum1 and txtNum2 or something like that. An integer variable that gets set when you click any of the math buttons. If you have a variable that sets something based on which operation was hit, then your set. (E.g. +=1, -=2, *=3, /=4).

Then when you hit the calculate button:
Get txtNum1 and convert to an actual number (remember its a string right now)
Get txtNum2 and convert to an actual number
Check the operation variable (If..Else or Case to select and perform actual calculation)
Display results

EJ123
02-12-08, 10:53 AM
1) NO. Never make a variable with a name that looks like one data type and is actually another one. "Dim txtBox1 as Integer" is nothing but a confusing mess that will get you in to trouble.

2) Didn't you hide labels in the last assignment? Hide them until the user clicked a button?

3) It sounds like you are not doing a standard calculator, but a very simple one to force some concepts. Two text boxes (one for each number) and buttons for +,-,/,* correct?

My suggestion would be to have two texboxes. txtNum1 and txtNum2 or something like that. An integer variable that gets set when you click any of the math buttons. If you have a variable that sets something based on which operation was hit, then your set. (E.g. +=1, -=2, *=3, /=4).

Then when you hit the calculate button:
Get txtNum1 and convert to an actual number (remember its a string right now)
Get txtNum2 and convert to an actual number
Check the operation variable (If..Else or Case to select and perform actual calculation)
Display results

Oh ok. I figured out how to do the hide labels. I just don't understand which variables to set, and to set them as what, and how to execute the math functions. Why can't it be something as easy as "me.lblAnswer.show(txtBox1 + txtBox2) when the user clicks the addition button? Arg. Will my variables strictly be string or will they be integer? I thought I needed to label the txtBox as a variable.

KingTermite
02-12-08, 11:10 AM
Oh ok. I figured out how to do the hide labels. I just don't understand which variables to set, and to set them as what, and how to execute the math functions. Why can't it be something as easy as "me.lblAnswer.show(txtBox1 + txtBox2) when the user clicks the addition button? Arg. Will my variables strictly be string or will they be integer? I thought I needed to label the txtBox as a variable.

The text boxes are variables, but they are TEXT boxes, not numbers. So you need to figure out out to convert the text to a number. Once you do that, its almost as easy as you said with the show. You need to figure out which operation it is, but then its pretty much what you have.

EJ123
02-12-08, 04:44 PM
The text boxes are variables, but they are TEXT boxes, not numbers. So you need to figure out out to convert the text to a number. Once you do that, its almost as easy as you said with the show. You need to figure out which operation it is, but then its pretty much what you have.

Oh. Do I need to input the convert to integer ( cInt?) somewhere within the code?

KingTermite
02-12-08, 04:56 PM
They will be separate.

Psuedocode:

dim TxtboxNum1 as textbox
dim TxtboxNum2 as textbox
dim Num1 as int
dim Num2 as int
dim ComputationType as int
dim Answer as float

when COMPUTE button pressed
Num1 = TxtboxNum1.ConvertToNumber()
Num2 = TxtboxNum2.ConvertToNumber()

case (ComputationType)
case [+]: Answer = Num1 + Num2
case [-]: Answer = Num1 - Num2
case
: Answer = Num1 * Num2
case [/]: Answer = Num1 / Num2

Display(answer)

EJ123
02-12-08, 05:06 PM
Wow! That looks fantastic. I see you entered the cases near the end. Hm.
The name I gave my text boxes were txtBox1 and txtBox2. When I changed what you said into mine, I get the green squigly lines undernearth :(

Why is that happening? I correctly spelled everything, yet it won't recognice it.

Edit: oh, I moved it to the public class form 1 above the first space for code and it worked except for the txtBox1-it's now a blue squigly.

KingTermite
02-12-08, 05:16 PM
VB.Net does a lot of the work for you. There are bonuses and drawbacks to that. A language like C may be a bit harder, but full control means YOU control everything.

You change the name in once place, but the compiler probably still has references to it as another name elsewhere (properties of the control perhaps??) or in auto generated code near top.

BTW...nothing says your names are wrong. A variable name is just a name, it could be "CharlieEatsLimaBeans" and still be correct. I tend to prefer using variable names that imply what it is....the more you code the more you realize how much that helps. Names like 'A' and 'variable1' don't tell me anything about what it is and it makes you much more likely to confuse your variables.

EJ123
02-12-08, 05:27 PM
If I have the Dim variable variable a different name then what I listed within the properties of that control, how would it work?

jaxgtr
02-12-08, 07:12 PM
I tend to prefer using variable names that imply what it is....the more you code the more you realize how much that helps. Names like 'A' and 'variable1' don't tell me anything about what it is and it makes you much more likely to confuse your variables.

+ 1 billion. Nothing is worse that trying to work on someones code that is not commented or us vague variable names. I hate that.

KingTermite
02-12-08, 08:12 PM
If I have the Dim variable variable a different name then what I listed within the properties of that control, how would it work?

Controls and basic data types are a little bit different.

If you dim a normal variable, it won't have a "properties" window. Only your controls will. I think if you want to change a control you should do it through the properties window.

EJ123
02-12-08, 10:00 PM
SO variables should never use the names of the controls with the same name displayed in the properties window?

I need at least 3 class-level variables (what is that?) in this .exe, and I have no idea how I could use even 1. :(

KingTermite
02-12-08, 10:13 PM
SO variables should never use the names of the controls with the same name displayed in the properties window?

I need at least 3 class-level variables (what is that?) in this .exe, and I have no idea how I could use even 1. :(

I'm not sure I even understand what you are asking.

If you put a control on the form, it automatically gives it a name. Let's say "TextBox1". Right then and there (as soon as you add it), change the name in the properties window to "TextBoxNumber1" or something like that. If you change it in the properties window, it changes it in the code.

However, once you begin using that variable (even though its still a control, it's still a variable) though, changing the name means it needs to be changed in EVERY place its referenced. Usually its easiest to go back and change it in the properties window, then find the places the code chokes when you try to compile to fix the other places you've modified it.


As far as using 3 class level variables.....seems like you will already have at least that many anyway: "Num1", "Num2", "ComputationType", "Result".....

EJ123
02-12-08, 10:27 PM
I still don't get when to use variables. I can't imagine how I even need them in a simple calculator.

EJ123
02-12-08, 10:29 PM
Controls and basic data types are a little bit different.

If you dim a normal variable, it won't have a "properties" window. Only your controls will. I think if you want to change a control you should do it through the properties window.

I just did something like this:
Me.lblAnswer.Text = CInt(txtBox1.Text) + CInt(txtBox2.Text)

and replaced the math sign for each control section. It works heh, but still no need to dim a variable. =/

KingTermite
02-12-08, 10:41 PM
I just did something like this:
Me.lblAnswer.Text = CInt(txtBox1.Text) + CInt(txtBox2.Text)

and replaced the math sign for each control section. It works heh, but still no need to dim a variable. =/

The problem is you are sharp enough to "see" that you don't need the variables. I would do it exactly as you are doing it, but the teacher doesn't probably realize you are able to take that leap yet.

What you can do, and what he probably expects is something like:
Class Level:
Dim Num1 as Integer
Dim Num2 as Integer
Dim Answer as Float


Calculation Section:
Num1 = CInt(txtBox1.Text)
Num2 = CInt(txtBox2.Text)
Answer = Num1 + Num2

Me.lblAnswer.Text = (Convert to string)Answer

EJ123
02-13-08, 06:32 PM
Hi KT. Just one problem. I don't know what a Float it (never talked about it before). Have any idea on what a substitute would be?

Edit: I used the code, and used string in place of the Float and it all works!!!!!! You make it so easy to understand that you should be the teacher:D Thank you!!

chipcom
02-13-08, 07:29 PM
Hi KT. Just one problem. I don't know what a Float it (never talked about it before). Have any idea on what a substitute would be?

Edit: I used the code, and used string in place of the Float and it all works!!!!!! You make it so easy to understand that you should be the teacher:D Thank you!!

A float is a floating decimal number. You need to use this type for doing mathematical operations rather than a string, then convert to a string to display your result in the interface, if needed.

If you use strings for mathematical operations, you're gonna get all kinds of weird results.

jaxgtr
02-13-08, 07:47 PM
Hi KT. Just one problem. I don't know what a Float it (never talked about it before). Have any idea on what a substitute would be?

Edit: I used the code, and used string in place of the Float and it all works!!!!!! You make it so easy to understand that you should be the teacher:D Thank you!!

Dude, I have to ask, are you using a book, in a class or using the fly by night learning on my own training program? These are all basic items that you should have learn in the Introduction to Programming and Algorithm Design COP 1000 or what ever it called where you are. Is this the class you are taking and just using VB.Net or did you already have that class?

Not trying to hack on you, but you might want do some addition research or pick up a language reference at a book store. Good luck.

EJ123
02-13-08, 07:52 PM
Dude, I have to ask, are you using a book, in a class or using the fly by night learning on my own training program? These are all basic items that you should have learn in the Introduction to Programming and Algorithm Design COP 1000 or what ever it called where you are. Is this the class you are taking and just using VB.Net or did you already have that class?

Not trying to hack on you, but you might want do some addition research or pick up a language reference at a book store. Good luck.

We do silly notes in class which do not apply to the assignments, watch free VB tutorials online by some guy who made them (about 23min. videos each), and have weekly quizzes. That's it.

EJ123
02-13-08, 07:53 PM
And now this code won't show up in the exe: http://i7.photobucket.com/albums/y251/viperfx10/Huh.jpg

EJ123
02-13-08, 08:05 PM
Also, does

Dim Num1 as Integer
Dim Num2 as Integer
Dim Answer as Float

constitute as holding the values of the input and the output solution?

jaxgtr
02-13-08, 08:34 PM
No, those are just declaring the variable type. Num1 is being declared as an Integer, etc. The variables are assigned values with the vb assignment statement

variable = value ex: num1 = txtWhatever.text

jaxgtr
02-13-08, 08:44 PM
By the way, what book are you using for this class?

KingTermite
02-13-08, 09:48 PM
And now this code won't show up in the exe: http://i7.photobucket.com/albums/y251/viperfx10/Huh.jpg

Is the properties on the label set to be hidden (by default) perhaps? Are you sure it's in that location and not in a spot that's not visible when you run the form? Is it there when you look at your form in the form view?


Also, does

Dim Num1 as Integer
Dim Num2 as Integer
Dim Answer as Float

constitute as holding the values of the input and the output solution?

I'm not 100% sure I understand that statement. The values of the input are held in the controls (classes) and if you assign them to a variable, now the variable is also holding (a copy of) them.

KingTermite
02-13-08, 09:50 PM
No, those are just declaring the variable type. Num1 is being declared as an Integer, etc. The variables are assigned values with the vb assignment statement

variable = value ex: num1 = txtWhatever.text

Oh great....totally screw him up by showing an assignment of text into a number variable. :rolleyes:

More like:

dim mystring as text
dim mynum as integer

mystring = "this is a string"
myint = 5

KingTermite
02-13-08, 09:52 PM
Dude, I have to ask, are you using a book, in a class or using the fly by night learning on my own training program? These are all basic items that you should have learn in the Introduction to Programming and Algorithm Design COP 1000 or what ever it called where you are. Is this the class you are taking and just using VB.Net or did you already have that class?

Not trying to hack on you, but you might want do some addition research or pick up a language reference at a book store. Good luck.

Easy there Tex....he's a BRAND NEW programmer in his first class. It may have been a long time since you were in a "1st programming class". They typically just give you basics of what you need for the next program and tell you "how" you have to do it. That's "supposed" to make it easier on the students as they are told how to do it....although if you actually learn what you are doing, it can be worse as you quickly realize you can do it a much better way. Just like EJ realized earlier he didn't NEED to store the input into a separate variable.

jaxgtr
02-13-08, 09:55 PM
Oh great....totally screw him up by showing an assignment of text into a number variable. :rolleyes:

More like:

dim mystring as text
dim mynum as integer

mystring = "this is a string"
myint = 5

Oooops, yep your right, it was just a generic entry. :o

jaxgtr
02-13-08, 10:02 PM
Easy there Tex....he's a BRAND NEW programmer in his first class. It may have been a long time since you were in a "1st programming class". They typically just give you basics of what you need for the next program and tell you "how" you have to do it. That's "supposed" to make it easier on the students as they are told how to do it....although if you actually learn what you are doing, it can be worse as you quickly realize you can do it a much better way. Just like EJ realized earlier he didn't NEED to store the input into a separate variable.

Again, I'm not trying to rag him, it just seems like some of these questions were something that might have been or should have been covered in the early stages of the class. I guess maybe they are doing it differently now compared to when I was learning. I guess each instructor has their own way.

EJ123
02-13-08, 10:09 PM
Anyone here know PHP?

KingTermite
02-13-08, 10:09 PM
Oooops, yep your right, it was just a generic entry. :o

I knew you were being generic...I just wanted to clarify to make sure it didn't confuse him.

jaxgtr
02-13-08, 10:13 PM
Learn VB before moving to PHP

KingTermite
02-13-08, 10:13 PM
Again, I'm not trying to rag him, it just seems like some of these questions were something that might have been or should have been covered in the early stages of the class. I guess maybe they are doing it differently now compared to when I was learning. I guess each instructor has their own way.

Even when I took programming classes in college (13-15 years ago), they didn't each ALL the data types at once and ALL the control structures at once and ALL compiler concepts (kinda required in the .NET world) at once. It would have been too much for a poor newbie to comprehend.

They would teach a few data types and a few control structures and then give a program that required those particular ones. Then get into some more advanced data types and control structures and do the same until they were doing things like records/structs/classes.

The problem came in when a student DID go above and read ahead in the book or read thoroughly to learn the other things "available" and then get confused because he KNEW he could do it a better way and he would think the way he was supposed to do it was wrong, thus confusing him. I think that's what happened to EJ earlier regarding the variables that weren't needed.

KingTermite
02-13-08, 10:14 PM
Anyone here know PHP?

I agree with Jax......VB is about as easy as its going to get. Learn VB decently well before confusing yourself with another language.

jaxgtr
02-13-08, 10:28 PM
The problem came in when a student DID go above and read ahead in the book or read thoroughly to learn the other things "available" and then get confused because he KNEW he could do it a better way and he would think the way he was supposed to do it was wrong, thus confusing him.

Yea, this was me, only I taught myself VBA using Access 2 and Excel 4 first doing stuff at my job and then I moved into SQL and VB 4/5/6, HTML, etc. Still work more in the SQL and VBA, some lite C++ if and only if, I have to. Never really got into the .Net world, but do mess around with it on occasion. I really like it compared to the old VB, more intuitive and less code work involved for a lot of stuff. When I started to work on getting my degree, I was like, come on, I know this stuff already, lets get to the hard stuff I don't know. I had to take a MS Access class which was a pre-req for the SQL class. I decide to take it online and I did the entire course in two days. I just showed up online every couple of days just to log my ID for class participation grade and thenI found out that the professor was a guy I used to work with that I had designed some Access DB's for. He just gave me an A for the course and I never checked in for the rest of the class. Needless to say it was interesting.

Jay68442
02-13-08, 10:33 PM
Oh ok. I figured out how to do the hide labels. I just don't understand which variables to set, and to set them as what, and how to execute the math functions. Why can't it be something as easy as "me.lblAnswer.show(txtBox1 + txtBox2) when the user clicks the addition button? Arg. Will my variables strictly be string or will they be integer? I thought I needed to label the txtBox as a variable.

if your using vb.net it would look like:
lblAnswer.text = txtBox1.text + txtBox2.text

That is of couse if you only allow whole numbers. If you insert decimals into these textboxes you will not get the correct answer. Instead use

lblAnswer.Text = CType(txtBox1.Text, Decimal) + CType(txtBox2.Text, Decimal)

StrangeWill
02-14-08, 01:00 AM
Learn VB before moving to PHP

I agree with Jax......VB is about as easy as its going to get. Learn VB decently well before confusing yourself with another language.
I would agree with KingTermite if this was VB 6.0, .NET has added a bit of complication, PHP doesn't have types (err it does, but for all intents and purposes you can pretty much ignore them as a new programmer) and is pretty basic, you're just exporting HTML documents so you don't have to deal with form management. Mainly what I don't like is the amount of complication added on top of the .NET project structure, very undesirable for new programmers I'd think.

However, a mistake in VB = crashed program, a mistake in PHP = possibly security hole in site.

Personally I like C# over VB.NET, the syntax feels so much better, of course now I'm mostly working in C++/PHP for now.


I started as a VB 6.0 programmer. An excellent place to start, though a little dated and it does have it's issues that you have to program around.

chipcom
02-14-08, 08:16 AM
I agree with Jax......VB is about as easy as its going to get. Learn VB decently well before confusing yourself with another language.

I gotta disagree...though it's probably a moot point. IMO it's better to learn a language using the C type syntax, which is in wider use and applies to many more languages, first - then move into VB, which is a totally different syntax, rather than learning VB first. Indeed, when it comes to .Net, screw VB, C# is the only way to fly! Of course that's just my own opinion. ;)

KingTermite
02-14-08, 08:23 AM
I started as a VB 6.0 programmer. An excellent place to start, though a little dated and it does have it's issues that you have to program around.
VB6 would definitely have been an easier starting point than VB.Net.



I gotta disagree...though it's probably a moot point. IMO it's better to learn a language using the C type syntax, which is in wider use and applies to many more languages, first - then move into VB, which is a totally different syntax, rather than learning VB first. Indeed, when it comes to .Net, screw VB, C# is the only way to fly! Of course that's just my own opinion. ;)

I completely agree that c-type syntax languages are more useful, but I don't think easier to learn. The syntax is more complicated and you're fighting syntax of simple programming concepts (like data types, control structures, etc....).

C# is my favorite language to use too, but it's really OO oriented and would be hard (harder than VB anyway) to pick up as a first language, I think.

chipcom
02-14-08, 08:41 AM
I completely agree that c-type syntax languages are more useful, but I don't think easier to learn. The syntax is more complicated and you're fighting syntax of simple programming concepts (like data types, control structures, etc....).

C# is my favorite language to use too, but it's really OO oriented and would be hard (harder than VB anyway) to pick up as a first language, I think.

Everyone's first language should be machine code. If you can stomach writing hundreds of lines to do the most basic tasks...then you just might be cut out to be a programmer! :D

Seriously, I find OO to be so much simpler to grasp and use than the old procedural languages I started out with.

KingTermite
02-14-08, 08:44 AM
Everyone's first language should be machine code. If you can stomach writing hundreds of lines to do the most basic tasks...then you just might be cut out to be a programmer! :D

I think you are mistaking machine code with assembly.

machine code is even worse. Take assembly, then convert it to hex numeric data for each individual instruction to make sure proper ALU control lines are set for the right operation, etc....

StrangeWill
02-14-08, 02:15 PM
I think you are mistaking machine code with assembly.

machine code is even worse. Take assembly, then convert it to hex numeric data for each individual instruction to make sure proper ALU control lines are set for the right operation, etc....

Assembly was a bloody nightmare, writing pages of code to do barely anything, I felt like such a useless programmer.

KingTermite
02-14-08, 02:51 PM
Assembly was a bloody nightmare, writing pages of code to do barely anything, I felt like such a useless programmer.

Join the club.

When I took the class that did assembly we had to do it on an IBM mainframe too. Not only the assembly itself, but fighting an unfamiliar environment just to execute it. It had to be wrapped in a JCL wrapper script and submitted as a mainframe job.

One assignment made up compute sines and cosines via Taylor series formulas, compute whether a number was prime, and compute whether it was odd (the only "easy" one).

chipcom
02-14-08, 02:59 PM
I think you are mistaking machine code with assembly.

machine code is even worse. Take assembly, then convert it to hex numeric data for each individual instruction to make sure proper ALU control lines are set for the right operation, etc....

Yep, that's machine code. Human-friendly code is for wussy.

jaxgtr
02-14-08, 06:32 PM
Yea, did some cobol for a while and after the mind numbing world of that, I had to move on.