Professional Web Applications Themes

Overriding Trim Function - ASP.NET General

Well, you're not really overloading it, you are writing another function with the same name. Overloading is something you do with a class you derive from the original. But that's just semantics. What you need to do is specify what you want to do more specifically. You'll need to create a reference to the assembly that contains the Trim function that you want to use. Then, fully qualify the name of your function. If your function is in the namespace Craig, and is a member of the class Stephajn, then you should call Craig.Stephajn.Trim(...) rather than just Trim(...), which is ...

  1. #1

    Default Re: Overriding Trim Function

    Well, you're not really overloading it, you are writing another function
    with the same name. Overloading is something you do with a class you derive
    from the original. But that's just semantics.

    What you need to do is specify what you want to do more specifically. You'll
    need to create a reference to the assembly that contains the Trim function
    that you want to use. Then, fully qualify the name of your function. If your
    function is in the namespace Craig, and is a member of the class Stephajn,
    then you should call Craig.Stephajn.Trim(...) rather than just Trim(...),
    which is ambiguous when not fully qualified.

    --
    Chris Jackson
    Software Engineer
    Microsoft MVP - Windows XP
    Windows XP Associate Expert
    --
    "Stephajn Craig" <com> wrote in message
    news:O4O81P$phx.gbl... 
    cutomized 
    on 
    my 
    Component 


    Chris Guest

  2. #2

    Default Re: Overriding Trim Function

    Stephajn,
    I was playing with this a little just now.

    If you include the Import directive just after the Page directive in your
    ASPX file, it should do what you want.

    <% Page Language="vb" Codebehind="page1.aspx.vb"
    inherits="webproject.page1" %>
    <% Import namespace="webproject" %>

    Where the name of my ASP.NET project is 'webproject'.

    Unfortunately you then get a BC30561 compile error: 'Trim is ambiguous,
    imported from the namespaces or types 'Microsoft.VisualBasic.Strings,
    webproject.TestModule'.

    You will need to change the name of your Trim function, or add Trim as a
    method to the base class for your page.

    page1.aspx inherits from class page1 in page1.aspx.vb
    class page1 inherits from class BasePage in BasePage.vb
    class BasePage inherits from System.Web.UI.Page

    Then add your Trim function to the BasePage class. The BasePage class can
    contain all the functionality (VB code) that is common across all pages. It
    cannot contain common ASPX code.

    Wouldn't it be easier to modify your retrieves from the database to avoid
    returning NULL values? Put the check for NULL in the Stored Procedure or
    Select statement you are using. Then your ASPX can simply use the values
    instead of need to worry about NULLs...

    Hope this helps
    Jay

    "Stephajn Craig" <com> wrote in message
    news:O4O81P$phx.gbl... 
    cutomized 
    on 
    my 
    Component 


    Jay Guest

  3. #3

    Default Re: Overriding Trim Function

    WHEW! So I'd have to create a base page class and then inherit all of my
    pages from this base class containing the Trim function. Doens't sound like
    fun when I've got One ASPX page, and lots of ASCX pages that get loaded
    dynamically based on QueryString data.

    I have a friend working on writing a VFP app that would allow me to select
    any column of any table in any database and go through it replacing null
    values with empty string values instead. I was just hoping that there might
    be a way for me to replace MS's goofy Trim function that doesn't handle an
    exception like this, with one of my own.

    Note to MS! Your Trim function in Microsoft.VisualBasic needs an exception
    handler to allow for DBNull values! (Somehow I doubt that will ever be
    seen....)

    --
    Stephajn Craig
    "Jay B. Harlow [MVP - Outlook]" <msn.com> wrote in message
    news:phx.gbl... 
    It 
    > cutomized [/ref]
    Function. [/ref]
    even [/ref]
    function [/ref]
    this [/ref]
    of 
    > Component 
    >
    >[/ref]


    Stephajn Guest

  4. #4

    Default Re: Overriding Trim Function

    Hi John,

    Yes, that's the one that I'm referring to. Naturally, a DBNull value
    cannot be converted to a String, so I created a version of the Trim function
    that looks like this:

    Public Function Trim(ByVal Value As Object) As String

    Try

    If LCase(Value.GetType.ToString) = "system.dbnull" Then

    Return String.Empty

    Else

    Return CType(Value, String).Trim

    End If

    Catch ex As Exception

    Return ""

    End Try

    End Function


    And this works for me when it's called. By passing in an Object datatype,
    and then attempting to CType it to a String, I can check for the DBNull, and
    handle an Exception where casting it to a string wouldn't work.

    As for running with Option Strict on, no I'm not.

    --
    Stephajn Craig
    "John Saunders" <com> wrote in message
    news:uKv%phx.gbl... [/ref]
    my 
    > like [/ref]
    select 
    > might [/ref]
    an 
    > exception 
    >
    > Excuse me, which Trim function are you referring to? Is it this one?
    >
    > Public Shared Function Trim(ByVal Str As String) As String
    >
    > If so, note that it takes a String parameter. It's not _possible_ for it[/ref]
    to 


    Stephajn Guest

  5. #5

    Default Re: Overriding Trim Function

    "Stephajn Craig" <com> wrote in message
    news:%phx.gbl... 
    function 
    and 

    The question is how are you getting DBNulls with which to call your Trim
    function?

    And if you were running with Option Strict On, you'd be encouraged not to do
    this.
    --
    John Saunders
    Internet Engineer
    com


    John Guest

  6. #6

    Default Re: Overriding Trim Function

    Stephajn,
    Maybe this won't help you with the trim problem, but if you never want to
    see a null come back from the DB, try using ISNULL in your SQL SELECT
    statement. (Read the Docs for examples - it's pretty simple)
    mark


    "Stephajn Craig" <com> wrote in message
    news:%phx.gbl... 
    function 
    and [/ref]
    > my [/ref][/ref]
    sound [/ref][/ref]
    loaded [/ref]
    > select [/ref][/ref]
    null 
    > > might [/ref][/ref]
    handle 
    > > exception [/ref][/ref]
    be 
    > >
    > > Excuse me, which Trim function are you referring to? Is it this one?
    > >
    > > Public Shared Function Trim(ByVal Str As String) As String
    > >
    > > If so, note that it takes a String parameter. It's not _possible_ for it[/ref]
    > to 
    >
    >[/ref]


    Mark Guest

  7. #7

    Default Re: Overriding Trim Function

    Hey there Mark,

    I didn't know about that function! But I guess it can't be used if
    you're using the much hated "select * from tbl" sort of statement. (And
    when I built this database, that was exactly what I was doing since I didn't
    have as much database knowledge back then as I do now.) Or can I use that
    function with select *? I tried "select IsNull(*, '') from Departments but
    that gives me an error

    Stephajn

    --
    Stephajn Craig
    "Mark MacRae" <com> wrote in message
    news:phx.gbl... [/ref]
    value 
    > function [/ref]
    datatype, 
    > and [/ref][/ref]
    of [/ref]
    > sound [/ref]
    > loaded 
    > > select [/ref]
    > null [/ref][/ref]
    there [/ref]
    > handle [/ref]
    > be [/ref][/ref]
    it 
    > >
    > >[/ref]
    >
    >[/ref]


    Stephajn Guest

  8. #8

    Default Re: Overriding Trim Function

    Hi John,

    Some of the data in the database my app runs from has null values in it.
    Not so much because I put them there, but because the data was inherited
    from a previous site that allowed for this kind of thing.

    What do you mean when you say "And if you were running with Option
    Strict On, you'd be encouraged not to do this."


    --
    Stephajn Craig
    "John Saunders" <com> wrote in message
    news:phx.gbl... [/ref]
    value 
    > function [/ref]
    datatype, 
    > and 
    >
    > The question is how are you getting DBNulls with which to call your Trim
    > function?
    >
    > And if you were running with Option Strict On, you'd be encouraged not to[/ref]
    do 


    Stephajn Guest

Similar Threads

  1. trim function
    By Nick in forum Macromedia Flash
    Replies: 5
    Last Post: June 28th, 01:31 PM
  2. problem with the trim() function
    By dijana1081 in forum Macromedia ColdFusion
    Replies: 1
    Last Post: April 27th, 07:35 PM
  3. Overriding GTK+1.x fonts
    By Javier Kohen in forum Debian
    Replies: 0
    Last Post: July 14th, 05:40 AM
  4. Overriding __doPostBack
    By Mike Dunn in forum ASP.NET General
    Replies: 2
    Last Post: July 9th, 08:25 PM
  5. note 33646 added to function.trim
    By rwelti@yahoo.com in forum PHP Notes
    Replies: 0
    Last Post: July 2nd, 10:45 PM

Bookmarks

Posting Permissions

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139