473,385 Members | 1,192 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

simple(?) problem with use of classes

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.

--
one door shuts, another closes . .
Jul 21 '05 #1
2 1172
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
Jul 21 '05 #2
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.

"Cor Ligthert" wrote:
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

Jul 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Alan Clark | last post by:
Dear All I need to do something very simple with Javascript and have been looking all over the web for two days for a suitable script. I'm the kind of person who learns by seeing how it's done....
2
by: Marek | last post by:
Actually, the very first question I should ask is "does it make sense at all?". For 2 days I've been trying to generate C# classes using XML schema and xsd.exe tool. So far the results are not...
1
by: William Sullivan | last post by:
I'm investigating using schemas to create classes for a project I'm working on. For simple classes, this works well (except for making all the instance var's public). However, the class I want to...
4
by: Coldman | last post by:
hi, IIS 5 and 6, IE 5 and 6, simple authentication does the browser send the username and password in clear text on every request after been authenticated?
0
by: Aristotelis E. Charalampakis | last post by:
Hi all, I have been studying various sample projects about multilingual winform applications, and I have figured out most of it. I have started migrating some code from vb6 to vb.net and I am...
12
by: Guyon Morée | last post by:
Hi all, I'm using simple classes as a container of named values and I'm instantiating a lot of them in a very short time. i was wondering if there is any benefit in using dicts instead from a...
7
by: toton | last post by:
Hi, I have a STL vector of of characters and the character class has a Boost array of points. The things are vector<Characterchars; and class Character{ private: array<Point,Npoints; }; Now...
11
by: utab | last post by:
Dear all, I have a simple template to convert its argument to a string: template<typename T> string AtoStr(T t){ ostringstream sstrm; sstrm << t; return sstrm.str(); }
11
by: eBob.com | last post by:
I have this nasty problem with Shared methods and what I think of as "global storage" - i.e. storage declared outside of any subroutines or functions. In the simple example below this "global"...
6
by: Tony | last post by:
Hello! Below I have a complete working program.with some simple classes one of these is a generic class. Now If I want to implement functionallity so I can compare animal with each other or...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.