locked
For Each var in VB.NET RRS feed

  • Question

  • User-850127045 posted

    How do you do this c# statement in vb.net?

    foreach (var jo in pages) {

    You can't do this because there is no var and assuming you don't know if you have a string and the data type will be determined at runtime, what is the equivalency for "determine data type at run time" in vb.net

            For Each jo As var In pages

    Will this work?

            For Each jo As Object In FBpages

    Thanks

    Friday, November 8, 2013 12:35 PM

Answers

  • User-1225287840 posted

    You could use,

    For Each jo As Object In pages
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, November 8, 2013 12:40 PM
  • User281315223 posted

    You should be able to use the following syntax, however this will provide you with a very generic object within the scope of your For Each loop : 

    For Each jo As Object In pages
        'Your code here'
    Next

    Object is about as close as you will get to a very generic object, however if you know what type of collection pages is, then you should have an idea of what type you can use. Otherwise you'll need to cast it as the type that you are expecting : 

    For Each jo As Object In pages
        'Your code here'
        Dim example As String = CType(jo,String)
    Next

    For instance if pages was a collection of FBPage objects, you might want to strongly type your jo iterator values as "FBPages" : 

    'Example collection of FBPage objects'
    Dim FBPages As List(Of FBPage) = GetYourFBPagesHere()
    
    'Iterate through each of the FBPage objects'
    For Each Dim jo As FBPage In FBPages
        'Your Logic here'
    Next

    Any additional information details that you have about your FBPages or pages collection may be helpful to provide you with a more detailed / specific answer.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, November 8, 2013 12:48 PM

All replies

  • User-1225287840 posted

    You could use,

    For Each jo As Object In pages
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, November 8, 2013 12:40 PM
  • User281315223 posted

    You should be able to use the following syntax, however this will provide you with a very generic object within the scope of your For Each loop : 

    For Each jo As Object In pages
        'Your code here'
    Next

    Object is about as close as you will get to a very generic object, however if you know what type of collection pages is, then you should have an idea of what type you can use. Otherwise you'll need to cast it as the type that you are expecting : 

    For Each jo As Object In pages
        'Your code here'
        Dim example As String = CType(jo,String)
    Next

    For instance if pages was a collection of FBPage objects, you might want to strongly type your jo iterator values as "FBPages" : 

    'Example collection of FBPage objects'
    Dim FBPages As List(Of FBPage) = GetYourFBPagesHere()
    
    'Iterate through each of the FBPage objects'
    For Each Dim jo As FBPage In FBPages
        'Your Logic here'
    Next

    Any additional information details that you have about your FBPages or pages collection may be helpful to provide you with a more detailed / specific answer.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, November 8, 2013 12:48 PM
  • User397347636 posted
    Option Infer On
    
    For Each jo In pages
    Next jo

    This will cause VB to infer the type of 'jo' - much better than using 'Object'.

    Friday, November 8, 2013 5:11 PM