Couple questions on the if statements

Technical support and scripting issues

Moderators: JRL, Dorian (MJT support)

User avatar
support
Automation Wizard
Posts: 1450
Joined: Sat Oct 19, 2002 4:38 pm
Location: London
Contact:

Post by support » Thu Oct 14, 2004 6:38 am

I think your mistake is where you have Value = bla bla it should be GetValue = bla bla. Value is a local variable and never goes anywhere. You want to be setting the result of the function here instead.
MJT Net Support
[email protected]

User avatar
Bob Hansen
Automation Wizard
Posts: 2475
Joined: Tue Sep 24, 2002 3:47 am
Location: Salem, New Hampshire, US
Contact:

Post by Bob Hansen » Thu Oct 14, 2004 1:16 pm

Note re support's suggestion.

That is only part of the problem. I inserted GetValue = Value last nght, but found that the VB always translates to "small". Now get 110 vs. 0.

If you put any string in the function, it translates to small, even using "abc" instead of the three options. That is what prompted me to submit my request for Single Stepping in VBScript. I have not had a chance to get back to that for more troubleshooting.
Hope this was helpful..................good luck,
Bob
A humble man and PROUD of it!

User avatar
support
Automation Wizard
Posts: 1450
Joined: Sat Oct 19, 2002 4:38 pm
Location: London
Contact:

Post by support » Thu Oct 14, 2004 1:24 pm

VBScript strings must always be given in quotes. Hence your script should be this:


VBSTART
Function GetValue(x,y)
If y="small" Then
GetValue = 100+x
ElseIf y="medium" Then
GetValue = 200+x
ElseIf y="large" Then
GetValue = 300+x
else Value=0
End If
End Function
VBEND

//Let>i=5
//Let>j=large
VBEval>GetValue(10,"large"),i
Msg>blah %i%


If using MacroScript variables do the following (because MacroScript strings do not need to be in quotes):

Let>j=large
VBEval>GetValue(10,"%j%"),i
MJT Net Support
[email protected]

Ax
Newbie
Posts: 9
Joined: Wed Oct 06, 2004 5:28 pm

Post by Ax » Thu Oct 14, 2004 2:05 pm

Thanks. I eventually managed to get that working late last night and forgot to report it back. Sorry for the trouble..

And yea I also ran into the problem that it always returns the first statement ("Small" in this case).. That puzzled me for a bit, before I ran into the aforementioned problem.

There is much of the syntax in VBScript that is still not entirely clear to me.

For instance, is there a way to mimic a "return" statement (present in C/Java).

So instead of doing GetValue = 100+x I'd be able to do return 100+x.

And more importantly, there was something I am not sure about regarding function calls.
At the moment, I am passing two parameters. A number (10 -> x).
The reason I am doing that is because in my test, I was unable to do something like:

VBEval>i+GetValue(large),i
or
VBEval>GetValue(large)+i,i

All I -really- wanted to do.. is to have a function that "returns" a fixed value depending whether a string (small, medium, large) and add that to variable i.

So in Java terms, I'd do:
i += GetValue(Medium) //(for example)

I am sure it is something very basic.. But I just can't seem to get it right..

User avatar
support
Automation Wizard
Posts: 1450
Joined: Sat Oct 19, 2002 4:38 pm
Location: London
Contact:

Post by support » Thu Oct 14, 2004 2:28 pm

Hi,

There is no result in VBScript functions. But you could have a local variable and just set the function to that at the end of the function if you would prefer to see the result returned only once.

As for adding one to the result of the function, yes, of course you can do this. I think the reason your code failed is because you do not have quotes around your string. The following example returns 11 in ans:


VBSTART

Function DoSomething(x)
DoSomething = x * 2
End Function

VBEND

VBEval>1+DoSomething(5),ans
MJT Net Support
[email protected]

Post Reply
cron
Sign up to our newsletter for free automation tips, tricks & discounts