locked
Translate a ternary operator into VB.net RRS feed

  • Question

  • User865464427 posted

    I'm usually reasonably good at translating C# into VB, but I'm a bit stuck partially due to the ternary operator in this function and also because I don't fully understand what is happening with the "from x in dataSource.OfType<object>() select 1).Sum()" bitCan anyone help me translate this? I am using VS 2008 so I know that there is a VB ternary operation available to me.

    //Gets the row count from a manually bound source or from a source in viewstate 
    private int _GetSourceCount(IEnumerable dataSource)
    {
        ICollection source = dataSource as ICollection;
    
        return source != null ?
             source.Count :
             (from x in dataSource.OfType<object>() select 1).Sum();
    }
    


     

    Wednesday, February 24, 2010 7:47 AM

Answers

  • User-952121411 posted

    Try this (btw IF is the ternary operator for VB.NET in VS.NET 2008):

    Private Function _GetSourceCount(dataSource As IEnumerable) As Integer
    	Dim source As ICollection = TryCast(dataSource, ICollection)
    
    	Return If(source IsNot Nothing, source.Count, (From x In dataSource.OfType(Of Object)() _
    		Select 1).Sum())
    End Function


     

    For future reference check out this link:

    Tools for Converting C# code to VB.NET (or vice versa) and a little => ... Function(), Lambdas too:

    http://allen-conway-dotnet.blogspot.com/2009/11/tools-for-converting-c-code-to-vbnet-or.html

    Hope this helps! Smile

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, February 24, 2010 9:40 AM
  • User397347636 posted

    You don't need the null check for VB - "RaiseEvent" takes care of that internally.

    If you ever really do need to make this check, you can use "If TotalRowCountAvailableEvent Is Nothing" - note the reference to the hidden event variable - but you shouldn't need to do this.

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, February 24, 2010 11:44 AM

All replies

  • User-952121411 posted

    Try this (btw IF is the ternary operator for VB.NET in VS.NET 2008):

    Private Function _GetSourceCount(dataSource As IEnumerable) As Integer
    	Dim source As ICollection = TryCast(dataSource, ICollection)
    
    	Return If(source IsNot Nothing, source.Count, (From x In dataSource.OfType(Of Object)() _
    		Select 1).Sum())
    End Function


     

    For future reference check out this link:

    Tools for Converting C# code to VB.NET (or vice versa) and a little => ... Function(), Lambdas too:

    http://allen-conway-dotnet.blogspot.com/2009/11/tools-for-converting-c-code-to-vbnet-or.html

    Hope this helps! Smile

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, February 24, 2010 9:40 AM
  • User865464427 posted

    Thanks for that, I knew that If() was the right function, but as I say I didn't quite understand what was going on with the "From x...." bit to know that I was formatting it correctly.

    Thanks for the links, the C# to VB converter I used to use couldn't cope with newer and more complex translations.

    Wednesday, February 24, 2010 9:56 AM
  • User-379429695 posted

    I'm probably reading the return statement incorrectly, but in the hope of preventing future problems or even clarifying for future readers, it would seem that while returning the Sum of column 1, you would not get the row count but instead a sum of the column.  Wouldnt you instead want to do Select 1).Count()  ? 

  • (From x In dataSource.OfType(Of Object)() _   
  •         Select 1).Sum()

 

Wednesday, February 24, 2010 10:19 AM
  • User397347636 posted

    Sorry to nitpick, but you'll need "source IsNot Nothing" instead of  "source <> Nothing".

    (the online converters really miss a lot of these subtleties).

    Wednesday, February 24, 2010 10:41 AM
  • User865464427 posted

    Yes, I'd spotted that, thanks.

    I'm still having a bit of trouble with the following, because VB needs me to call RaiseEvent and I'm not how to work that into the  "!= null" condition. If I raise the event, is it even possible for it to return Nothing? Another one of those things an auto translator won't pick up, hence why I don't normally use them.

    public event EventHandler<PageEventArgs> TotalRowCountAvailable; 
    
    protected virtual void OnTotalRowCountAvailable(PageEventArgs e) 
         { 
              if (TotalRowCountAvailable != null) 
                    TotalRowCountAvailable(this, e); 
         }
    


    Thanks for everyone's help so far!  

    Wednesday, February 24, 2010 11:07 AM
  • User-952121411 posted

    Sorry to nitpick, but you'll need "source IsNot Nothing" instead of  "source <> Nothing".
     

    Good catch thank you.  I updated the code in my reply.

    Wednesday, February 24, 2010 11:15 AM
  • User397347636 posted

    You don't need the null check for VB - "RaiseEvent" takes care of that internally.

    If you ever really do need to make this check, you can use "If TotalRowCountAvailableEvent Is Nothing" - note the reference to the hidden event variable - but you shouldn't need to do this.

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, February 24, 2010 11:44 AM