how to use a class in an aspx file?

J

Jan

Hi,

I defined a function like this:
Public Function myfunction(ByVal myvar As Object) As String
...
Return xyz
End Function

in aspx file, i use it like this:

<asp:Literal ID="lit" runat="server" Text='<%# myfunction(Eval("myfield"))
%>' />


Now i defined a class with the same function like this:

Public Class descr
Public Shared Function myfunction(ByVal myvar As Object) As String
.....
Return xyz
End Function
End Class


My question is: how to use that class in the aspx file?

Thanks
Jan
 
S

susiedba

are you talking about clientside?

ASP.net doesn't support clientside script

sorry
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

Jan said:
Hi,

I defined a function like this:
Public Function myfunction(ByVal myvar As Object) As String
...
Return xyz
End Function

in aspx file, i use it like this:

<asp:Literal ID="lit" runat="server" Text='<%# myfunction(Eval("myfield"))
%>' />


Now i defined a class with the same function like this:

Public Class descr
Public Shared Function myfunction(ByVal myvar As Object) As String
.....
Return xyz
End Function
End Class


My question is: how to use that class in the aspx file?

Thanks
Jan

As the method is static (Shared in VB), you access it by specifying the
class name and the method:

<%# descr.myfunction(Eval("myfield")) %>
 
C

Cor Ligthert [MVP]

Jan,

Be aware that you are creating a dangerous module with your code.
A shared class as you made it, is in VB nothing more than a module, which in
this case is static in memory as all modules are. .

An application in ASP.Net belongs to all clients, therefore the module as
you make it can be used as a conainer for constants. Not for data belonging
to one client. For that you need a real class.

Class TheClassIWantToUse
Public Sub New
'this is the constructor
End Sub
Public function CountTheWhatever(byval Counter as integer)
return Counter + 1
end sub

To use you say
public sub WhatIWantToDo as TheClassIWantToUse

You can than use it as.
counter = WhatIWantToDo.CountTheWhatever(1)

I hope this gives an idea

Cor
 
J

Jan

Thanks to both

Cor Ligthert said:
Jan,

Be aware that you are creating a dangerous module with your code.
A shared class as you made it, is in VB nothing more than a module, which
in this case is static in memory as all modules are. .

An application in ASP.Net belongs to all clients, therefore the module as
you make it can be used as a conainer for constants. Not for data
belonging to one client. For that you need a real class.

Class TheClassIWantToUse
Public Sub New
'this is the constructor
End Sub
Public function CountTheWhatever(byval Counter as integer)
return Counter + 1
end sub

To use you say
public sub WhatIWantToDo as TheClassIWantToUse

You can than use it as.
counter = WhatIWantToDo.CountTheWhatever(1)

I hope this gives an idea

Cor
 
J

Jan

Cor or Goran,

After your explanation about the dangereous Shared, i tried the same with a
normal class, but this doesn't work.
So i have the same question: how to use a normal class in an aspx file?

in aspx file, i use a (non-class) function like this:

<asp:Literal ID="lit" runat="server" Text='<%# myfunction(Eval("myfield"))
%>' />

i tried this: <%# descr.myfunction(Eval("myfield")) %> but of course, it
fails ...

my class is now:
---------------
Public Class descr
Public Function myfunction(ByVal myvar As Object) As String
.....
Return xyz
End Function
End Class
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

There is nothing dangerous with a shared method.

If you would use shared variables in the method, that would be something
different, but that would be dangerus because of the shared variable,
not at all because it's done in a shared method.

There are a lot of shared methods that you use all the time, like
Int32.Parse, DateTime.Now, String.Concat or Math.Round. There is nothing
dangerous about them.
 
C

Cor Ligthert [MVP]

Goran,

I had not the idea that we were talking about a shared methods. As well not
to a static variable.

We were talking about a shared Class were I especially wrote about module.

If you are used to the behaviour of a Shared Class/Module in a windowsform
situation than a Shared Class/Module is dangerous to use in ASPX.

I know at least one person who was in past writting the same as you and is
now warning every time for that.

Cor
 
C

Cor Ligthert [MVP]

Jan,

At least you have to instance it (construct it), in my message I forgot the
"New" word sorry for that

Your code will than be
dim mydescr as new descr.

Cor
 
J

Jan

Thanks.


Cor Ligthert said:
Jan,

At least you have to instance it (construct it), in my message I forgot
the "New" word sorry for that

Your code will than be
dim mydescr as new descr.

Cor
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

Cor said:
Goran,

I had not the idea that we were talking about a shared methods. As well not
to a static variable.

If you have no idea what the question is about, then perhaps you should
not reply to it.

The advice you give is irrelevant to the situation as you are talking
about something completely different.
We were talking about a shared Class were I especially wrote about module.

No, _you_ were talking about a shared class. There is nothing about a
shared class in the original post.
If you are used to the behaviour of a Shared Class/Module in a windowsform
situation than a Shared Class/Module is dangerous to use in ASPX.

There is nothing dangerous about a static class or static methodss.
Using static variables is dangerous, but that is the same regardless if
they are in a static class or not.
I know at least one person who was in past writting the same as you and is
now warning every time for that.

Cor
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

You don't need to create an instance of the class to access shared
methods, and there is no reason to make it an instance method.

Actually the method should be shared. Making it an instance method
implies that the method is using data from the class, which it doesn't.
 

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

Top