|
-
Feb 12th, 2008, 07:20 PM
#1
Thread Starter
Addicted Member
I need SIMPLE VB help (really easy question)
for VB 05 express.
I need to make a basic calculator. I'm having trouble with this, and it's due next monday, but I need to finish it asap.
I don't understand the formulas for the +,-,/, *
I have two text boxes, four buttons for the signs, and 3 labels. I don't even know where to begin with the variables. 
Why can't I just make something simple as (when someone inputs the data into the text boxes, followed by clicking the "addition" button):
Me.lblAnswer.show(txtBox1 + txtBox2) and have that work out alright?
Please help me! I have no idea of the right coding.
-
Feb 12th, 2008, 07:22 PM
#2
Re: I need SIMPLE VB help (really easy question)

It seems that you are using .Net 2005 so I've moved your thread to here.
Last edited by MartinLiss; Feb 12th, 2008 at 07:26 PM.
-
Feb 12th, 2008, 07:47 PM
#3
Re: I need SIMPLE VB help (really easy question)
 Originally Posted by Seahawks1
Why can't I just make something simple as (when someone inputs the data into the text boxes, followed by clicking the "addition" button):
Me.lblAnswer.show(txtBox1 + txtBox2) and have that work out alright?
Because textboxes hold strings, and the + operator, when used on strings, concatenates them together. What you have would actually work if you wrote this:
Me.lblAnswer.Text = CInt(txtBox1.text) + CInt(txtBox2.text)
What this does is take the text property of the textboxes (which is the contents), convert them into integers using CInt(), and put them into the text property of the label.
There are a few issues with that code, though, because if what you have in the text box is either a blank string, or a string that has any non-numerical character, then CInt will throw an exception. For your situation, you can probably ignore that, though it would not be good practice. A better solution would be this:
vb Code:
Dim n1 as integer
Dim n2 as integer
If Integer.TryParse(Me.txtBox1.Text,n1) AndAlso Integer.TryParse(Me.txtBox2.Text,n2) then
me.lblAnswer.Text = (n1+n2).ToString
Else
MessageBox.Show ("That ain't a number you mo-ron!","I Spit at Thee")
end if
That uses TryParse which returns True if the string can be converted, and false if it can't. Much more interesting variations on this code are possible.
My usual boring signature: Nothing
 
-
Feb 12th, 2008, 08:49 PM
#4
Thread Starter
Addicted Member
Re: I need SIMPLE VB help (really easy question)
 Originally Posted by Shaggy Hiker
Because textboxes hold strings, and the + operator, when used on strings, concatenates them together. What you have would actually work if you wrote this:
Me.lblAnswer.Text = CInt(txtBox1.text) + CInt(txtBox2.text)
What this does is take the text property of the textboxes (which is the contents), convert them into integers using CInt(), and put them into the text property of the label.
There are a few issues with that code, though, because if what you have in the text box is either a blank string, or a string that has any non-numerical character, then CInt will throw an exception. For your situation, you can probably ignore that, though it would not be good practice. A better solution would be this:
vb Code:
Dim n1 as integer
Dim n2 as integer
If Integer.TryParse(Me.txtBox1.Text,n1) AndAlso Integer.TryParse(Me.txtBox2.Text,n2) then
me.lblAnswer.Text = (n1+n2).ToString
Else
MessageBox.Show ("That ain't a number you mo-ron!","I Spit at Thee")
end if
That uses TryParse which returns True if the string can be converted, and false if it can't. Much more interesting variations on this code are possible.
But what is n1 and n2? How does VB know what Im referring to? I don't even know what that is referring to
-
Feb 12th, 2008, 09:14 PM
#5
Re: I need SIMPLE VB help (really easy question)
This:is a declaration. When you declare something you are telling the compiler that it exists and how to use it. That is telling the compiler to allocate memory for an Integer value that will be referred to by the name n1.
This:
vb.net Code:
Integer.TryParse(Me.txtBox1.Text, n1)
is calling the TryParse method of the Integer type. What that does is take the string you pass to the first argument, which in this case is the Text of the TextBox, and attempts to parse it into an Integer. That means that it attempts to interpret the characters in the string as numeric digits. If it succeeds it will assign that Integer value to the n1 variable you declared before and return True. If it fails n1 will be set to zero and it will return False.
So, reading Shaggy's code in English it says:
Create an Integer variable named n1.
Create an Integer variable named n2.
If the text in txtBox1 can be interpreted as an Integer and that Integer assigned to the variable n1 and also if the text in txtBox2 can be interpreted as an Integer and that Integer assigned to n2 then add n1 and n2 together, convert the result to a string and display that string in lblAnswer. Otherwise display a very impolite message to the user pointing out their low IQ.
You need to do a lot of work. I suggest that you read over your notes furiously and read as many beginner tutorials as you can.
-
Feb 12th, 2008, 09:53 PM
#6
Re: I need SIMPLE VB help (really easy question)
 Originally Posted by jmcilhinney
Otherwise display a very impolite message to the user pointing out their low IQ.
That part isn't technically necessary, and some people even frown on it, but when I hear somebody burst out laughing when they are using one of my programs, I know that I worded the message well. Why be boring when you can entertain?
My usual boring signature: Nothing
 
-
Feb 12th, 2008, 10:02 PM
#7
Thread Starter
Addicted Member
Re: I need SIMPLE VB help (really easy question)
What's it mean to declare three variables at the class level as a means to hold the values of the input and the output solution?
-
Feb 12th, 2008, 10:19 PM
#8
Re: I need SIMPLE VB help (really easy question)
 Originally Posted by Seahawks1
What's it mean to declare three variables at the class level as a means to hold the values of the input and the output solution?
Declaring variables is about the most elementary thing you can do in programming. If you don't know what that means then you are in more trouble than I thought. You really need to go through what you've learned in class because you seem to have missed out on the absolute basics.
When you declare a variable it can either be inside a method, in which case it's local, or outside a method, in which case it's a member. Member variables are declared at the class level rather than the method level.
-
Feb 12th, 2008, 10:28 PM
#9
Thread Starter
Addicted Member
Re: I need SIMPLE VB help (really easy question)
What is a class level?
I don't get making up some variable name like N1, and then just randomly assigning it to something.
Now I'm having trouble with one of the labels displaying the mathmatical sign (+,-,*,/) when the user clicks on either 4 of the add/subtract/divide/or multiply buttons.
I tried:
Me.lblEquals.Text = "=" <<(that works)
but when I tried (for the sign label): Me.lblSign.Text = "+"
it wouldn't work. This was all under the btnAdd code section, so under the subtraction section it would be "Me.lblSign.Text = "-"
Any idea why the code won't show the sign at lblSign?
-
Feb 12th, 2008, 10:46 PM
#10
Re: I need SIMPLE VB help (really easy question)
OK, this is the last question I'll answer that you should have known after your very first programming lesson.
As I have pointed out in my previous post, if you declare a variable within a class definition but not within a method definition then that variable is said to be declared at the class level. If you declare a variable within a method definition then it is said to be declared at the method level. Class-level variables are also known as member variables and method-level variables are also known as local variables.
vb.net Code:
Public Class SomeClass Private thisIsAClassLevelOrMemberVariable As String Public Sub SomeMethod() Dim thisIsAMethodLevelOrLocalVariable As Integer End Sub End Class
No-one is just making up a variable name like N! and randomly assigning something to it. Shaggy's code declared two variables named n1 and n2, which is short for number 1 and number 2, i.e. the first number and then second number. It then parsed the text entered by the user in the two TextBoxes and specifially assigned the result to those variables. Once the user input has been converted to numbers then you can perform mathematics on it. You cannot perform mathematics on strings.
As for your missing "+" sign, things don't just not work. If there was no exception thrown then it worked, so either you're overwriting it somewhere else or you haven't made the Label big enough to see it or something like that.
-
Feb 12th, 2008, 11:17 PM
#11
Lively Member
Re: I need SIMPLE VB help (really easy question)
'Add these Code to correspoding button click event
' To add (+)
Me.lblAnswer.Text = iif(trim(txtBox1.text)="",0,Val(txtBox1.text)) + iif(trim(txtBox2.text)="",0,Val(txtBox2.text))
' To Subtract (-)
Me.lblAnswer.Text = iif(trim(txtBox1.text)="",0,Val(txtBox1.text)) - iif(trim(txtBox2.text)="",0,Val(txtBox2.text))
' To Multiply (*)
Me.lblAnswer.Text = iif(trim(txtBox1.text)="",0,Val(txtBox1.text)) * iif(trim(txtBox2.text)="",0,Val(txtBox2.text))
' To Devide (/)
Me.lblAnswer.Text = iif(trim(txtBox1.text)="",0,Val(txtBox1.text)) / iif(trim(txtBox2.text)="",0,Val(txtBox2.text))
Hope this solves your problem, Good Luck
-
Feb 12th, 2008, 11:34 PM
#12
Re: I need SIMPLE VB help (really easy question)
Is iif still around in VB05? I thought it wasn't, but I can't keep it straight. There's something new in 08, but I'm still on 05.
My usual boring signature: Nothing
 
-
Feb 12th, 2008, 11:43 PM
#13
Re: I need SIMPLE VB help (really easy question)
VB 2008 has the If operator, although The IIf Runtime function presumably still exists in VB 2008, as it does in VB 2005. It should be noted that both the IIf function and the If operator return an Object reference. That means that ComITSolutions code will not compile under Option Strict On. That's not likely to be a big issue here but everyone should have it On anyway.
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
|