Results 1 to 13 of 13

Thread: I need SIMPLE VB help (really easy question)

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Feb 2008
    Location
    Seattle.
    Posts
    176

    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.

  2. #2

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: I need SIMPLE VB help (really easy question)

    Quote 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:
    1. Dim n1 as integer
    2. Dim n2 as integer
    3.  
    4. If Integer.TryParse(Me.txtBox1.Text,n1) AndAlso Integer.TryParse(Me.txtBox2.Text,n2) then
    5.  me.lblAnswer.Text = (n1+n2).ToString
    6. Else
    7.  MessageBox.Show ("That ain't a number you mo-ron!","I Spit at Thee")
    8. 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

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Feb 2008
    Location
    Seattle.
    Posts
    176

    Re: I need SIMPLE VB help (really easy question)

    Quote 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:
    1. Dim n1 as integer
    2. Dim n2 as integer
    3.  
    4. If Integer.TryParse(Me.txtBox1.Text,n1) AndAlso Integer.TryParse(Me.txtBox2.Text,n2) then
    5.  me.lblAnswer.Text = (n1+n2).ToString
    6. Else
    7.  MessageBox.Show ("That ain't a number you mo-ron!","I Spit at Thee")
    8. 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

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: I need SIMPLE VB help (really easy question)

    This:
    vb.net Code:
    1. Dim n1 As Integer
    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:
    1. 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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: I need SIMPLE VB help (really easy question)

    Quote 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

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Feb 2008
    Location
    Seattle.
    Posts
    176

    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?

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: I need SIMPLE VB help (really easy question)

    Quote 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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Feb 2008
    Location
    Seattle.
    Posts
    176

    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?

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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:
    1. Public Class SomeClass
    2.  
    3.     Private thisIsAClassLevelOrMemberVariable As String
    4.  
    5.     Public Sub SomeMethod()
    6.         Dim thisIsAMethodLevelOrLocalVariable As Integer
    7.  
    8.     End Sub
    9. 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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  11. #11
    Lively Member ComITSolutions's Avatar
    Join Date
    Feb 2008
    Location
    Bangalore
    Posts
    94

    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

  12. #12
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    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

  13. #13
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width