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
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