Answered How to convert this Visual Basic Code to C#

Joined
Sep 14, 2020
Messages
11
Programming Experience
5-10
Hi group,

I would like to write similar code from Visual Basic to C#, this is the code from Visual Basic:

C#:
For i = 1 To 14
                If GroupBox6.Controls("CmbR" & CInt(i)).Text = GroupBox1.Controls("txbCom11" & CInt(i)).Text Then
                    contador_aciertos_1 = contador_aciertos_1 + 1
                    GroupBox1.Controls("txbCom11" & CInt(i)).BackColor = Color.Sienna
                Else
                    GroupBox1.Controls("txbCom11" & CInt(i)).BackColor = Color.NavajoWhite
                End If
Next

Any advice how to translate this to C# will be appreciated.

Kind regards,

Francisco Mtz.
 
Last edited by a moderator:
There is a link to a program in my signature which will convert valid VB.Net syntax for you.
See the Converting VB.Net To C# link under my spoiler.
 
There is a link to a program in my signature which will convert valid VB.Net syntax for you.
See the Converting VB.Net To C# link under my spoiler.

Thanks a lot.

I have converted Visual Basic to C# code, but there is still an error:

1600122144860.png


I will really appreciate your help on this.

Kind regards,

Francisco Mtz.
 

Attachments

  • 1600122116541.png
    1600122116541.png
    264.4 KB · Views: 22
Post your code in code tags and not as a screenshot. We can not copy the code from a screenshot.

Thank you very much for your quick replies.

Sorry to paste my code as screenshot.

This is the C# code that I will try:
C#:
for (int i = 1; i <= 9; i++)
{
                            if (GroupBox6.Controls["CmbR" + Convert.ToInt32(i)].Text == GroupBox1.Controls["txbCom11" + Convert.ToInt32(i)].Text)
                            {
                                contador_aciertos_1 = contador_aciertos_1 + 1;
                                GroupBox1.Controls["txbCom11" + Convert.ToInt32(i)].BackColor = Color.Sienna;
                            }
                            else
                            {
                                GroupBox1.Controls["txbCom11" + Convert.ToInt32(i)].BackColor = Color.NavajoWhite;
                            }
}
I really appreciate your help.

Kind regards,

Francisco Mtz.
 
Last edited by a moderator:
Welcome to the forums. In the future, please put your code in the code tags.

As quick aside, in your code in post #5, i is already an int. int in C# maps to the .NET type Int32. So there is no need to call Convert.ToInt32(i) because it is already the correct type.
 
As quick aside, in your code in post #5, i is already an int. int in C# maps to the .NET type Int32. So there is no need to call Convert.ToInt32(i) because it is already the correct type.
The converter has included that because it is the closest C# equivalent to the CInt that was used in the VB code but the same advice applies, i.e. there was no point using CInt in the VB code when i is already type Integer. If Option Infer was On in VB then that's what type it would be. If Option Infer was Off then that code should have included an explicit type declaration:
Visual Basic:
For i As Integer = 1 To 14
 
converter has included that because it is the closest C# equivalent to the CInt that was used in the VB code but the same advice applies,
That's why I said further up :
There is a link to a program in my signature which will convert valid VB.Net syntax for you.

Cleaning up some of the clutter and unnecessary castings or anything else needing correction before conversion will certainly perform a better conversion. The converter he used is the one you recommended, and I've since linked it in my signature because it does a reasonable job. It will do a better job if he cleans up the original source first.
 

Latest posts

Back
Top Bottom