simple(?) problem with use of classes

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a web application that collects user input on a page. This is then
processed separately in a class. I want to return the values created in the
class to another web page. I can get this to work fine if I return the
values created in the class to the original page, but I can't get this to
work when I try with the second page.

To illustrate the problem, I have a sample pag e with 2 text boxes, a label
and a button. The user puts some numbers in each of the text boxes. When
the button is clicked, the class adds together the text box values, and the
label displays the result:

On the input page:

Dim class1 As New Class1

Dim x As Single
Dim y As Single

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
x = CSng(TextBox1.Text)
y = CSng(TextBox2.Text)

class1.DoAdd(x, y)

Label1.Text = CStr(class1.getTheAnswer)
End Sub

The class code:
Public Class Class1

Private thesum As Single

Public Sub DoAdd(ByVal x As Single, ByVal y As Single)
thesum = Val(x + y)
End Sub

Public Function getTheAnswer() As Single
getTheAnswer = thesum

End Function

End Class

If I try to, for example, change the button click event to go to a second
page and display the class.getTheAnswer on a label on it, I get an 'reference
to a non-shared member requires an object reference' error in the code:

(the second page code)
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

lblTest.Text = CStr(class1.getTheAnswer)
End Sub

I presume that I am guilty of some fundamental lack of knowledge, but would
really appreciate some help with this. Thanks.
 
TattyMane,

An Webpage application is stateless, that means that a page start always
completly clean. (This is completly different than with a windowform
application)

So when you use a class to holds your information, it is clean when opened
by the second form. You can use a so called shared class, however that
belongs to all users (It belongs to the application) and means that you are
almost have to duplicate the Session functionality in it.

Better is to use for this the Session which belong to the user which has
started the session.

Session.item("MyItem") = "Whatever"

myItem = Session.item("MyItem")

As a more general note,
In my opinion there are 2 better newsgroups for this kind of questions.
microsoft.public.dotnet.framework.aspnet
and
microsoft.public.dotnet.languages.vb

When you do not know (you will see that in a while) which newsgroup is the
best for your question, crosspost it. (crossposting is sending one message
to more newsgroups in one time). That is no problem in this newsgroups,
while multipost, sending to more newsgroups one by one is hated.

I hope this helps?

Cor
 
Thanks Cor.

I suspected that it was something fairly basic that I had come up against.
The idea of using a class was to separate the processing logic from the
presentation layer of my application. But this doesn't look possible, which
is a pity.

Thinking about it, I guess the 'trap for young players' in this is the way
that a single page utilising a class appears to work quite fine, whereas I
suppose that if 'viewstate' wasn't enabled, it would not seem to work quite
so well.

I shall use session variables to move the user input between pages, I suppose.

Thanks also for the advice about where to post this sort of query, and
associated etiquette.

Regards.
 
Back
Top