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:

Sheepings

Senior Programmer
Staff member
Joined
Sep 5, 2018
Messages
1,518
Location
UK
Programming Experience
10+
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.
 
Joined
Sep 14, 2020
Messages
11
Programming Experience
5-10
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

Sheepings

Senior Programmer
Staff member
Joined
Sep 5, 2018
Messages
1,518
Location
UK
Programming Experience
10+
Post your code in code tags and not as a screenshot. We can not copy the code from a screenshot.
 
Joined
Sep 14, 2020
Messages
11
Programming Experience
5-10
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:

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
1,788
Location
Chesapeake, VA
Programming Experience
10+
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.
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,140
Location
Sydney, Australia
Programming Experience
10+
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
 

Sheepings

Senior Programmer
Staff member
Joined
Sep 5, 2018
Messages
1,518
Location
UK
Programming Experience
10+
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.
 
Top Bottom