Need VBScript function to return formatted string based on parameters

This is probably a pretty basic function, but i've been having all kinds of trouble with syntax.  I need a function to be able to accept a value, and a name, then return a string based on the value not being null.  If the value is null, then the string needs to add 'clear="true"', and also remove the cdata tags.  Below is some psuedocode to give you an idea of what i'm after.  Thanks.


Function printattribute(value, name)

if len(value) < 1 Then

f.WriteLine(Chr(9)+Chr(9)+Chr(9)+"<attribute clear=""true"" name=""+name+""></attribute>")

else 
f.WriteLine(Chr(9)+Chr(9)+Chr(9)+"<attribute name=""+name+""><![CDATA["+value+"]]></attribute>")
end if

End Function

'calling the functions
printattribute(watermelon,fruit)
printattribute("",fruit)

Open in new window

bmsandeAsked:
Who is Participating?
 
Bill PrewCommented:
This should handle the return of the result.

wscript.echo printattribute("watermelon", "fruit")
wscript.echo printattribute("", "fruit")

Function printattribute(value, name)
    If Len(value) < 1 Then
        printattribute = Chr(9) + Chr(9) + Chr(9) + "<attribute clear=""true"" name=""" + name + """></attribute>"
    Else 
       printattribute = Chr(9) + Chr(9) + Chr(9) + "<attribute name=""" + name + """><![CDATA[" + value + "]]></attribute>"
    End If
End Function

Open in new window

~bp
0
 
aikimarkCommented:
* there is a difference between an empty string and a Null value
* You should be passing strings to the function.
Example:
printattribute("watermelon", "fruit")

Open in new window

* Normally, I would expect a function to return some value.
0
 
labopsCommented:
The following is VB.net  -  the biggest difference is using "" to embed a " in string converted function to string as function wasn't returning a value .

    Private Sub printattribute(value, name)


        If Len(value) < 1 Then

            MsgBox(Chr(9) & Chr(9) & Chr(9) & "<attribute clear=""true""" & name & " = " & name & """><attribute>")

        Else
            MsgBox(Chr(9) & Chr(9) & Chr(9) & "<attribute name=""" & name & """><![CDATA[" & value & "]]></attribute>")
        End If

    End Sub
    Public Sub main()

        'calling the subroutine
        printattribute("watermelon", "fruit")
        printattribute("", "fruit")
    End Sub
0
 
bmsandeAuthor Commented:
ok the syntax appears to be working, but I somehow need to return that string generated by the sub, the msgbox does me no good.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.