473,320 Members | 2,189 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,320 software developers and data experts.

Declaring variables on same line fixes overflow

3
I have a program to calculate wattage from voltage and amps. If I declare V and I on different lines, then I get an overflow when V = 1000 and A = 100. If I declare them on the same line, then the overflow is fixed and doesn't occour

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdCalc_Click()
  2.  
  3. Dim W As Long
  4. Dim Amps As Integer
  5. Dim Voltage As Integer
  6.  
  7. Voltage = CInt(txtVolts.Text)
  8. Amps = CInt(txtAmps.Text)
  9.  
  10. If Voltage >= 1 And Voltage <= 1000 Then
  11.     If Amps >= 1 And Amps <= 100 Then
  12.         W = CLng(Voltage * Amps)
  13.         txtWatts.Text = W
  14.     Else
  15.         MsgBox "Enter amps between 1 and 100"
  16.     End If
  17. Else
  18.     MsgBox "Enter volts between 1 and 1000"
  19. End If
  20.  
  21. End Sub
Nov 14 '12 #1
7 1647
Rabbit
12,516 Expert Mod 8TB
Please use code tags when posting code.

Show us your declaration when it's on one line.

And is this VBA or VB? Because this is the VB forum and I suspect you're using VBA.
Nov 14 '12 #2
Callum
3
Expand|Select|Wrap|Line Numbers
  1. Dim Voltage, Amps As Integer
This is VB6.
Nov 14 '12 #3
Callum, this line
Expand|Select|Wrap|Line Numbers
  1. Dim Voltage, Amps as Integer
  2.  
is equivalent to
Expand|Select|Wrap|Line Numbers
  1. Dim Voltage as Variant
  2. DIm Amps as Integer
  3.  
Not to this, as one might think
Expand|Select|Wrap|Line Numbers
  1. Dim Voltage as Integer
  2. DIm Amps as Integer
  3.  
Nov 14 '12 #4
Overflow does not occur because being Voltage a Variant, it becomes a Long which has an upper limit of (2^31)-1, whereas Integer upper limit equals (2^15)-1
Nov 14 '12 #5
Rabbit
12,516 Expert Mod 8TB
Lexly is absolutely correct. On one line, the equivalent would be:
Expand|Select|Wrap|Line Numbers
  1. Dim Voltage as Integer, Amps as Integer 
Which will result in an overflow. Just declare them as Longs.
Nov 14 '12 #6
Callum
3
Didn't realise the variable defaulted to variant when done one line. Thanks
Nov 14 '12 #7
Killer42
8,435 Expert 8TB
Variables always default to Variant type, unless you specify a type. The important point here is not that they were on a single line, but that you didn't specify a type for the first one.

You would get the same effect if you coded
Expand|Select|Wrap|Line Numbers
  1. Dim Voltage
  2. Dim Amps As Integer
Nov 15 '12 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: Stefanie | last post by:
Hi, I have a question about declaring variables for writing data to a file for random access. I have two string variables declared in a module (user-defined types). The point is that for one of...
17
by: Stan Brown | last post by:
This seems simple, but I've been unable to figure out how to do it. I have a quotation in a paragraph of (let's assume) more than one line. I want to put the speaker's name at the end of the last...
7
by: opt_inf_env | last post by:
Hello, I would like to create a button which should be put in the end of a sentence (on the same line). I do like this: <html> This is my sentence. <form action=file.php> <input...
1
by: ColinWard | last post by:
Hi guys. I have a question about declaring variables. I do a lot of re-querying of controls in my database and I use the Set statement with a variable set to the name of the control to tell the...
5
by: Ant | last post by:
hi, I'm now using C#. Seeing as though you can declare & initialize or pass a value to a variable on the same line as the declaration, is it still best practice to group all the variables together...
1
by: swat | last post by:
hi im the beginner of the c++ ...how can i build the programm so that in the output box i can input a number n in the first line and in the second line i input n numbers on the same line with a...
6
by: =?Utf-8?B?QUw=?= | last post by:
Hi I usually stick to the convention of not declaring variables in my bodies of "loops" (including foreach) ie int x; for (int i = 0; i < 10; i++) {
5
by: HMS Surprise | last post by:
I want to print a count down timer on the same line. I tried print '\r', timeLeft, which just appends to the same line. thanx,
0
by: Berlin Brown | last post by:
I have two DIVs on the same line, a left navigation frame and the right content. I want both of those items on the same line. Lets say the left nav content is 140pixels and the right content is...
5
by: chuckiechan | last post by:
This is what I would like it to do. Enter the search file to look in April_hours Enter your search item 50 150 #example of text in document April1, 50 100 150 75 200 April2, 100 150 200 75 250...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.