Converting an Object to String

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • polymorphic
    New Member
    • Oct 2006
    • 28

    Converting an Object to String

    I need to convert a data object to string.

    Currently, my script follows as:
    strTitle = CStr(<%=mobjSys tem.DescrShort %>)

    When this is compiled it appears as:

    strTitle = CStr(SOPs - Title1)

    The error I get is that a '0' is expected. I've tried different variants with no success. The reason I want to convert it to string is to later pass it as part of a URL: Server.URLEncod e(strTitle)

    Any advice?

    Thanks,
    cj
  • JamieHowarth0
    Recognized Expert Contributor
    • May 2007
    • 537

    #2
    Hi cj,

    This code means that ASP will parse the value in mobjSystem.Desc rShort as the unquoted parameter of CStr.
    Take out the <%= and %> and ASP will then parse the value as a quoted string like so:
    [code=asp]
    strTitle = CStr(mobjSystem .DescrShort )
    [/code]

    which, when compiled, turns into:
    [code=asp]
    strTitle = CStr("SOPs - Title1")
    [/code]
    The error "0" is expected is because you can't substract strings - your string contains the "-" and when this is turned into a parameter, ASP treats it as "SOPs" is a variable, "Title1" is a variable and it should subtract Title1 from SOPs.
    Failing that, use this code instead:
    [code=asp]
    strTitle = CStr("<%=mobjSy stem.DescrShort %>")
    [/code]
    That way, you've put the quotes in for ASP to recognise the value as a string.

    Hope it helps.

    medicineworker

    Comment

    • polymorphic
      New Member
      • Oct 2006
      • 28

      #3
      Originally posted by medicineworker
      Hi cj,

      This code means that ASP will parse the value in mobjSystem.Desc rShort as the unquoted parameter of CStr.
      Take out the <%= and %> and ASP will then parse the value as a quoted string like so:
      [code=asp]
      strTitle = CStr(mobjSystem .DescrShort )
      [/code]

      which, when compiled, turns into:
      [code=asp]
      strTitle = CStr("SOPs - Title1")
      [/code]
      The error "0" is expected is because you can't substract strings - your string contains the "-" and when this is turned into a parameter, ASP treats it as "SOPs" is a variable, "Title1" is a variable and it should subtract Title1 from SOPs.
      Failing that, use this code instead:
      [code=asp]
      strTitle = CStr("<%=mobjSy stem.DescrShort %>")
      [/code]
      That way, you've put the quotes in for ASP to recognise the value as a string.

      Hope it helps.

      medicineworker

      That is a good point. It will not read strTitle = CStr("<%=mobjSy stem.DescrShort %>"). The error reads "mobjSystem " required.

      Comment

      • polymorphic
        New Member
        • Oct 2006
        • 28

        #4
        Originally posted by polymorphic
        That is a good point. It will not read strTitle = CStr("<%=mobjSy stem.DescrShort %>"). The error reads "mobjSystem " required.
        OK, Here I think I have the answer:

        What I think was happening is that when I was converting to string:
        strTitle = CStr(<%=mobjSys tem.DescrShort %>)

        The result would be:

        strTitle = SOPs - Research Pilot Brewery

        This would result in an error: "Expected end of statement" - Error 0 .

        When I used strTitle = CStr("<%=mobjSy stem.DescrShort %>") with the Server.URLEncod e then the mobjSystem object would not be recognized but literally. When I removed the Server.URLEncod e(strTitle), and changed it to just strTitle THEN it worked.

        One question: Is "Expected end of statement" - Error 0 a mathematical error? How did you know the "-" was being used as a substract?

        Thanks much,
        cj

        Comment

        • Nicodemas
          Recognized Expert New Member
          • Nov 2007
          • 164

          #5
          No.

          Just do this:

          Code:
          strTitle = CStr(mobjSystem.DescrShort)
          It'll work, as the others have pointed out. strTitle will then be whatever the value of mobjSystem.Desc rShort is.

          Your problem was that you had ASP delimiters (<%... %>) in your ASP code.

          Comment

          Working...