Object scope issue...

B

Brad Pears

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
 
S

Scott M.

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
 
C

Cor Ligthert[MVP]

Brad,

In addition to Scott his message, as you are programming OOP then first try
to set things in the methode itself, then globaly in a class and as last in
a module.

Setting it in a module means that it is forever in the main part of your
program, globaly means that it is forever in your instanced class and in a
method means that it will be disposed by the garbadge collector as soon as
the method ens and there is no reference to or from it anymore (this with
the exception of the static declaration in VB)

Don't think about overhead of creating variables everytime, there is OOP
build around.

Cor
 
R

RobinS

How is your business object set up? When you say the user clicks to save a
row, are you using a datagridview, or do you mean just to add a new object?

Have you checked out Deborah Kurata's book "Doing Objects in VB2005" ? It
talks about how to handle the state of an object, and how to handle updates.
Great book.

RobinS.
GoldMail, Inc.
 
B

Brad Pears

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
 
S

Scott M.

Again, instead of explaining what you are doing, we need to see your actual
code.

Also, I really think you should consider using DataSets to manage all of
this, rather than re-inventing the wheel.
 
C

Cor Ligthert[MVP]

Scott,

In the way the reply from the OP is, he is not using wheels, he wants to
ride a boat on the highway.

Cor
 
B

Brad Pears

Hi there...

When the user clicks to save, I am actually running a stored procedure that
inserts a row in our SQL Server db. I am not using a datagridview. In fact I
have never used one yet. Do they actually work well becasue I may have a
need for that in the application I am currently developing.

When the user clicks the "Save" button on the form, I then first check to
see if a parent class has already been instantiated. If it has not, I
instantiate the "parent" class. If it has already been instantiated (and
this is where I was having the problem) then I do not need to create a new
instance. Then, I call the parent objects "update" method. This update
method - does the insert (or update) into the database for the parent table
and then also instantiates a related "child" class and calls it's update
method as well to store some child data.

I think I may have it figured out now... I have created a public variable
at the form level because it is here that I will need to instantiate the
object and possibly persist it for a while. I realized that I was actually
"destroying" the parent object in the code at the end of the Save buttons
"click: event (as part of the clean up) - so I wasn't getting desireable
results.

I will have to check that book out...

Thanks, Brad
 

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