Click here to Skip to main content
16,017,069 members
Please Sign up or sign in to vote.
1.00/5 (5 votes)
See more:
VB
Dim slop1, intercept1 As Double



ElseIf RawADC >= Val(TextBoxRawVal2.Text) And RawADC < Val(TextBoxRawVal3.Text) Then
slop1 = (Val(TextBoxRealWeight3.Text) - Val(TextBoxRealWeight2.Text)) / (Val(TextBoxRawVal3.Text) - Val(TextBoxRawVal2.Text))
intercept1 = Val(TextBoxRealWeight3.Text) - slop1 * Val(TextBoxRawVal3.Text)


What I have tried:

How to write this code in C# Thanks
Posted
Comments
CHill60 4 days ago    
As I explained earlier - show what you have tried.
Richard MacCutchan 4 days ago    
You cannot start a block with ElseIf.

This is the kind of question that GenAI is very good at.
Put these prompt into ChatGPT (for example) and the answer will explain it to you in simple terms (and will show you why your code is incorrect as Richard said). You will then be able to do the conversion yourself.

Make a comparison of vb and c# syntax for "if then else". What are the kinds of errors that a junior developer might make when converting this kind of code between vb and c#

In C# on Windows what property of a TextBox retrieves its value and how do I get its numeric value? What errors may be produced and how can I manage them?

Also your question will get down-voted for several reasons, you haven't shown any code that you have tried to write yourself (no effort), you don't explain what aspect of it that you are having difficulty with (suggesting you just want others to do the work for you)
 
Share this answer
 
This VB code is not properly organized. I make some assumption to complete it.
1) In absence if we found an elseif. I assumed it as if.
2) The Data Type and Value of RawADC is not defined. I defined it as double and assigned 100.

In c# text to numeric conversion is not that much simple, you have to define it as double, decimal, and integer etc. In this case, I defined it as double. You are collecting values from text boxes, there are issues to handle blank and non-numeric values. I did not go for that.

Finally, the converted code will be like it

C#
{
    double slop1, intercept1;
    double RawADC = 100;
    if (RawADC >= Convert.ToDouble(TextBoxRawVal2.Text) & RawADC < Convert.ToDouble(TextBoxRawVal3.Text))
    {
        slop1 = (Convert.ToDouble(TextBoxRealWeight3.Text) - Convert.ToDouble(TextBoxRealWeight2.Text)) / (Convert.ToDouble(TextBoxRawVal3.Text) - Convert.ToDouble(TextBoxRawVal2.Text));
        intercept1 = Convert.ToDouble(TextBoxRealWeight3.Text) - slop1 * Convert.ToDouble(TextBoxRawVal3.Text);
        txtSlop1.Text = slop1.ToString();
        txtIntercept1.Text = intercept1.ToString();
    }
}

I implemented this code on .Net 6.
 
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