locked
Convert C# to VB RRS feed

  • Question

  • User559005402 posted

    Hi there, I need some help converting this code to VB.

     List<MenuItem> tabItems;
     
     private void spMenu_Loaded(object sender, RoutedEventArgs e)
     {
      XElement xml = XElement.Load("Menu.xml");
      var listItems = from p in xml.Elements("Item")
                      select new MenuItem
                      {
                       Text = (string)p.Attribute("Text").Value,
                       Id = (int)p.Attribute("Id"),
                       Help = (string)p.Attribute("Help"),
                       Selected=p.Attribute("Selected")!=null ? true : false
                      };
      tabItems = listItems.ToList();
      
     }

    The code reads an XML file into a list. My conversion is losing the attributes.

    Private tabItems As List(Of MenuItem)
    
    Private Sub spMenu_Loaded(ByVal sender As Object, ByVal e As RoutedEventArgs)
        Dim xml As XElement = XElement.Load("Menu.xml")
        Dim listItems = From p In xml.Elements("Item") _ 
            Select New MenuItem()
            
        tabItems = listItems.ToList()
    End Sub


    Thanks for any help.
     

    Tuesday, April 13, 2010 7:00 PM

Answers

  • User397347636 posted
    Imports Microsoft.VisualBasic
    
    'INSTANT VB NOTE: This code snippet uses implicit typing. You will need to set 'Option Infer On' in the VB file or set 'Option Infer' at the project level.
    
     Private tabItems As List(Of MenuItem)
    
     Private Sub spMenu_Loaded(ByVal sender As Object, ByVal e As RoutedEventArgs)
      Dim xml As XElement = XElement.Load("Menu.xml")
      Dim listItems = From p In xml.Elements("Item") _
                      Select New MenuItem With {.Text = CStr(p.Attribute("Text").Value), .Id = CInt(Fix(p.Attribute("Id"))), .Help = CStr(p.Attribute("Help")), .Selected=If(p.Attribute("Selected") IsNot Nothing, True, False)}
      tabItems = listItems.ToList()
    
     End Sub


     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, April 14, 2010 9:49 AM

All replies

  • User-1460196090 posted

    The few lines that you haven't converted in VB.NET would be:

                       Text = (String)p.Attribute("Text").Value,
                       Id = (Integer)p.Attribute("Id"),
                       Help = (String)p.Attribute("Help"),
                       Selected=p.Attribute("Selected")<> IIf(Nothing ,  True ,  False)


    I havent tested it yet, but try it...

    Regards,
    Hajan

    Tuesday, April 13, 2010 7:57 PM
  • User2130758966 posted

    Yeah it seems to do that for me as well. This thread shows the vb.net syntax (I am not a vb coder so the below is just a guess)

    Private tabItems As List(Of MenuItem)
    
    Private Sub spMenu_Loaded(ByVal sender As Object, ByVal e As RoutedEventArgs)
        Dim xml As XElement = XElement.Load("Menu.xml")
        Dim listItems = From p In xml.Elements("Item") _
            Select New MenuItem With _
              { _
                .Text = p.Attribute("Text").Value, _
                .Id = p.Attribute("Id"), _
                .Help = p.Attribute("Help") _
             }
    
        tabItems = listItems.ToList()
    End Sub

    Well its late and I should be in bed and I didnt take the time to look up the ternary operator for your last attribute but I think you get the idea...

    oh and I think the vb equiv is iif

    Tuesday, April 13, 2010 8:00 PM
  • Tuesday, April 13, 2010 8:01 PM
  • User-952121411 posted

    The ternary operator in VB.NET  is just 'If' in your case, and the code should be able to be used like shown below:

            Dim xml As XElement = XElement.Load("Menu.xml")
            Dim listItems = From p In xml.Elements("Item") _
                Select New MenuItem With { _
                      .Text = p.Attribute("Text").Value, _
                      .Id = p.Attribute("Id"), _
                      .Help = p.Attribute("Help"), _
                      .Selected = If(Not IsNothing(p.Attribute("Selected")), True, False)}
    
            tabItems = listItems.ToList()


    The only issue I have with that code is that the values for '.Id' and '.Help' do not exist on a type of System.Web.UI.WebControls.MenuItem so the compiler is not liking my code.  It makes sense, but since I do not have the complete code and that might be a custom menu control with those properties exposed, then the code above might work in your application.  The important line above was for defining the .Selected property.

    If it reads easier to you, the equivilent line of what I have above written in a different way is below:

    .Selected = If(p.Attribute("Selected") IsNot Nothing, True, False)


    Lastly, if you are using LINQ to XML, it is always good to make sure an attribute, element, etc has value before assignment as done in the line above.  That could be applied to any of your lines; for example look at the modified line for the "Text" value:

    .Text = If(p.Attribute("Text").Value IsNot Nothing, p.Attribute("Text").Value, String.Empty),

     

    Hope this helps! Smile

     

    Wednesday, April 14, 2010 9:45 AM
  • User397347636 posted
    Imports Microsoft.VisualBasic
    
    'INSTANT VB NOTE: This code snippet uses implicit typing. You will need to set 'Option Infer On' in the VB file or set 'Option Infer' at the project level.
    
     Private tabItems As List(Of MenuItem)
    
     Private Sub spMenu_Loaded(ByVal sender As Object, ByVal e As RoutedEventArgs)
      Dim xml As XElement = XElement.Load("Menu.xml")
      Dim listItems = From p In xml.Elements("Item") _
                      Select New MenuItem With {.Text = CStr(p.Attribute("Text").Value), .Id = CInt(Fix(p.Attribute("Id"))), .Help = CStr(p.Attribute("Help")), .Selected=If(p.Attribute("Selected") IsNot Nothing, True, False)}
      tabItems = listItems.ToList()
    
     End Sub


     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, April 14, 2010 9:49 AM
  • User-2080168305 posted

    hi,

    u can do that using online convertor like below.

    http://www.developerfusion.com/tools/convert/vb-to-csharp/

    http://www.developerfusion.com/tools/convert/csharp-to-vb/


    I hope it will help u,


    Wednesday, April 14, 2010 10:19 AM
  • User397347636 posted

    Actually you can't - hence the original post.

     

    Wednesday, April 14, 2010 10:30 AM
  • User559005402 posted

    Thanks David. I just had to drop the Fix function to make it work. I am not clear what your comment about implicit typing means. I didn't do anything special and it seems to work OK. Thanks again. This was very unusual for me.

     

    Wednesday, April 14, 2010 11:26 AM
  • User397347636 posted

    The 'Fix' call is added since integer casts work differently in C# and VB - C# always truncates, while VB 'CInt' rounds, so the 'Fix' is often necessary, unless the converter can reliably determine that the target of the cast is some other integer type (short, long, etc.).

    Regarding the implicit typing comment, for VB9 and above a local 'Dim' without a specified type will result in VB inferring the correct type if an initializer is included - but this requires 'Option Infer On' at the file level or 'Option Infer' set at the project level.  If this is not done, the old VB behavior of assuming the 'Object' type will be used - and this is often not very desirable.

     

    Wednesday, April 14, 2010 12:00 PM