# Need Some Pointers on Performing a Simple Calculation Using a Function in C#

Status
Not open for further replies.

#### Slabs1960

##### Member
I am moving from WinForms & VB.Net to WPF & C#. My question is related to C#.

I have no idea of how to begin. I have trolled several forums for an idea of how to go about it, and get more confused as I go a long. The Function takes 3 string inputs from text boxes, converts them to real values, performs a calculation and returns a string value as an answer.

in VB.Net I would do the following:
VB.NET:
``````  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``````

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

#### jmcilhinney

##### VB.NET Forum Moderator
Staff member
Firstly, if you really have trolled several forums then it's no wonder you're getting nowhere. Trolling is arguing a position you don't really hold to get a reaction out of someone. Hopefully, you actually meant "trawled".

As for the question, the equivalent C# code will be almost exactly the same. There's no change in logic; just a change in syntax. If you understand C# syntax then it's a no-brainer. If you don't understand C# syntax then you probably need to put this project aside for now and learn it.

The first point to address is that you have declared a function there with no return type. That's bad enough in VB but is not allowed at all in C#. You should have `Option Strict On` in VB and then it won;t be allowed there either. This:
VB.NET:
``Function TestCalc(MilliAmps As String, PV_High As String, PV_Low As String)``
should be this:
VB.NET:
``Function TestCalc(MilliAmps As String, PV_High As String, PV_Low As String) As String``
Having made that change, I just pasted your code into a C# project and then made the appropriate syntax adjustments. This is the result:
C#:
``````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 = Convert.ToDouble(MilliAmps);
PV_High_dbl = Convert.ToDouble(PV_High);
PV_Low_dbl = Convert.ToDouble(PV_Low);

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

PV_Value_str = PV_Value_dbl.ToString();

return PV_Value_str;
}``````
Like I said, no difference in the logic at all. It's mostly just moving types in front of variables, adding semicolons and changing VB-specific conversions to standard .NET conversions. On the subject of the latter, `Convert.ToDouble` and `ToString` would both work just as well in VB.

For future conversion needs, you should download and install this. It is way better than any online converter and the free edition is enough for most people. They have one for going the other way too.

Also, if what you're trying to do is write C# code then it is more of a C# question than a VB question. If you have similar questions in future, you should post them at our sister C# forum instead. There's a link to it at the top of the page.

#### Slabs1960

##### Member
Thanks...

The syntax is what I an trying to learn. It a big mind shift for me, as I have coding in VB.Net for a long time.

Thanks for the link.

#### JuggaloBrotha

##### VB.NET Forum Moderator
Staff member
Thanks...

The syntax is what I an trying to learn. It a big mind shift for me, as I have coding in VB.Net for a long time.

Thanks for the link.
Given that pretty much anything you can do in C# you can also do in vb.net and both compile to the same intermediate language anyways, what's the reason(s) for migrating to C#?

Status
Not open for further replies.

Replies
2
Views
1K
Replies
0
Views
3K
Replies
1
Views
1K
Replies
3
Views
2K
Replies
9
Views
3K