Re: Object scope issue...
- From: "Brad Pears" <bradp@xxxxxxxxxxxxxxxxxxxxx>
- Date: Wed, 6 Feb 2008 12:51:10 -0500
Hi Scott...
Specifically, in the Save buttons click event, I create a new instance of
the class object then call it's "update" method. Now in this particular
case, the way the data is entered, initially they are actually entering data
into two tables with the one click of the Save button. First, the "Parent"
data is saved (clsParentData) and then the related child "detail" data is
saved (clsChildData).
So, the "update" method on the Parent class, inserts or updates parent data
and then when that process has completed, it then creates an instance of the
child class (clsChildData) and calls it's "update" method which then either
inserts or updates the related "child" data into the db. Once again, this
processing is all done in the Parent class's update method.
Now, if the user clicks to "Save" the data and the inserts happen (lets say
there was no data to begin with) and then they want to change the detail
data right after that (maybe because they made a mistake), they make the
data change and click "Save" again. So what happens is the parent's "update"
method runs again - but this time I need to check to see if the parent
object still exists (the root of my qeustion), becasue if it does I do not
want to attempt to insert another parent.
What I was doing in my "Save" command button code was setting the parent
object that I created to "nothing" so of course each time this ran, it was
attempting to isnert another parent - and would fail. So I removed the "set
clsParentData = nothing" at the end of that code and added the clsParentData
as a public declaration in my vb module instead. Now of course I will be in
charge of determining when and where to actually destroy the parent object.
This seems to work ok now, but I am thinking that I should not be delcaring
this public class in the vb module. Where else would I declare it so that it
is always persisting until I explicetly destroy it? Would I do this in the
form?
PS... I am not really using datasets in this example. I am explicitly
calling SQL 2000 stored procedures in my code to do the data inserts/updates
etc... I only use datasets after calling an SQL stored procedure to get
data from teh db - I then fill the dataset with that retrieved data - then
work with the dataset after that.
Thanks, Brad
"Scott M." <smar@xxxxxxxxxxxxx> wrote in message
news:uzd%23j0EaIHA.4808@xxxxxxxxxxxxxxxxxxxxxxx
I have a few thoughts on this.
First, you seem to be asking about 2 separate things. The first is your
database issue, which forces me to ask if you are using a DataSet to store
your data. If so, a DataSet is already equipped to keep track of the
changes to the data and the type of changes that were made (add, delete,
modify). This makes knowing what action to perform, based on what was
done to the data a no-brainer.
Second, if you could just show us your code, we could see how your
variable scope is set up.
-Scott
"Brad Pears" <bradp@xxxxxxxxxxxxxxxxxxxxx> wrote in message
news:OMG8tNEaIHA.3940@xxxxxxxxxxxxxxxxxxxxxxx
I just have a question about how objects should be used in situations
where you want to preserve an object's state while performing some
particular function.
In my vb.net 2005 app, a user enters data and then clicks a "Save"
command
button to save the row. If the user simply changes existing data on the
form, (meaning a row already exists) I only do a database update.
However,
if the user was adding a new row I would have first created a new
instance
of the object and subsequently added the row data to the database.
I set up the object as a "public" variable in my main vb module like
this...
"public MyObj as clsMyObj"
Then in the "Save" command button code, I first check to see if MyObj =
nothing. If so, I then create a new instance of the object (MyObj=new
clsMyObj) and run code that inserts the new row.
If the user were to go back and modify some data and clicks the "Save"
command button again, I
would expect my code above (if MyObj = nothing ) to be false and I would
then just update the data as opposed to updating it.
Is this typically how one would control an objects scope - by setting as
a public variable and checking to see if
the object = nothing or object = something? If so than does this mean
that for any object that I need to preserve scope, I need to setup as a
public variable?
I am new to OO design etc... and I want to make sure that I don't have
copies of objects all over the place etc... It seems that there must be a
better way to do this?
Help!
Thanks, Brad
.
- Follow-Ups:
- Re: Object scope issue...
- From: Scott M.
- Re: Object scope issue...
- References:
- Object scope issue...
- From: Brad Pears
- Re: Object scope issue...
- From: Scott M.
- Object scope issue...
- Prev by Date: Re: .NET embeddable web server ???
- Next by Date: Re: Using Object Oriented Programming to revolutionize physics?
- Previous by thread: Re: Object scope issue...
- Next by thread: Re: Object scope issue...
- Index(es):