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>")

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

End Function

'calling the functions

Open in new window

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Open in new window

* Normally, I would expect a function to return some value.
The following is  -  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>")

            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
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.
Bill PrewIT / Software Engineering ConsultantCommented:
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>"
       printattribute = Chr(9) + Chr(9) + Chr(9) + "<attribute name=""" + name + """><![CDATA[" + value + "]]></attribute>"
    End If
End Function

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.