Conversion from C# to VB.NET
-
Hi, I have tried to convert an C# sample program to VB.Net but was confronted with one problem. This sample describes adding controls dynamically to a placeholder and reading back their values after user clicks a button. You can see the whole C# source here. I think I have managed to do all the other conversion but following snippet of C# code is problem. :confused: (I have removed the comments)
XPathDocument surveyDoc = new XPathDocument(Server.MapPath("ExSurvey.xml"));
XPathNodeIterator itr = surveyDoc.CreateNavigator().Select("//question");
System.Text.StringBuilder sb;
sb = new System.Text.StringBuilder();
sb.Append("Survey submitted on " + DateTime.Now + Environment.NewLine);
while (itr.MoveNext()) {
string controlName = itr.Current.GetAttribute("name", "");
sb.Append(controlName);
sb.Append(" : ");
object ctrl = FindControl(controlName);
if (ctrl is TextBox) {
sb.Append(((TextBox)ctrl).Text);
}
if (ctrl is RadioButtonList) {
if (((RadioButtonList)ctrl).SelectedItem != null) {
sb.Append(((RadioButtonList)ctrl).SelectedItem.Value);
}
}
sb.Append(Environment.NewLine);
}
string body = sb.ToString();This code returns the ID of the control and it's value in
body
. That's what I'm after. My effort in translating this snippet was as follows:Dim surveyDoc As XPathDocument = New XPathDocument(Server.MapPath("ExSurvey.xml"))
Dim itr As XPathNodeIterator = surveyDoc.CreateNavigator().Select("//question")
Dim sb As System.Text.StringBuilder
sb = New System.Text.StringBuilder()
sb.Append("Survey submitted on " + DateTime.Now + "<br>")
While (itr.MoveNext())
Dim controlName As String = itr.Current.GetAttribute("name", "")
sb.Append(controlName)
sb.Append(" : ")
Dim ctrl As Object = FindControl(controlName)
If (TypeOf ctrl Is TextBox) Then
Dim myTextBox As New TextBox()
myTextBox = ctrl
sb.Append(myTextBox.Text + "<br>")
End If
If (TypeOf ctrl Is RadioButtonList) Then
Dim myRadioButtonList As New TextBox()
myRadioButtonList = ctrl
If (Not myRadioButtonList.SelectedItem Is Nothing) Then
sb.Append(myRadioButtonList.SelectedItem.Value.ToString + "<br>")
End If
End If
sb.Append("<br>")
End While
Dim body As String = sb.ToString()To my disappointment my
body
contains only the ID and the semic -
Hi, I have tried to convert an C# sample program to VB.Net but was confronted with one problem. This sample describes adding controls dynamically to a placeholder and reading back their values after user clicks a button. You can see the whole C# source here. I think I have managed to do all the other conversion but following snippet of C# code is problem. :confused: (I have removed the comments)
XPathDocument surveyDoc = new XPathDocument(Server.MapPath("ExSurvey.xml"));
XPathNodeIterator itr = surveyDoc.CreateNavigator().Select("//question");
System.Text.StringBuilder sb;
sb = new System.Text.StringBuilder();
sb.Append("Survey submitted on " + DateTime.Now + Environment.NewLine);
while (itr.MoveNext()) {
string controlName = itr.Current.GetAttribute("name", "");
sb.Append(controlName);
sb.Append(" : ");
object ctrl = FindControl(controlName);
if (ctrl is TextBox) {
sb.Append(((TextBox)ctrl).Text);
}
if (ctrl is RadioButtonList) {
if (((RadioButtonList)ctrl).SelectedItem != null) {
sb.Append(((RadioButtonList)ctrl).SelectedItem.Value);
}
}
sb.Append(Environment.NewLine);
}
string body = sb.ToString();This code returns the ID of the control and it's value in
body
. That's what I'm after. My effort in translating this snippet was as follows:Dim surveyDoc As XPathDocument = New XPathDocument(Server.MapPath("ExSurvey.xml"))
Dim itr As XPathNodeIterator = surveyDoc.CreateNavigator().Select("//question")
Dim sb As System.Text.StringBuilder
sb = New System.Text.StringBuilder()
sb.Append("Survey submitted on " + DateTime.Now + "<br>")
While (itr.MoveNext())
Dim controlName As String = itr.Current.GetAttribute("name", "")
sb.Append(controlName)
sb.Append(" : ")
Dim ctrl As Object = FindControl(controlName)
If (TypeOf ctrl Is TextBox) Then
Dim myTextBox As New TextBox()
myTextBox = ctrl
sb.Append(myTextBox.Text + "<br>")
End If
If (TypeOf ctrl Is RadioButtonList) Then
Dim myRadioButtonList As New TextBox()
myRadioButtonList = ctrl
If (Not myRadioButtonList.SelectedItem Is Nothing) Then
sb.Append(myRadioButtonList.SelectedItem.Value.ToString + "<br>")
End If
End If
sb.Append("<br>")
End While
Dim body As String = sb.ToString()To my disappointment my
body
contains only the ID and the semicIt looks like where you are creating a new TextBox() which does not appear to be needed. If TypeOf ctrl Is TextBox Then sb.Append(CType(ctrl, TextBox).Text) End If If TypeOf ctrl Is RadioButtonList Then If Not (CType(ctrl, RadioButtonList).SelectedItem Is Nothing) Then sb.Append(CType(ctrl, RadioButtonList).SelectedItem.Value) End If End If This should work