Class property syntax problems - Newbie

M

msch-prv

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
 
M

msch-prv

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Proxy Class Property? 1
Session Problems 2
Property in Class 1
Property 1
Property 0
Property 1
Class: @property -> .__dict__ 6
Property 1

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top