-2

I don't see ternary operator listed under Operators in VBScript language. Wondering if we can use it in VbScript or any alternative way to easily implement the same WITHOUT using IF.

newuser
  • 73
  • 1
  • 1
  • 9
  • I know the way to implement using IF. Curious if there is any alternate keyword/simple code to implement ternary WITHOUT using IF as mentioned in my question. – newuser Sep 28 '16 at 16:32
  • @Victor Moraes Seems like there is nothing as such as per the post. Thanks ! – newuser Sep 28 '16 at 16:33
  • 1
    You asked about ternary operations, a.k.a `IIF`, as implemented in VBA. For example: `IIf (Expression; TruePart; FalsePart)` (which also does not exist in VBScript by default). You are looking for a syntax of ternary operator in VBScript, as in the link I've posted. By the way, this was the syntax used by OP in the referred link's question: `lunchLocation = (dayOfTheWeek == "Tuesday") ? "Fuddruckers" : "Food Court";`. Pretty close to what you've asked and I see no `If` in their example aswell – Victor Moraes Sep 28 '16 at 16:39
  • Exactly for the case `(a>b) ? 1 : 0` you may use `x = (a > b) * -1`. And generally without using `If` try constuction `Select Case a > b: Case True x = 1: Case Else x = 0: End Select` or `Select Case True: Case a > b x = 1: Case Else x = 0: End Select` ;) – omegastripes Sep 28 '16 at 17:06
  • @Victor Moraes IIf is implemented using IF. I posted this question to know if there is any keyword or other way to implement without IF. – newuser Sep 28 '16 at 17:36
  • 1
    Ok, so you know the answer is "no, there isn't", and this is still duplicate since it is, in one or the other, questioning about ternary operators. Same core question which is already answered in the other topic. If there isn't a ternary operator in VBScript, there isn't anything *with* or *without* `If` :) – Victor Moraes Sep 28 '16 at 17:44
  • 1
    @VictorMoraes is absolutely right, just read [this answer](http://stackoverflow.com/a/20353438/692942) from [the duplicate](http://stackoverflow.com/q/20353072/692942) that's been flagged - *"The **conditional ternary operator doesn't exist out of the box**, but it's pretty easy to create your own version in VBScript"*. That's your answer right there! – user692942 Sep 28 '16 at 17:48
  • @omegastripes hmm yeah, because that's easier or more readable then an [`IIf()` implementation](http://stackoverflow.com/a/20353438/692942). – user692942 Sep 28 '16 at 17:51

1 Answers1

0

I will not use this instead of my original answer, but ... Can it be done without IF?

Option Explicit

Sub LetSet(ByRef Variable, ByRef Value)
    Select Case IsObject(Value)
        Case True : Set Variable = Value
        Case Else : Variable = Value
    End Select
End Sub

Function IIf(Condition, TrueValue, FalseValue)
Dim bCondition
    bCondition = False 
    On Error Resume Next
    bCondition = CBool(Condition)
    On Error Goto 0
    Select Case bCondition
        Case True : LetSet IIf, TrueValue
        Case Else : LetSet IIf, FalseValue
    End Select
End Function 

    WScript.Echo IIf( True, "1", "2" )
    WScript.Echo IIf( False, "1", "2" )

Dim obj
    With WScript.CreateObject("Scripting.FileSystemObject")
        Set obj = .GetFile(WScript.ScriptFullName)
    End With 

    WScript.Echo IIf( True, obj, WScript )
    WScript.Echo IIf( False, obj, WScript )
Community
  • 1
  • 1
MC ND
  • 69,615
  • 8
  • 84
  • 126
  • They just want to know if they could use a ternary operator instead of a conditional *(they say `If` but they mean any conditional including `Select`)* why re-post this here, it's [already covered](http://stackoverflow.com/a/20355065/2861476) *(by you no-less)*. Simple answer to the question is "No". – user692942 Sep 28 '16 at 18:56
  • 1
    @Lankymart, Probably you are right. From my point of view the answer is *"There are better ways to do it and all use `IF`"* (and that is already pointed), but I don't know **why** OP insists in not using `IF` (at least at this moment there is not any OP reference to `select` in the comments), I can not see any but *maybe* OP has a reason for it, so I posted the answer. – MC ND Sep 28 '16 at 19:29