Using extensions in an aspx page? (with code-behind) RRS feed

  • Question

  • User1860262783 posted

    I have created a class containing a few string extensions, as follows:

    Imports Microsoft.VisualBasic
    Namespace Extensions
        <HideModuleName()> _
        Public Module Extensions
            <System.Runtime.CompilerServices.Extension()> _
            Public Function Replace(ByVal s As String, ByVal charPosition As Integer, ByVal replaceWith As Char) As String
                Return s.Substring(0, charPosition) + replaceWith + s.Substring(charPosition + 1)
            End Function
            <System.Runtime.CompilerServices.Extension()> _
            Public Function Replace(ByVal s As String, ByVal start As Integer, ByVal length As Integer, ByVal replaceWith As String) As String
                Return s.Substring(0, start) + replaceWith + s.Substring(start + length)
            End Function
            <System.Runtime.CompilerServices.Extension()> _
            Public Function ToProperCase(ByVal s As String) As String
                Dim ret As String = s.ToLower()
                Dim regEx As New Regex("^(.)|\s(.)|\.\s*(.)|\/(.)", RegexOptions.Compiled)
                For Each m As RegularExpressions.Match In regEx.Matches(s)
                    ret = ret.Replace(m.Index, m.Length, m.Value.ToUpper())
                Return ret
            End Function
        End Module
    End Namespace

    This works fine when I call the new methods (str.ToProperCase) within an aspx.vb file, but when I try and use 'String.ToProperCase(Eval("blah"))' or 'Eval("blah").ToString().ToProperCase()' I get an error telling me that "'ToProperCase' is not a member of 'String'". Oh, just in case it's relevant to this, I'm using .Net Framework 2.0.

    Would I need to register something within the aspx page in order to use extensions? If not, is there another way to enable them?


    Friday, September 4, 2009 5:56 AM

All replies

  • User-1659704165 posted


    U may Try overriding The string..



    Chk the above link For more info

    Friday, September 4, 2009 6:07 AM
  • User1860262783 posted

    You mean overriding ".ToString()"? I don't see how this helps me add functionality to the String object, tbh. Either you don't understand what I am trying to do or I don't understand your solution...

    It seems like you are suggesting that I override .ToString() to make it apply "Proper Case", is that correct?

    Sunday, September 6, 2009 7:22 PM