Click here to Skip to main content
15,898,010 members
Please Sign up or sign in to vote.
1.50/5 (2 votes)
See more:
Hai, i have probleam with my code, currently i am create scientific calculator which have function sin, cos & tan. The code i create it not working because every time i convert the function it will show 0 as answer.Please advice me how to correct my code here.

VB
Public Class Form1
    Dim total1, total2 As Double
    Dim [Operator] As String
    Const conversionFactor As Double = Math.PI / 180
Dim result As Double
        total2 = Val(answer.Text)

        Select Case [Operator]
             Case "SIN"
                result = Math.Sin(total1 / conversionFactor)
                answer.Text = result
            Case "COS"
                result = Math.Cos(total1 / conversionFactor)
                answer.Text = result
            Case "Tan"
                result = Math.Tan(total1 / conversionFactor)
        End Select
        answer.Text = result
    End Sub 
Posted
Updated 6-Sep-11 12:32pm
v2

If this is all of the code, the first problem seems to be that you use total1 variable, but you don't assign a value to it. Also you don't set the operator so none of the cases are hit.

Put a breakpoint to the beginning of the code and go step-by-step to see intermediate results and behaviour.
 
Share this answer
 
v2
Comments
Espen Harlinn 6-Sep-11 19:17pm    
Good reply - 5'ed
Wendelius 7-Sep-11 1:08am    
Thanks Espen :)
The usual way to handle this is to set a breakpoint at
VB
total2 = Val(answer.Text)


That will allow you to inspect what's going on as the method executes.

This might be interesting: Debugging in Visual Studio[^]

Best regards
Espen Harlinn
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 6-Sep-11 19:53pm    
This is correct, of course, my 5.
However, I don't feel much enthusiastic about this project looking at OP's samples. I tried to give some advice to make things reasonable, maybe a bit too lapidary -- please take a look at my solution.
--SA
Espen Harlinn 7-Sep-11 4:47am    
Thanks Sergey!
Case with immediate constants "SIN", "COS" etc. is the worst you could invent. If these are buttons, give each variable a semantic name and add a handler to each button individually, but share the handler code, parametrize if. A parameter could be of some enumeration type, not string:

C#
enum Operator { sin, cos, tag, /*...*/}


Make a dictionary which finds a delegate instance by each operator's value:

C#
using OperatorDictionary = System.Collections.Generic.Dictionary<Operator, OperatorApplication>;

//...

delegate double OperatorApplication(double left, double right);


The same idea works if you parse text with expression. Parse it into some logical structure, where the operator code is enumeration shown above, not string. Something like that. With your immediate constants of the string type, even with explicit string constants, you will get sunk in support issues pretty soon. Don't lock yourself.

—SA
 
Share this answer
 
v4
Comments
Espen Harlinn 6-Sep-11 19:16pm    
Good points as usual :)
Sergey Alexandrovich Kryukov 6-Sep-11 20:04pm    
Thank you, Espen.
--SA
Wendelius 7-Sep-11 1:09am    
Good advice, +5
Sergey Alexandrovich Kryukov 7-Sep-11 1:15am    
Thank you, Mika.
--SA

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