Click here to Skip to main content
16,011,784 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
my code is
VB
 Num1 = Val(TextBox1.Text)
        TextBox1.Clear()
        Arithmetic = "+"
'btnEqual click is
 Num2 = Val(TextBox1.Text)
        If Arithmetic = "+" Then
            Answer = Num1 + Num2
textbox1.text=Answer
Posted
Updated 16-Dec-13 20:21pm
v3

Hi,

Try this

Answer = val(Num1 + Num2);

By default string concatenation is happening in your case.So the above code might work for you.
Hope this helps you a bit.

Regards,
RK
 
Share this answer
 
v2
Comments
CHM021 16-Dec-13 6:06am    
Thanks! It works for me.
♥…ЯҠ…♥ 16-Dec-13 6:09am    
Cheers... Next time try to give more input about your question otherwise you may get delay to solve your issue. Upvote if it deserves.
OriginalGriff 16-Dec-13 6:10am    
*Cough* VB *Cough*
Since you are tagging this as ASP.NET, I will assume this is website code.
In which case, no it won't work like that. Variable values are not preserved between runs, they are destroyed when the page load (or postback processing) is completed.

If you want to preserve a value, you need to look at either storing the total in a Cookie (on the client) or the Session (on the server) and retrieving it before you start working.
Cookies[^]

Save in Session[^]
Read from Session[^]

BTW: You do realise that doing all your math operations in the codebehind will create a rather slow calculator, don't you?
 
Share this answer
 
Comments
CHM021 16-Dec-13 5:59am    
I don't understand it clearly.When I run the program all operations work properly except + operation. It gives wrong answer ,for instance 7+7=77.
♥…ЯҠ…♥ 16-Dec-13 6:07am    
you should have mention this in your question itself, you should not say code is not working.Every Code will work perfectly if and only if code logic is correct.
Every code is perfect until it satisfies requirement.
OriginalGriff 16-Dec-13 6:09am    
Ah!
So, you need to look at your definition of Num1 and Num2...
I'm guessing, but did you declare them as strings?
Dim Num1 As String
Dim Num2 As String
If so, then that is exactly what I would expect to happen...
Declare them as integers and it should work fine:
Dim Num1 As Integer
Dim Num2 As Integer
just
Get a two variable or text box
For eg:-
initialize values
Z=x+y
or

Res=Val(txt1.text)+Val(txt2.text)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900