Points of Required Attention™
Please chime in on a proposed restructuring of the ROM hacking sections.
Views: 88,501,973
Main | FAQ | Uploader | IRC chat | Radio | Memberlist | Active users | Latest posts | Calendar | Stats | Online users | Search 04-29-24 06:27 PM
Guest: Register | Login

0 users currently in Computing | 1 guest

Main - Computing - Visual Basic help, really basic. New thread | New reply


darkwitch
Posted on 04-04-07 04:37 AM (rev. 2 of 04-04-07 04:40 AM) Link | Quote | ID: 23092


Paragoomba
Level: 20

Posts: 52/65
EXP: 41519
Next: 920

Since: 02-21-07
From: Puerto Rico

Last post: 6224 days
Last view: 6224 days
So, I am trying to learn a third programing language and I choosed Visual Basic (6.0), I find it quite easy and decided to code a calculator, I managed to make the value add or subtract when I hit the equals button, etc... Here is the problem:

If instep = 0 Then
If method = subtract Then
res.Text = val1 - val2
End If
If method = Add Then
res.Text = val1 + val2
End If
End If
val1 = 0
val2 = 0
method = ""
End Sub

That's triggered when I hit the equals button, well, lets say I tried "1+1" it brought 2, but if I put next "5-3" it gives me 8. I am sure the problem is with the "method" variable but I can't figure it out.

The Add button changes the "method" variable to "Add".
The subtract button changes the "method" variable to "subtract".


Also, I haven't figured how to add a number to an integer like:
I have 'number' as "5" if I hit the 6 button it replaces it to "6" instead of changing it to '56'. I tried using += but that just gives me an error.

Help would be really appreciated

Sukasa
Posted on 04-04-07 06:12 AM Link | Quote | ID: 23140


Red Birdo
Level: 92

Posts: 119/2112
EXP: 7691395
Next: 65542

Since: 02-19-07

Last post: 4451 days
Last view: 3223 days
Try
If instep = 0 Then
If method = "Subtract" Then 'If it's subtracting...
res.Text = val1 - val2
Else 'otherwise add
res.Text = val1 + val2
End If
End If
val1 = 0
val2 = 0
method = ""
End Sub

HyperHacker
Posted on 04-04-07 07:51 AM Link | Quote | ID: 23168

...
Level: 73

Posts: 222/1220
EXP: 3367679
Next: 118189

Since: 03-25-07
From: no

Last post: 6094 days
Last view: 6078 days
Posted by darkwitch
Also, I haven't figured how to add a number to an integer like:
I have 'number' as "5" if I hit the 6 button it replaces it to "6" instead of changing it to '56'. I tried using += but that just gives me an error.
You have to do it the long way in VB: x = x + foo.

darkwitch
Posted on 04-04-07 02:41 PM Link | Quote | ID: 23216


Paragoomba
Level: 20

Posts: 53/65
EXP: 41519
Next: 920

Since: 02-21-07
From: Puerto Rico

Last post: 6224 days
Last view: 6224 days
In the '1' button I have:
val1 = val1 + 1

^ But that wouldn't increment val1 by one? Like if val1 = 4 it would change it to 5 instead of 41.

Sukasa
Posted on 04-04-07 04:43 PM Link | Quote | ID: 23223


Red Birdo
Level: 92

Posts: 120/2112
EXP: 7691395
Next: 65542

Since: 02-19-07

Last post: 4451 days
Last view: 3223 days
Can you post your full code here?

darkwitch
Posted on 04-04-07 05:33 PM (rev. 4 of 04-04-07 05:42 PM) Link | Quote | ID: 23227


Paragoomba
Level: 20

Posts: 54/65
EXP: 41519
Next: 920

Since: 02-21-07
From: Puerto Rico

Last post: 6224 days
Last view: 6224 days
Declarations:
Dim method As String
Dim val1 As Integer
Dim val2 As Integer
Dim instep As Integer


Private Sub equals_Click()
If instep = 0 Then
MsgBox ("You must add a second value to add or subtract!")
Else
If method = "subtract" Then
res.Text = val1 - val2
End If
If method = "Add" Then
res.Text = val1 + val2
End If
End If
val1 = 0
val2 = 0
method = ""
End Sub


Private Sub minus_Click()
instep = 1
res.Text = ""
method = "subtract"
End Sub


Private Sub num0_Click()
If instep = 0 Then
val1 = val1 + 0
res.Text = val1
Else
val2 = val2 + 0
res.Text = val2
End If
End Sub ' I dont think I need to post all number buttons since they are the same as num0


Private Sub plus_Click()
instep = 1
method = "Add"
res.Text = ""
End Sub


There it has the equals button, the plus and the minus, and the 0 button.

Here is another problem, if I hit 4 - 3 before hitting the equals, I get on the text box "7" and if hit equals I get "-7".

Sukasa
Posted on 04-04-07 05:59 PM Link | Quote | ID: 23232


Red Birdo
Level: 92

Posts: 121/2112
EXP: 7691395
Next: 65542

Since: 02-19-07

Last post: 4451 days
Last view: 3223 days
Try adding "instep = 0" to the equals_click() event.

darkwitch
Posted on 04-04-07 06:17 PM Link | Quote | ID: 23235


Paragoomba
Level: 20

Posts: 55/65
EXP: 41519
Next: 920

Since: 02-21-07
From: Puerto Rico

Last post: 6224 days
Last view: 6224 days
Cool that fixed it, now the only problem is that when I hit the "5" button and then again, instead of putting "55" it puts "10". Is the "val1 = val1 + x" thingy. But I can't figure a way to make the 'x' go after val1 instead of increasing it by "x".

Sukasa
Posted on 04-04-07 06:49 PM Link | Quote | ID: 23244


Red Birdo
Level: 92

Posts: 122/2112
EXP: 7691395
Next: 65542

Since: 02-19-07

Last post: 4451 days
Last view: 3223 days
Hm, try this: Dim Val1/2 as a string, and add "5" not 5, the same for the other numbers. Then, in equals_click, make is res.text = str(val(val1) =/- val(val2))

darkwitch
Posted on 04-04-07 07:44 PM Link | Quote | ID: 23254


Paragoomba
Level: 20

Posts: 56/65
EXP: 41519
Next: 920

Since: 02-21-07
From: Puerto Rico

Last post: 6224 days
Last view: 6224 days
Finally it's fixed, yeah that worked. Thanks a bunch!

Sukasa
Posted on 04-04-07 08:36 PM Link | Quote | ID: 23262


Red Birdo
Level: 92

Posts: 123/2112
EXP: 7691395
Next: 65542

Since: 02-19-07

Last post: 4451 days
Last view: 3223 days
Welcome

The reason I said to use strings is that val1+5 originally added five to a number, whereas using strings adds "5" to the end of the string, and you can just convert the string to a number

So, instead of 5+5 = 10, you're going "5" + "5" = "55"

Main - Computing - Visual Basic help, really basic. New thread | New reply

Acmlmboard 2.1+4δ (2023-01-15)
© 2005-2023 Acmlm, blackhole89, Xkeeper et al.

Page rendered in 0.025 seconds. (321KB of memory used)
MySQL - queries: 82, rows: 101/102, time: 0.016 seconds.