|
-
Jan 13th, 2005, 12:52 PM
#1
Thread Starter
PowerPoster
gets null!
hi there. using vb.net here with asp.net
basically, i have made a .vb file. this file has public properties that can get or set values
i have 2 aspx pages
both create an instance of this public property file.
one, sets the values as required, the other one needs to retrieve it based on a unique number given to it in the querystring.
however when it tries to do that, i get a nullreference exception. i am guessing its because i created a new instance - each instance is its own instance/space therefore when accessing a new instance of a class, it cant get any values - because nothing has been set in it!
is this right? if so - how can i resolve it?
thanks
-
Jan 13th, 2005, 04:26 PM
#2
Addicted Member
Re: gets null!
In your class, just add a default constructor like so:
VB Code:
Public Sub New()
Me.Code = nothing
Me.Name = nothing
End Sub
So when you create a new instance of the class (Dim aClass as new cClass),
it receives those default values.
You can then search for your object with the unique number, and make that new instance equal to that object.
Is this what you are talking about ?
-
Jan 13th, 2005, 04:29 PM
#3
Thread Starter
PowerPoster
Re: gets null!
um - i think so
basically:
1 .vb file - this file will be shared, so any aspx page can access it and give it back any values stored in each object created in this vb file
x number of aspx pages, one can construct an object via .vb file, another can access an objects properties....
?
-
Jan 13th, 2005, 04:38 PM
#4
Addicted Member
Re: gets null!
Yeah seems fine 
All the objects of a specific class that I create I store in a collection, so once you create your one object on the first aspx page, chuck it into the collection to store across requests from the client and then its easy to search for that object and pull it out of the collection from the other aspx pages.
-
Jan 13th, 2005, 04:39 PM
#5
Thread Starter
PowerPoster
Re: gets null!
sweet - ill give it a bash thanks a bunch!
ASP.NET ROX!
-
Jan 14th, 2005, 10:06 AM
#6
Thread Starter
PowerPoster
Re: gets null!
sorry, i completly forgot and lost it.
how is this going to work? how can i create an object from one aspx page, store it, and let another aspx page(s) access that object on a UID?
-
Jan 14th, 2005, 11:22 AM
#7
Addicted Member
Re: gets null!
Well what you can do is once you have created your object, you can add it to your Session state (Session.Add("NameOfObject", aObject)) or Application state (Application.Add("NameOfObject", aObject)). This will store your object across requests. If you want the diff between the two go search on google :P
Then on the next aspx page your can get your object out of that state:
VB Code:
Dim aObject as new ClassName = ctype(session.item("NameOfObject", ClassName)
There are many other ways to do it, but this is just a simple way for storing a small amount of data.
-
Jan 14th, 2005, 11:41 AM
#8
Thread Starter
PowerPoster
Re: gets null!
thanks
-
Jan 17th, 2005, 06:36 AM
#9
Thread Starter
PowerPoster
Re: gets null!
well it doesnt seem to want to accept that
after the "=" in that code, vb.net complains that it should have expected an end statement
-
Jan 17th, 2005, 09:59 AM
#10
Addicted Member
Re: gets null!
sorry
VB Code:
Dim aObject as new ClassName
aObject = CType(Session.Item("NameOfObject"), ClassName)
-
Jan 17th, 2005, 10:26 AM
#11
Thread Starter
PowerPoster
Re: gets null!
thanks in the mean while i figured out another way but still have a problem
the main sign up page:
dim theSession as new sess 'Global variable on top of page
---------------------------
theSession.createUser(theSession.getSessID, txtusername.text)
Session("users") = theSession
..
..
theLoginPage:
dim theUserObject as sess
theUserObject = CType(Session("users"), sess)
etc...
great, works
but the problem happens, when you create another user. the session id is overwritten and the userobject is just created as a new object, or rather theSession object is recreated as a new object, therefore any old references are erased from memory
how do i avoid this?
thanks
-
Jan 17th, 2005, 03:12 PM
#12
Addicted Member
Re: gets null!
Well what you can do is create a Collection, and store your session objects that you create in there. So instead of storing the object in the session, store the collection which can hold many objects.
VB Code:
Dim sessionCollection as New Collection
sessionCollection.Add(thesession, thesession.ID)
Session.Add("sessColl", sessionCollection)
Then when you want to retrieve the object, get it out of the collection using its ID as the unique identifier.
By the way, if you are trying to make your own session objects, why dont you just use the ASP.NET Session object, and store your users' values in there?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|