Results 1 to 4 of 4

Thread: Trimming a String Function with ASP.NET and VB.NET

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2004
    Posts
    2

    Trimming a String Function with ASP.NET and VB.NET

    I'm trying to run a simple function on a dataset field but can not for the life of me figure out how to get it to work.


    All I want to do it trim the text that is output by some bound data. The function I have written is below

    Code:
    <script runat="server"> 
    Function TrimTeaser( strString As String) 
    strString = strString.Substring (0,4) 
    End Function 
    </script>
    On the page:

    Code:
    <%# TrimTeaser(ds_main_news.FieldValue("headerText", Container)) %>
    I have tried so many variations and all either complain of missing Expressions, no String data, or it simply does not print to the page. Help!

    Cheers,
    Jeff

  2. #2
    Frenzied Member Memnoch1207's Avatar
    Join Date
    Feb 2002
    Location
    DUH, Guess...Hint: It's really hot!
    Posts
    1,861
    Use Trim$() function.
    Being educated does not make you intelligent.

    Need a weekend getaway??? Come Visit

  3. #3

    Thread Starter
    New Member
    Join Date
    Apr 2004
    Posts
    2
    So I changed it to Substring$ and the page renders without errors, but nothing shows up where the trimmed string is supposed to.

    Code:
       
    <script runat="server"> 
    Function TrimTeaser( strString As String) 
    strString = strString.Substring$ (0,4) 
    End Function 
    </script>

  4. #4
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    You are missing one VERY IMPORTANT line of code my friend.

    IF you will recall, a function is differentiated from a Sub by one important thing... it RETURNS something.

    You have no return in your function, effectively returning nothing.
    VB Code:
    1. <script runat="server">
    2. Function TrimTeaser(strString As String)
    3. Return strString.SubString(0,4)
    4. End Function
    5. </script>


    That said, if that's all your function does, you might as well not even create a function for it, and skip the overhead.
    Code:
    <%# DirectCast(ds_main_news.FieldValue("headerText", Container),String).SubString(0,4) %>
    If you get an error with the code immediately above, its because I don't use Crystal reports, and therefore am unclear what fieldvalue returns, I only guess its an object of some kind. You may have to substitute convert.toString instead.
    Last edited by nemaroller; Apr 6th, 2004 at 11:12 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width