PC Review


Reply
Thread Tools Rate Thread

Convert VB code to C# code

 
 
=?Utf-8?B?YmVib3Ax?=
Guest
Posts: n/a
 
      20th Feb 2004
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


 
Reply With Quote
 
 
 
 
Cor
Guest
Posts: n/a
 
      20th Feb 2004
Hi bebop,

This must be so easy, here some links to get the basic differences.
The HTML part stays the same.

Code C# and VB
http://www.harding.edu/USER/fmccown/...omparison.html

Language compare
http://msdn.microsoft.com/library/en...quivalents.asp

I hope this helps a little bit?

Cor
>
> 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 Sub
> <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 .NET
>
> Thanks.
>
> bebop
>
>



 
Reply With Quote
 
=?Utf-8?B?YmVib3A=?=
Guest
Posts: n/a
 
      20th Feb 2004
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
 
Reply With Quote
 
Doknjas
Guest
Posts: n/a
 
      26th Feb 2004
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();
}


Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
 
Reply With Quote
 
cw bebop
Guest
Posts: n/a
 
      29th Feb 2004
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






*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
 
Reply With Quote
 
Doknjas
Guest
Posts: n/a
 
      1st Mar 2004
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();
}


Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
 
Reply With Quote
 
cw bebop
Guest
Posts: n/a
 
      1st Mar 2004
I got it working changed some lines so that the CheckBox value is
displayed in the Label.

Thanks.

bebop



*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Linq to XML--Are there code examples that make Linq as easy as SQL? Or how can I convert ths simple pseudo code into real code? Reece Microsoft C# .NET 4 10th Dec 2008 03:13 AM
Excel code convert to Access code - Concat & eliminate duplicates italia Microsoft Access Queries 1 12th Sep 2006 07:22 PM
Excel code convert to Access code - Concat & eliminate duplicates italia Microsoft Access 2 12th Sep 2006 06:13 PM
Excel code convert to Access code - Concat & eliminate duplicates italia Microsoft Access Macros 0 12th Sep 2006 05:04 PM
Excel code convert to Access code - Concat & eliminate duplicates italia Microsoft Excel Programming 1 12th Sep 2006 12:14 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 11:23 AM.