C# to VB.net conversion for "?" conditionals

Discussion in 'ASP .Net' started by .Net Sports, Jan 26, 2006.

  1. .Net Sports

    .Net Sports Guest

    I need to convert some C# ?Conditionals over to vb.net (most likely in
    vb.net using If..Then statements), but the C#2VBNETconverter by
    KamalPatel isn't converting them:

    string PurchaseType = (Convert.ToString(drwData["DailyItem"]) ==
    "True") ? "ID" : "PID";

    XmlNodeList Selections = xdcData.GetElementsByTagName((X == 0) ?
    "Highlight" : "Selection");

    TIA
    NetSports
     
    .Net Sports, Jan 26, 2006
    #1
    1. Advertising

  2. .Net Sports

    Peter Macej Guest

    Peter Macej, Jan 26, 2006
    #2
    1. Advertising

  3. .Net Sports

    Fao, Sean Guest

    ..Net Sports wrote:
    > I need to convert some C# ?Conditionals over to vb.net (most likely in
    > vb.net using If..Then statements), but the C#2VBNETconverter by
    > KamalPatel isn't converting them:
    >
    > string PurchaseType = (Convert.ToString(drwData["DailyItem"]) ==
    > "True") ? "ID" : "PID";
    >
    > XmlNodeList Selections = xdcData.GetElementsByTagName((X == 0) ?
    > "Highlight" : "Selection");


    It has been a very long time since I've done any VB programming so
    please check my work. But that should convert to something like this.

    dim PurchaseType As String
    dim Selections As XmlNodeList

    If drwData["DailyItem"].ToString() = "True" Then
    PurchaseType = "ID"
    Else
    PurchaseType = "PID"
    End If



    If X = 0 Then
    Selections = xdcData.GetElementsByTagName("Highlight")
    Else
    Selections = xdcData.GetElementsByTagName("Selection")
    End If

    Hope that helps,

    --
    Sean
     
    Fao, Sean, Jan 26, 2006
    #3
  4. I would recommend Sean's solution as well.

    if you really want to single-line it, you can always use the iif function:
    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vblr7/html/vafctiif.asp

    Karl

    --
    MY ASP.Net tutorials
    http://www.openmymind.net/


    "Fao, Sean" <-WANT-NO-SPAM> wrote in message
    news:ODJq$...
    > .Net Sports wrote:
    >> I need to convert some C# ?Conditionals over to vb.net (most likely in
    >> vb.net using If..Then statements), but the C#2VBNETconverter by
    >> KamalPatel isn't converting them:
    >>
    >> string PurchaseType = (Convert.ToString(drwData["DailyItem"]) ==
    >> "True") ? "ID" : "PID";
    >>
    >> XmlNodeList Selections = xdcData.GetElementsByTagName((X == 0) ?
    >> "Highlight" : "Selection");

    >
    > It has been a very long time since I've done any VB programming so please
    > check my work. But that should convert to something like this.
    >
    > dim PurchaseType As String
    > dim Selections As XmlNodeList
    >
    > If drwData["DailyItem"].ToString() = "True" Then
    > PurchaseType = "ID"
    > Else
    > PurchaseType = "PID"
    > End If
    >
    >
    >
    > If X = 0 Then
    > Selections = xdcData.GetElementsByTagName("Highlight")
    > Else
    > Selections = xdcData.GetElementsByTagName("Selection")
    > End If
    >
    > Hope that helps,
    >
    > --
    > Sean
     
    Karl Seguin [MVP], Jan 26, 2006
    #4
  5. Hi,

    VB has similar function called IIF

    <From MSDN>
    Public Function IIf( _
    ByVal Expression As Boolean, _
    ByVal TruePart As Object, _
    ByVal FalsePart As Object _
    )

    Keep in mind that this is a FUNCTION as oposed to C# ?:, which is operator.
    What does it mean? This means that TruePart and FalsePart are passed as
    parameter to the function and if they are emthods' return values sides are
    going to be executed regardless of the value of the Expression. This might
    lead to hard-to-discover semantic errors if calculations of the true-part
    and/or false-part have side effects.


    --
    HTH
    Stoitcho Goutsev (100)

    ".Net Sports" <> wrote in message
    news:...
    >I need to convert some C# ?Conditionals over to vb.net (most likely in
    > vb.net using If..Then statements), but the C#2VBNETconverter by
    > KamalPatel isn't converting them:
    >
    > string PurchaseType = (Convert.ToString(drwData["DailyItem"]) ==
    > "True") ? "ID" : "PID";
    >
    > XmlNodeList Selections = xdcData.GetElementsByTagName((X == 0) ?
    > "Highlight" : "Selection");
    >
    > TIA
    > NetSports
    >
     
    Stoitcho Goutsev \(100\), Jan 26, 2006
    #5
  6. "Karl Seguin [MVP]" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME
    net> schrieb:
    >I would recommend Sean's solution as well.
    >
    > if you really want to single-line it, you can always use the iif function:
    > http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vblr7/html/vafctiif.asp


    ACK, but note that all parameters are evaluated in the 'IIf' function.

    --
    M S Herfried K. Wagner
    M V P <URL:http://dotnet.mvps.org/>
    V B <URL:http://classicvb.org/petition/>
     
    Herfried K. Wagner [MVP], Jan 26, 2006
    #6
  7. (You get what you pay for with free converters)

    Our Instant VB demo edition gives:

    Dim PurchaseType As String
    If (Convert.ToString(drwData("DailyItem")) = "True") Then
    PurchaseType = "ID"
    Else
    PurchaseType = "PID"
    End If

    Dim Selections As XmlNodeList
    If (X = 0) Then
    Selections = xdcData.GetElementsByTagName("Highlight")
    Else
    Selections = xdcData.GetElementsByTagName("Selection")
    End If

    --
    David Anton
    www.tangiblesoftwaresolutions.com
    Instant C#: VB to C# converter
    Instant VB: C# to VB converter
    Instant C++: C# to C++ converter & VB to C++ converter
    Instant J#: VB to J# converter



    ".Net Sports" wrote:

    > I need to convert some C# ?Conditionals over to vb.net (most likely in
    > vb.net using If..Then statements), but the C#2VBNETconverter by
    > KamalPatel isn't converting them:
    >
    > string PurchaseType = (Convert.ToString(drwData["DailyItem"]) ==
    > "True") ? "ID" : "PID";
    >
    > XmlNodeList Selections = xdcData.GetElementsByTagName((X == 0) ?
    > "Highlight" : "Selection");
    >
    > TIA
    > NetSports
    >
    >
     
    =?Utf-8?B?RGF2aWQgQW50b24=?=, Jan 27, 2006
    #7
  8. Note that replacing "?" with IIf (which is what the free converters do) is
    not correct.
    The correct conversion is if/else.
    --
    David Anton
    www.tangiblesoftwaresolutions.com
    Instant C#: VB to C# converter
    Instant VB: C# to VB converter
    Instant C++: C# to C++ converter & VB to C++ converter
    Instant J#: VB to J# converter



    "Peter Macej" wrote:

    > Take a look at other converters. Free SharpDevelop from
    > http://www.icsharpcode.net/OpenSource/SD/ can do this. There is also
    > online version at
    > http://www.developerfusion.co.uk/utilities/convertcsharptovb.aspx.
    > It seems it handles these statements correctly.
    >
    > --
    > Peter Macej
    > Helixoft - http://www.vbdocman.com
    > VBdocman - Automatic generator of technical documentation for VB, VB
    > ..NET and ASP .NET code
    >
     
    =?Utf-8?B?RGF2aWQgQW50b24=?=, Jan 27, 2006
    #8
  9. Karl,
    For VS 2005 code I would recommend my generic IIf over the VB.IIf as the
    generic IIf is type safe.

    http://www.tsbradley.net/Cookbook/Generics/genericIIf.aspx

    As Herfried points out, my generic IIf & VB.IIf are both functions all
    parameters are evaluated, meaning there may be subtle side effects, if you
    are simply passing constants for the truePart & falsePart parameters, then
    the fact IIf is a function wouldn't matter... If the parameters are function
    calls then this may matter...


    --
    Hope this helps
    Jay [MVP - Outlook]
    ..NET Application Architect, Enthusiast, & Evangelist
    T.S. Bradley - http://www.tsbradley.net


    "Karl Seguin [MVP]" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME
    net> wrote in message news:%...
    |I would recommend Sean's solution as well.
    |
    | if you really want to single-line it, you can always use the iif function:
    |
    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vblr7/html/vafctiif.asp
    |
    | Karl
    |
    | --
    | MY ASP.Net tutorials
    | http://www.openmymind.net/
    |
    |
    | "Fao, Sean" <-WANT-NO-SPAM> wrote in message
    | news:ODJq$...
    | > .Net Sports wrote:
    | >> I need to convert some C# ?Conditionals over to vb.net (most likely in
    | >> vb.net using If..Then statements), but the C#2VBNETconverter by
    | >> KamalPatel isn't converting them:
    | >>
    | >> string PurchaseType = (Convert.ToString(drwData["DailyItem"]) ==
    | >> "True") ? "ID" : "PID";
    | >>
    | >> XmlNodeList Selections = xdcData.GetElementsByTagName((X == 0) ?
    | >> "Highlight" : "Selection");
    | >
    | > It has been a very long time since I've done any VB programming so
    please
    | > check my work. But that should convert to something like this.
    | >
    | > dim PurchaseType As String
    | > dim Selections As XmlNodeList
    | >
    | > If drwData["DailyItem"].ToString() = "True" Then
    | > PurchaseType = "ID"
    | > Else
    | > PurchaseType = "PID"
    | > End If
    | >
    | >
    | >
    | > If X = 0 Then
    | > Selections = xdcData.GetElementsByTagName("Highlight")
    | > Else
    | > Selections = xdcData.GetElementsByTagName("Selection")
    | > End If
    | >
    | > Hope that helps,
    | >
    | > --
    | > Sean
    |
    |
     
    Jay B. Harlow [MVP - Outlook], Jan 27, 2006
    #9
  10. Cor Ligthert [MVP], Jan 27, 2006
    #10
  11. .Net Sports

    Phill W. Guest

    ".Net Sports" <> wrote in message
    news:...
    >I need to convert some C# ?Conditionals over to vb.net
    >
    > string PurchaseType = (Convert.ToString(drwData["DailyItem"]) ==
    > "True") ? "ID" : "PID";


    The direct equivalent of the "?" operator is Visual Basic's IIf function:

    Dim PurchaseType As String _
    = DirectCast( IIf( drwData.Item("DailyItem").Equals( "True" ) _
    , "ID" _
    , "PID" _
    ), String )

    (line breaks for clarity(?) only)

    HTH,
    Phill W.
     
    Phill W., Jan 27, 2006
    #11
  12. .Net Sports

    Marc Gravell Guest

    Yes... but no...

    As David observed - the *correct* replacement is If / Else / End If

    The difference is that IIf always evaluates both sides, where-as ? and the
    If / Else / End If construct only evaluate the side appropriate based on the
    logical test.

    So yes, IIf looks very similar, but it doesn't work the same, and you could
    end up running more methods than you want; bad if the two sides are
    expensive functions (for example).

    You get similar problems with short-circuiting boolean expressions - hence
    VB.Net introducing the AndAlso / OrElse operators (not present in VB6).

    Aside: to quote Michael S a while ago:

    >I think we should have the OrElse keyword in C#. But only as an empty
    >keyword to threaten the compiler:
    >
    > myList.Add(myString) OrElse;
    >
    > Or maybe it could be used to eat exceptions. Hey, that would be quite
    > handy sometimes.


    One of my favourite quotes; another from my top 10 (also from Michael S):

    > Visual Basic Sucks So Hard It Bends Light.


    Marc
     
    Marc Gravell, Jan 27, 2006
    #12
  13. So would I, and that's why I'm recommending it, but I did want to make all
    options known.

    Thanks to both Herfried and Jay for pointing out the implications of IIF,
    which I wasn't aware of and should certainly be taken into account.

    Karl

    --
    MY ASP.Net tutorials
    http://www.openmymind.net/


    "Cor Ligthert [MVP]" <> wrote in message
    news:e%...
    > Karl,
    >
    >> if you really want to single-line it, you can always use the iif
    >> function:
    >> http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vblr7/html/vafctiif.asp
    >>

    > Why to obfuscate it to prevent maintenance?
    >
    > I go for the solution from Sean
    >
    > :)
    >
    > Cor
    >
    >
     
    Karl Seguin [MVP], Jan 27, 2006
    #13
  14. Phil,
    If your on VS 2005, check out my generic IIf (link previously posted), you
    can simplify your statement to:

    | Dim PurchaseType As String _
    | = IIf( drwData.Item("DailyItem").Equals( "True" ) _
    | , "ID" _
    | , "PID" _
    | )

    The above line is with Option Strict On! My generic IIf uses generics & type
    inference to "create" a "IIf(boolean, String, String) As String" as needed
    and call it, no ugly DirectCast needed, no boxing of value types. Totally
    Option Strict On friendly!


    Of course there may be side effects...

    Consider the following:

    ' note we are explicitly setting the seed values in each case to
    ' ensure we get the same sequence of pseudo random numbers back.

    Dim falseCounter As New Random(1)
    Dim trueCounter As New Random(2)
    Debug.WriteLine(IIf(True, trueCounter.Next(), falseCounter.Next()),
    "true")
    Debug.WriteLine(IIf(False, trueCounter.Next(), falseCounter.Next()),
    "false")

    In VB it displays:

    true: 1655911537
    false: 237820880

    While in alleged direct equivalent in C#:

    Random falseCounter = new Random(1);
    Random trueCounter = new Random(2);
    Debug.WriteLine(true ? trueCounter.Next() : falseCounter.Next(),
    "true");
    Debug.WriteLine(false ? trueCounter.Next() :
    falseCounter.Next(), "false");

    Displays:

    true: 1655911537
    false: 534011718


    Notice that the second line doesn't match, this is because VB called the
    falseCounter.Next function twice (once on each line), while C# only called
    it once (only on the second line).

    If instead of IIf we use the code that Sean gave:

    Dim value As Integer
    If True Then
    value = trueCounter.Next()
    Else
    value = falseCounter.Next()
    End If
    Debug.WriteLine(value, "true")
    If False Then
    value = trueCounter.Next()
    Else
    value = falseCounter.Next()
    End If
    Debug.WriteLine(value, "false")



    We then get the same results as C#:

    true: 1655911537
    false: 534011718

    Consider what happens if instead of calling Random.Next() you are calling a
    GetNextSequenceNumber function, that simply increments a counter & return
    it,

    id = IIf(sequenceAlreadyExists, theExistingSequence,
    GetNextSequenceNumber())

    For example you are creating records from some input source and if the
    record doesn't have an ID you call GetNextSequenceNumber to assign a new
    one. The above line in VB will cause gaps in the sequence number, as is
    called for every "line" in the input source. Consider what happens if the
    GetNextSequenceNumber() function is fairly expensive (performance, memory
    pressure, network, other) to execute.

    As I stated, in the case of constants like the OP & your example, using IIf
    doesn't hurt, however I strongly recommend any one using IIf to understand
    what it is actually doing!

    --
    Hope this helps
    Jay [MVP - Outlook]
    ..NET Application Architect, Enthusiast, & Evangelist
    T.S. Bradley - http://www.tsbradley.net


    "Phill W." <-c.u-k> wrote in message
    news:drd5h1$9v5$...
    | ".Net Sports" <> wrote in message
    | news:...
    | >I need to convert some C# ?Conditionals over to vb.net
    | >
    | > string PurchaseType = (Convert.ToString(drwData["DailyItem"]) ==
    | > "True") ? "ID" : "PID";
    |
    | The direct equivalent of the "?" operator is Visual Basic's IIf function:
    |
    | Dim PurchaseType As String _
    | = DirectCast( IIf( drwData.Item("DailyItem").Equals( "True" ) _
    | , "ID" _
    | , "PID" _
    | ), String )
    |
    | (line breaks for clarity(?) only)
    |
    | HTH,
    | Phill W.
    |
    |
     
    Jay B. Harlow [MVP - Outlook], Jan 27, 2006
    #14
  15. .Net Sports

    ZenRhapsody Guest

    All of you are making a huge deal of this. The auto converter is wrong IF
    there are any function calls in the <true> or <false> sections of
    <conditional> ? <true> : <false>

    VB correct equivalent is

    IF <conditional> Then
    <true>
    ELSE
    <false>
    END IF

    Don't bother with IIF() function - it will screw you because is supposedly
    evaluates both the true and false sections before deciding what to do.
    IF/ELSE does not. IF/ELSE correctly evaluates only the needed section of
    code.




    ".Net Sports" <> wrote in message
    news:...
    >I need to convert some C# ?Conditionals over to vb.net (most likely in
    > vb.net using If..Then statements), but the C#2VBNETconverter by
    > KamalPatel isn't converting them:
    >
    > string PurchaseType = (Convert.ToString(drwData["DailyItem"]) ==
    > "True") ? "ID" : "PID";
    >
    > XmlNodeList Selections = xdcData.GetElementsByTagName((X == 0) ?
    > "Highlight" : "Selection");
    >
    > TIA
    > NetSports
    >
     
    ZenRhapsody, Jan 29, 2006
    #15
  16. .Net Sports

    Ted Miller Guest

    Don't minimize the issue -- function calls are not the only constructs with
    side effects.

    "ZenRhapsody" <> wrote in message
    news:%23k5I$...
    > All of you are making a huge deal of this. The auto converter is wrong IF
    > there are any function calls in the <true> or <false> sections of
    > <conditional> ? <true> : <false>
    >
    > VB correct equivalent is
    >
    > IF <conditional> Then
    > <true>
    > ELSE
    > <false>
    > END IF
    >
    > Don't bother with IIF() function - it will screw you because is supposedly
    > evaluates both the true and false sections before deciding what to do.
    > IF/ELSE does not. IF/ELSE correctly evaluates only the needed section of
    > code.
    >
    >
    >
    >
    > ".Net Sports" <> wrote in message
    > news:...
    >>I need to convert some C# ?Conditionals over to vb.net (most likely in
    >> vb.net using If..Then statements), but the C#2VBNETconverter by
    >> KamalPatel isn't converting them:
    >>
    >> string PurchaseType = (Convert.ToString(drwData["DailyItem"]) ==
    >> "True") ? "ID" : "PID";
    >>
    >> XmlNodeList Selections = xdcData.GetElementsByTagName((X == 0) ?
    >> "Highlight" : "Selection");
    >>
    >> TIA
    >> NetSports
    >>

    >
    >
     
    Ted Miller, Feb 3, 2006
    #16
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mustafa Aydin

    Conditionals & paths

    Mustafa Aydin, Apr 21, 2004, in forum: XML
    Replies:
    0
    Views:
    466
    Mustafa Aydin
    Apr 21, 2004
  2. Replies:
    1
    Views:
    1,450
    Zafar Abbas
    Dec 30, 2005
  3. Barney Frank
    Replies:
    3
    Views:
    336
    anton muhin
    Jul 29, 2004
  4. Joshua Ginsberg

    Inline Conditionals?

    Joshua Ginsberg, Aug 24, 2004, in forum: Python
    Replies:
    15
    Views:
    3,871
    Martin Maney
    Sep 6, 2004
  5. Robert Brewer

    RE: Inline Conditionals?

    Robert Brewer, Aug 26, 2004, in forum: Python
    Replies:
    1
    Views:
    338
    Alex Martelli
    Aug 26, 2004
Loading...

Share This Page