Convert VB code to C# code

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I need help converting the VB code

Private Sub Button1_Click _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles Button1.Click
Dim cntrlTester As Control
Dim chkTester As CheckBox
Dim sb As New System.Text.StringBuilder
For Each cntrlTester In _
Page.Controls(1).Controls
If TypeOf cntrlTester Is _
System.Web.UI.WebControls.CheckBox Then
chkTester = cntrlTester
sb.Append(chkTester.ID & _
" : " & chkTester.Checked.ToString & _
"<br>")
End If
Next
Label1.Text = sb.ToString
End Su
<form id="Form1" method="post" runat="server"><p><asp:CheckBox id="CheckBox1" runat="server"
text="checkbox1"></asp:checkbox></p><p><asp:CheckBox id="CheckBox2" runat="server"
text="checkbox2"></asp:checkbox></p><p><asp:CheckBox id="CheckBox3" runat="server"
text="checkbox3"></asp:checkbox></p><p><asp:Label id="Label1" runat="server"></asp:label></p><p><asp:Button id="Button1" runat="server" text="button"></asp:button></p></form

TO C# code for Visual Studio .NE

Thanks

bebo
 
It helps a little but not much because the comparison isn't the same

In VB code it uses a foreach

Private Sub Button1_Click _ (ByVal sender As System.Object, _ ByVal e As System.EventArgs) _
Handles Button1.Click
Dim cntrlTester As Control
Dim chkTester As CheckBox
Dim sb As New System.Text.StringBuilder
For Each cntrlTester In _ Page.Controls(1).Controls //the C# equivalent uses an array with a foreach and VB code
If TypeOf cntrlTester Is _ //doesn't use an array at the link
System.Web.UI.WebControls.CheckBox Then // http://www.harding.edu/USER/fmccown/WWW
chkTester = cntrlTester /vbnet_csharp_comparison.htm
sb.Append(chkTester.ID & _
" : " & chkTester.Checked.ToString & _
"<br>")
End If
Next
Label1.Text = sb.ToString
End Sub
 
I got the following via Instant C#:

//INSTANT C# TODO TASK: Insert the following converted event handlers
at the end of the 'InitializeComponent' method for forms or into a
constructor for other classes:
//Button1.Click += new System.EventHandler(Button1_Click);

private void Button1_Click(object sender, System.EventArgs e)
{
CheckBox chkTester = null;
System.Text.StringBuilder sb = new System.Text.StringBuilder();
foreach (Control cntrlTester in Page.Controls[1].Controls)
{
if (typeof(cntrlTester) == System.Web.UI.WebControls.CheckBox)
{
chkTester = cntrlTester;
sb.Append(chkTester.ID + " : " + chkTester.Checked.ToString() +
"<br>");
}
}
Label1.Text = sb.ToString();
}
 
I tried your code in Visual Studio C# .NET

got errors:

The type or namespace name 'cntrlTester' could not be found(are you
missing a using directive or an assembly reference?)

and

Cannot implicitly convert type 'System.Web.UI.Control' to
'System.Web.UI.WebControls.CheckBox'

Did you get the same errors when you tried the code?

bebop
 
Here is the sanitized code: (Instant C# is fixing the conversion of
VB's TypeOf for the next release)

//INSTANT C# TODO TASK: Insert the following converted event handlers
at the end of the 'InitializeComponent' method for forms or into a
constructor for other classes:
//Button1.Click += new System.EventHandler(Button1_Click);

private void Button1_Click(object sender, System.EventArgs e)
{
CheckBox chkTester = null;
System.Text.StringBuilder sb = new System.Text.StringBuilder();
foreach (Control cntrlTester in Page.Controls[1].Controls)
{
if (cntrlTester is System.Web.UI.WebControls.CheckBox)
{
chkTester = (CheckBox)cntrlTester;
sb.Append(chkTester.ID + " : " + chkTester.Checked.ToString() +
"<br>");
}
}
Label1.Text = sb.ToString();
}
 
I got it working changed some lines so that the CheckBox value is
displayed in the Label.

Thanks.

bebop
 
Back
Top