new to coding

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am just a beginner coding in VB6. I am trying to write a simple code to
add 2 numbers together and put the answer in a text box
I have one textbox open to accept each number, a textbox to accept the
answer and a button where I am adding this code
Dim intNumber1 As Integer
Dim intNumber2 As Integer
Dim intAnswer As Integer
intAnswer = intNumber1 + intNumber2
txtAnswer.Text = intAnswer
How come I keep getting 0 as an anwer
Thanks in advance
 
No I haven 't assigned them a number. If I want the user to input the
numbers and come up with an answer then I guess Integer is not the correct
Data Type to use
I am liitle new to this, I have a book I am using called Visual Basic 2005
but download the VB6 express edition from Microsoft
 
I think John was wondering why you were using VB6 Express edition rather
than Visual Basic 2005 Express Edition. You can get Visual Basic 2005
Express for free from the following web site:

http://msdn.microsoft.com/vstudio/express/vb/default.aspx

Integer is the correct data type to use. A data type indicates what type of
data your program is working with for a certain operation. This is because
numbers, for example, can be represented using characters, but you can't add
characters; you can only add numbers. On the other hand, you can place 2
numbers "next to" each other to form a new number. For example:

Dim one As String = "1"
Dim two As String = "2"
txtAnswer.Text = one + two ' answer is "12"
txtAnswer.Text = two - one ' exception thrown

Dim a as Integer = 1
Dim b as Integer = 2
txtAnswer.Text = a + b ' answer is 3
txtAnswer.Text = a - b ' answer is -1
txtAnswer.Text = a & b ' exception thrown

Now, there will may some confusion, as VB allows you to optionally use
incorrect data types by using the compiler option strict off, which is
unfortunately the default. This means that the compiler will perform some
data type conversions automagically, such as:

txtAnswer.Text = a + b ' answer is 3

txtAnswer is a TextBox. It contains text that the user types or works with
as text, or String data type. When option strict is off, the above line of
code will automagically convert [a + b] (3) to "3". But don't get them mixed
up. It's always better to keep track of your data types. Even with option
strict on, the following will work:

txtAnswer.Text = CType(a + b, String) ' converts 3 to "3"

If you don't keep track of your data types, you may experience errors that
are hard to track down.

Now, as to assigning them values, you can do this programmatically, as I've
illustrated above, or you can get input from the user, as in:

Dim first as Integer
Dim second as Integer

first = Convert.ToInt32(txtInputFirst.Text)
second = Convert.ToInt32(txtInputSecond.Text)
txtAnswer.Text = CType(first + second, String)

--
HTH,

Kevin Spencer
Microsoft MVP

Printing Components, Email Components,
FTP Client Classes, Enhanced Data Controls, much more.
DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net
 
Thank you Kevin, that helped but I am still confused as to why my code is
only giving me 0 as an answer. Can you tell me where I am going wrong

Kevin Spencer said:
I think John was wondering why you were using VB6 Express edition rather
than Visual Basic 2005 Express Edition. You can get Visual Basic 2005
Express for free from the following web site:

http://msdn.microsoft.com/vstudio/express/vb/default.aspx

Integer is the correct data type to use. A data type indicates what type of
data your program is working with for a certain operation. This is because
numbers, for example, can be represented using characters, but you can't add
characters; you can only add numbers. On the other hand, you can place 2
numbers "next to" each other to form a new number. For example:

Dim one As String = "1"
Dim two As String = "2"
txtAnswer.Text = one + two ' answer is "12"
txtAnswer.Text = two - one ' exception thrown

Dim a as Integer = 1
Dim b as Integer = 2
txtAnswer.Text = a + b ' answer is 3
txtAnswer.Text = a - b ' answer is -1
txtAnswer.Text = a & b ' exception thrown

Now, there will may some confusion, as VB allows you to optionally use
incorrect data types by using the compiler option strict off, which is
unfortunately the default. This means that the compiler will perform some
data type conversions automagically, such as:

txtAnswer.Text = a + b ' answer is 3

txtAnswer is a TextBox. It contains text that the user types or works with
as text, or String data type. When option strict is off, the above line of
code will automagically convert [a + b] (3) to "3". But don't get them mixed
up. It's always better to keep track of your data types. Even with option
strict on, the following will work:

txtAnswer.Text = CType(a + b, String) ' converts 3 to "3"

If you don't keep track of your data types, you may experience errors that
are hard to track down.

Now, as to assigning them values, you can do this programmatically, as I've
illustrated above, or you can get input from the user, as in:

Dim first as Integer
Dim second as Integer

first = Convert.ToInt32(txtInputFirst.Text)
second = Convert.ToInt32(txtInputSecond.Text)
txtAnswer.Text = CType(first + second, String)

--
HTH,

Kevin Spencer
Microsoft MVP

Printing Components, Email Components,
FTP Client Classes, Enhanced Data Controls, much more.
DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net

dick23 said:
No I haven 't assigned them a number. If I want the user to input the
numbers and come up with an answer then I guess Integer is not the correct
Data Type to use
I am liitle new to this, I have a book I am using called Visual Basic 2005
but download the VB6 express edition from Microsoft
 
dick23 said:
Thank you Kevin, that helped but I am still confused as to why my code is
only giving me 0 as an answer. Can you tell me where I am going wrong

That's because you didn't set the Integer.

VB is going to set the number to 0 if you don't set it to a number > 0.

dim int1 as Integer = 1
dim int2 as Integer = 2

dim intAnswer as Integer = int1 + int2

If you don't initialize the any numeric variable, then it's value is set to
zero.

You could to this too, after a dim int1 as Integer.

int1 = 1
int2 = 2

intAnswer = int1 + int2

If you have a number in a textbox, then you have to convert the string
number into a type of Integer.

int1 = cInt(tbxNumber1.Text)
int2 = cInt(tbxNumber2.Text)

intAnswer = int1 + int2

tbxAnswer.Text = intAnswer.ToSring

or

tbxAnswer..Text = cStr(intAnswer.Text)

You can also use the Convert as well.

int1 = Convert.toInt16(tbxNumber1.Text)

http://msdn.microsoft.com/vstudio/express/beginner/learningpath/
 
I gave you examples of assigning values to variables, both programmatically
and via user input. Your problem was that you didn't assign values to your
variables.

Think of a variable as a "box" for holding data. When you refer to the "box"
name, you are actually referring to what is IN the box. If you don't put
anything into the box, what is in it? Assigning variables is the process of
putting data into the box.

--
HTH,

Kevin Spencer
Microsoft MVP

Printing Components, Email Components,
FTP Client Classes, Enhanced Data Controls, much more.
DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net

dick23 said:
Thank you Kevin, that helped but I am still confused as to why my code is
only giving me 0 as an answer. Can you tell me where I am going wrong

Kevin Spencer said:
I think John was wondering why you were using VB6 Express edition rather
than Visual Basic 2005 Express Edition. You can get Visual Basic 2005
Express for free from the following web site:

http://msdn.microsoft.com/vstudio/express/vb/default.aspx

Integer is the correct data type to use. A data type indicates what type
of
data your program is working with for a certain operation. This is
because
numbers, for example, can be represented using characters, but you can't
add
characters; you can only add numbers. On the other hand, you can place 2
numbers "next to" each other to form a new number. For example:

Dim one As String = "1"
Dim two As String = "2"
txtAnswer.Text = one + two ' answer is "12"
txtAnswer.Text = two - one ' exception thrown

Dim a as Integer = 1
Dim b as Integer = 2
txtAnswer.Text = a + b ' answer is 3
txtAnswer.Text = a - b ' answer is -1
txtAnswer.Text = a & b ' exception thrown

Now, there will may some confusion, as VB allows you to optionally use
incorrect data types by using the compiler option strict off, which is
unfortunately the default. This means that the compiler will perform some
data type conversions automagically, such as:

txtAnswer.Text = a + b ' answer is 3

txtAnswer is a TextBox. It contains text that the user types or works
with
as text, or String data type. When option strict is off, the above line
of
code will automagically convert [a + b] (3) to "3". But don't get them
mixed
up. It's always better to keep track of your data types. Even with option
strict on, the following will work:

txtAnswer.Text = CType(a + b, String) ' converts 3 to "3"

If you don't keep track of your data types, you may experience errors
that
are hard to track down.

Now, as to assigning them values, you can do this programmatically, as
I've
illustrated above, or you can get input from the user, as in:

Dim first as Integer
Dim second as Integer

first = Convert.ToInt32(txtInputFirst.Text)
second = Convert.ToInt32(txtInputSecond.Text)
txtAnswer.Text = CType(first + second, String)

--
HTH,

Kevin Spencer
Microsoft MVP

Printing Components, Email Components,
FTP Client Classes, Enhanced Data Controls, much more.
DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net

dick23 said:
No I haven 't assigned them a number. If I want the user to input the
numbers and come up with an answer then I guess Integer is not the
correct
Data Type to use
I am liitle new to this, I have a book I am using called Visual Basic
2005
but download the VB6 express edition from Microsoft


:

have you assigned a value to your numbers? Incidentally, why are you
learning to code in vb6?

Regards

John Timney (MVP)
http://www.johntimney.com
http://www.johntimney.com/blog


I am just a beginner coding in VB6. I am trying to write a simple
code
to
add 2 numbers together and put the answer in a text box
I have one textbox open to accept each number, a textbox to accept
the
answer and a button where I am adding this code
Dim intNumber1 As Integer
Dim intNumber2 As Integer
Dim intAnswer As Integer
intAnswer = intNumber1 + intNumber2
txtAnswer.Text = intAnswer
How come I keep getting 0 as an anwer
Thanks in advance
 
Back
Top