Home>

### vbscript - calculation when vbs value is "" "

I have a question about the title.

I want to store the calculation result of the following code in variable a.
When "", I want to calculate as 0.
If the variable used for calculation (this time variable b) contains "" "etc., how can I calculate without error?

If b = "", what is the state of variable b?

``````Dim a
Dim b
Dim c
a = "100"
b = ""
IF b<>"" Then
a = a-60
ELSE
a = a-b-60
END IF``````
Tried source 1
``a = CInt (a)-CInt (b)-60``
Tryed source 2
``````a = CInt (a)-60
a = CInt (a)-CInt (b)``````
Tried source 3
``````IF b<>"" Then
a = a-60
ELSE
b = 0
a = a-b-60
END IF``````
I want to store

a = 40

CInt will fail if the argument cannot be converted to int.
If`a-b-60`is written, type conversion is performed internally.
At this time, "" cannot be converted to int, so an error occurs.

Also, I feel that I don't understand the condition specification of the If statement.

``````If b<>"" Then
If 'b is not ""
Else
When 'b is ""
End If``````

It becomes.

IsNumeric function can be used to determine if it is a number.
Therefore, I think that it would be good to calculate separately when it can be converted as a number and when it cannot be converted.
(This is not only when "", but when it cannot be recognized as a numerical value. If only "", change the condition of the If statement as such)

``````'Determines whether it can be converted to a numeric value using the IsNumeric function
If can be converted as a number? Then
'If it can be converted as a number
a = CInt (a)-CInt (b)-60
Else
'If it cannot be converted as a number
a = CInt (a)-60
End If
If you want to change 'b permanently, you may use the following method.
If can't be converted as a number? Then
'If it cannot be converted as a number
b = "0"
End If
a = CInt (a)-CInt (b)-60``````

Variables`a`and`b`are assumed to be strings only consisting of numbers,
The quickest way is to add`0`to the left of the "0" target variable when`CInt ()`.
``````a = "100"