简体   繁体   中英

I need some pointers on performing a simple calculation in C#

I am moving from VB.Net to C#. I am a complete beginner in C#.

I an trying to code a simple calculation in a function. The user inputs 3 values, a calculation is performed, and returns an answer. I have no idea of where to start.

I have trolled the forums, referenced a book, etc. I am getting bogged down with attributes (extern, partial, static, etc) and other things around it.

In VB.Net I would do this:

 Function TestCalc(MilliAmps As String, PV_High As String, PV_Low As String)

    Dim MilliAmps_dbl As Double
    Dim PV_High_dbl As Double
    Dim PV_Low_dbl As Double
    Dim PV_Value_dbl As Double
    Dim PV_Value_str As String


    MilliAmps_dbl = CDbl(MilliAmps)
    PV_High_dbl = CDbl(PV_High)
    PV_Low_dbl = CDbl(PV_Low)

    PV_Value_dbl = (((MilliAmps_dbl - 4) / 16) * (PV_High_dbl - PV_Low_dbl)) + PV_Low_dbl


    PV_Value_str = CStr(PV_Value_dbl)

    Return PV_Value_str

End Function

The code takes 3 string inputs from text boxes, converts them to real values, performs the calculation and returns a string value as an answer.

Could someone please point me in the right direction, as I am clueless at the stage, of how to do it in C#.

Thanks

Here is your code sample, converted to C# with the Code Converter C#to/from VB.NET, with one minor addition (the return value is declared as string):

    public string TestCalc(string MilliAmps, string PV_High, string PV_Low)
    {
        double MilliAmps_dbl;
        double PV_High_dbl;
        double PV_Low_dbl;
        double PV_Value_dbl;
        string PV_Value_str;


        MilliAmps_dbl = System.Convert.ToDouble(MilliAmps);
        PV_High_dbl = System.Convert.ToDouble(PV_High);
        PV_Low_dbl = System.Convert.ToDouble(PV_Low);

        PV_Value_dbl = (((MilliAmps_dbl - (double)4) / (double)16) * (PV_High_dbl - PV_Low_dbl)) + PV_Low_dbl;


        PV_Value_str = System.Convert.ToString(PV_Value_dbl);

        return PV_Value_str;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM