Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Conversion from C# to VB.NET

Conversion from C# to VB.NET

Scheduled Pinned Locked Moved Visual Basic
csharpquestioncomsysadmin
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    lasseran
    wrote on last edited by
    #1

    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

    T 1 Reply Last reply
    0
    • L lasseran

      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

      T Offline
      T Offline
      The Limey
      wrote on last edited by
      #2

      It 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

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups