473,386 Members | 1,679 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

new to coding

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
Jun 29 '07 #1
6 1270
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
"dick23" <di****@discussions.microsoft.comwrote in message
news:ED**********************************@microsof t.com...
>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

Jun 29 '07 #2
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
"John Timney (MVP)" wrote:
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
"dick23" <di****@discussions.microsoft.comwrote in message
news:ED**********************************@microsof t.com...
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


Jun 29 '07 #3
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/ex...b/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" <di****@discussions.microsoft.comwrote in message
news:50**********************************@microsof t.com...
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
"John Timney (MVP)" wrote:
>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
"dick23" <di****@discussions.microsoft.comwrote in message
news:ED**********************************@microso ft.com...
>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



Jun 29 '07 #4
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" wrote:
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/ex...b/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" <di****@discussions.microsoft.comwrote in message
news:50**********************************@microsof t.com...
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
"John Timney (MVP)" wrote:
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
"dick23" <di****@discussions.microsoft.comwrote in message
news:ED**********************************@microsof t.com...
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


Jun 29 '07 #5

"dick23" <di****@discussions.microsoft.comwrote in message
news:89**********************************@microsof t.com...
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/ex.../learningpath/

Jun 29 '07 #6
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" <di****@discussions.microsoft.comwrote in message
news:89**********************************@microsof t.com...
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" wrote:
>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/ex...b/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" <di****@discussions.microsoft.comwrote in message
news:50**********************************@microso ft.com...
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
"John Timney (MVP)" wrote:

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
"dick23" <di****@discussions.microsoft.comwrote in message
news:ED**********************************@microso ft.com...
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



Jul 2 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: ganesan | last post by:
Hi Guys, Could any one knows the best coding standards styles(with variable declarations for c#) . and if any links or site with the best coding standards for .NET send me those links regards...
4
by: dotNetDave | last post by:
About three weeks ago I released the first .NET coding standards book titled "VSDN Tips & Tricks .NET Coding Standards". Here is what the famous author/ speaker Deborah Kurata says about it: ...
0
by: Berthold Höllmann | last post by:
I have a default coding header # -*- coding: iso-8859-15 -*- in my python files. I now have Problems with this settings. I swithched to Python 2.4.1 under Windows. When I import files with the...
144
by: Natt Serrasalmus | last post by:
After years of operating without any coding standards whatsoever, the company that I recently started working for has decided that it might be a good idea to have some. I'm involved in this...
7
by: Ralph Lund | last post by:
Hi. I am starting a new project with C#. I am searching for "good" coding conventions. I know that there are some coding conventions from microsoft, (but they are very extensive and not clear)....
13
by: benben | last post by:
Is there an effort to unify the c++ coding standard? Especially identifier naming. Not a big issue but it would be annoying to have to incorporate different coding styles simultaneously when...
7
by: Robert Seacord | last post by:
The CERT/CC has just deployed a new web site dedicated to developing secure coding standards for the C programming language, C++, and eventually other programming language. We have already...
3
by: editormt | last post by:
A recent poll asked if programming standards are used by development organisations... and if they are controlled. None: 20% Yes, but without control: 49% Yes, with control: 31% Participants:...
0
by: pat | last post by:
CodeCheck Coding Standard's Support As a free service to our customers we offer support in developing "rule-files" for automating corporate coding standards. If you have a coding standard that...
8
by: =?ISO-8859-1?Q?Arnaud_Carr=E9?= | last post by:
Hi all, I guess you all know how difficult it is to choose a conding standard. And even more difficult it is to explain the choice to your dev team :-) I'm looking for an "official" c++ coding...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.