question about classes

A

ajaymehra

Hi all,
I have a class file called Properties.vb in which I define a publi
property as follows
-----------------------------------------------------------
Public Class Properties
Private CalSelectedDate As Date = Now()

Public Property CalSelDate() As Date
Get
Return CalSelectedDate
End Get
Set(ByVal Value As Date)
CalSelectedDate = Value
End Set
End Property

End Class
-----------------------------------------------------------

Now I have Page1.aspx where I create an object of Properties class an
set the property value.
I also have Page2.aspx where I create another object of Properies clas
and try to access the property value - But I dont get it.

Im guessing this is because each object I create has its own memor
space...
So how would I be able to accomplish this ? How do I get values fro
another class ?

TIA,
A

ajaymehr
 
C

Chris Jackson

Every instance of a class (object) contains its own copy of each member
variable. If you are navigating to Page 2, Page 1 is already gone, and you
can't get to it any more. If you try to just create a new instance, you get
exactly that: a new instance. If you want to pass information back and
forth, you should try using a Session variable, using Server. Transfer, or
other such means to preserve the state that you need to access from the next
page.
 
J

Jim Cheshire [MSFT]

Ajaymehra,

It sounds like you are expecting that IIS will hold an instance of that
object in memory for as long as you need it. In fact, that's not what
happens. When you instantiate that object, you can use that object until
the page class is destroyed. That happens when the page is sent to the
Response stream. If you want to access the instance you create on Webform1
in another page, you have to explicitly persist that instance using Session
state, Viewstate, or some other method.

Jim Cheshire, MCSE, MCSD [MSFT]
ASP.NET
Developer Support
(e-mail address removed)

This post is provided as-is with no warranties and confers no rights.


--------------------
NNTP-Posting-Date: Thu, 19 Feb 2004 13:28:34 -0600
From: ajaymehra <[email protected]>
Newsgroups: microsoft.public.dotnet.framework.aspnet
Subject: question about classes
Date: Thu, 19 Feb 2004 13:41:01 -0600
Message-ID: <[email protected]>
Organization: MCSE.MS forum
User-Agent: MCSE.MS news gateway
X-Newsreader: MCSE.MS news gateway
X-Originating-IP: 167.89.2.5
Lines: 26
X-Trace: sv3-RuVh3tOqYkjiolpygEWMy10+aew0zenSGXC4c6YnqScfCPTLVDR3N2Ex3Y4cQPgjA9J93c1g
0fvDTCb!XsvOj0yFkFsyRFP9bXi6PT3tJMTUZQkYTomnQAW46Bu25B6xIWk=
X-Complaints-To: (e-mail address removed)
X-DMCA-Notifications: http://www.giganews.com/info/dmca.html
X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers
X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly
X-Postfilter: 1.1
Path: cpmsftngxa07.phx.gbl!cpmsftngxa06.phx.gbl!cpmsftngxa09.phx.gbl!TK2MSFTNGP08.
phx.gbl!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!ne
wspeer1-gui.server.ntli.net!ntli.net!newspeer1-win.server.ntli.net!border1.n
ntp.ash.giganews.com!border2.nntp.sjc.giganews.com!border1.nntp.sjc.giganews
.com!nntp.giganews.com!local1.nntp.sjc.giganews.com!news.giganews.com.POSTED
!not-for-mail
Xref: cpmsftngxa07.phx.gbl microsoft.public.dotnet.framework.aspnet:211529
X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet

Hi all,
I have a class file called Properties.vb in which I define a public property as follows
-----------------------------------------------------------
Public Class Properties
Private CalSelectedDate As Date = Now()

Public Property CalSelDate() As Date
Get
Return CalSelectedDate
End Get
Set(ByVal Value As Date)
CalSelectedDate = Value
End Set
End Property

End Class
and try to access the property value - But I dont get it.
Im guessing this is because each object I create has its own memory space...
So how would I be able to accomplish this ? How do I get values from another class ?

TIA,
AJ


ajaymehra[/QUOTE]
 
K

Kevin Spencer

Now I have Page1.aspx where I create an object of Properties class and
set the property value.
I also have Page2.aspx where I create another object of Properies class
and try to access the property value - But I dont get it.

Im guessing this is because each object I create has its own memory
space...
So how would I be able to accomplish this ? How do I get values from
another class ?

Get them from the class instance that you set the property in. A class
definition is just that: It defines the structure of a class, but is not an
instance of the class, just a blueprint if you will. Using the New Operator
or any other method that returns an instance of that class creates an
instance of the class. And you can create as many instances of a class as
you like. If the class definition is a blueprint, the class instance is the
house. You can build many houses from a single blueprint.

Now, I believe that the real issue you're dealing with is, how do you access
a class that was created in another Page? The answer is not all that simple,
as there are many ways, depending upon how that second Page is arrived at.
For example, if you do a Server.Transfer, you can literally transfer the
class instance to the page being transferred to. On the other hand, more
often than not you're likely to want to store the class instance in a cache
of one sort or another, such as SessionState or Application Cache. Where you
store it depends on how much accessibility it needs. If you store it in
Session, it is available to all Pages seen by a single user. If you store in
Application Cache, it is available to all pages seen by all users.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 
A

ajaymehra

Thanks all of you for clearing up my concepts.
Kevin - That blueprint...house example was excellent.
Thanks again

Kevin said:
*> Now I have Page1.aspx where I create an object of Properties clas
and

Get them from the class instance that you set the property in.
class
definition is just that: It defines the structure of a class, but i
not an
instance of the class, just a blueprint if you will. Using the Ne
Operator
or any other method that returns an instance of that class create
an
instance of the class. And you can create as many instances of
class as
you like. If the class definition is a blueprint, the class instanc
is the
house. You can build many houses from a single blueprint.

Now, I believe that the real issue you're dealing with is, how do yo
access
a class that was created in another Page? The answer is not all tha
simple,
as there are many ways, depending upon how that second Page i
arrived at.
For example, if you do a Server.Transfer, you can literally transfe
the
class instance to the page being transferred to. On the other hand
more
often than not you're likely to want to store the class instance in
cache
of one sort or another, such as SessionState or Application Cache
Where you
store it depends on how much accessibility it needs. If you store i
in
Session, it is available to all Pages seen by a single user. If yo
store in
Application Cache, it is available to all pages seen by all users.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.

ajaymehr
 
J

Jim Cheshire [MSFT]

He could also use a Shared instance.

Jim Cheshire, MCSE, MCSD [MSFT]
ASP.NET
Developer Support
(e-mail address removed)

This post is provided as-is with no warranties and confers no rights.

--------------------
 

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