Class property syntax problems - Newbie

Discussion in 'ASP .Net' started by msch-prv@bluewin.ch, Jul 30, 2006.

  1. Guest

    Sorry for this trivial question: I am a newbie...

    I have difficulties calling up a class poperty.

    Situation: I would like to move the following (sample) function from
    global.asax to a custom class (MyClass). The following works fine in an
    aspx file:

    global.asax:
    Public Shared Function SayHello(strName as string) As String
    Return "Hello, " & strName
    End Function

    aspx file:
    Dim strHello as string = global_asax.SayHello("John")

    Question: How do I proceed with classes? In class MyClass, I have:

    Imports Microsoft.VisualBasic
    Namespace MyClass.Misc

    Public Class StrUtil

    Public ReadOnly Property SayHello(strName as string) As String
    Return "Hello, " & strName
    End Property

    End Class
    End Namespace

    How can I call up SayHello() in an aspx script? The error 'variable
    used before it has been assigned a value' shows up with the following
    code:

    <%@ Import Namespace="MyClass.Misc"%>
    ....
    Dim objStrUtil as StrUtil
    Response.Write("Msg: " & objStrUtil.SayHello("John"))

    Thanks for any hints, Mark
     
    , Jul 30, 2006
    #1
    1. Advertising

  2. Guest

    I found an answer using the nice tutorial at
    http://www.aspnet101.com/aspnet101/tutorials.aspx?id=43

    Create a class file, say 'StrUtil.vb', in App_Code. Define a namespace
    say 'Util.Strings' and add the following:

    Imports Microsoft.VisualBasic
    Imports System.Web.UI.Page

    Namespace Util.Strings

    Public Class StrUtil

    Public Shared Function LimitSize(ByVal strData As String, ByVal
    intSize As Integer) As String

    If strData.ToString().Length > intSize Then
    Return strData.Substring(0, intSize) & "..."
    Else
    Return strData.ToString()
    End If
    End Function

    End Class

    End Namespace

    Now in the aspx file, add the namespace and invoke the name of the
    class function:

    <%@ Import Namespace="Util.Strings"%>
    ...
    <script runat="server">

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As
    System.EventArgs)

    Dim strInput As String = "The quick brown fox jumps over the
    lazy cow."
    Dim strTmp As String = StrUtil.LimitSize(strInput, 20)
    Response.Write("Abridged string: " & strTmp)
    End Sub
     
    , Jul 30, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. E11
    Replies:
    1
    Views:
    4,942
    Thomas Weidenfeller
    Oct 12, 2005
  2. gabriele renzi
    Replies:
    2
    Views:
    238
    gabriele renzi
    Dec 31, 2005
  3. Ken Bloom
    Replies:
    3
    Views:
    243
  4. Good Night Moon
    Replies:
    9
    Views:
    316
    Rick DeNatale
    Jul 25, 2007
  5. Spam Catcher
    Replies:
    4
    Views:
    169
    Darko
    Nov 21, 2007
Loading...

Share This Page