object orientation question

  • Thread starter Thread starter Big D
  • Start date Start date
B

Big D

Hi all,

I have a general question about the best way to go about object orientation
as it relates to representing data in a database (or other source for that
matter).

Lets say I have some class "foo" that is a piece of business info. I can
populate an instance of foo (let's call it "myFoo") by sending it, let's
say, the id of a record in a database:

Dim myFoo as new Foo(433)

The new constructor goes out and gets the mess of info for this object.
This is great.

However, what if I want to make a new record of the foo type, but there are
properties of Foo that are readOnly? For example you COULD do something
like:

Dim myFoo as new Foo()
myFoo.Item1 = 3
myFoo.Item2 = 4
myFoo.insert()

and the "Insert" procedure inserts the new data into the database. But what
if myFoo.Item2 should really only be readOnly? This seems to pop up alot,
where there is a piece of information for an object that should not be
modified if it is already existing, but if you are making a new one you need
write access to it.

You can have "wrappers" for private members of the properties that are read
only so you could have something like:

Dim myFoo as New Foo()
myFoo.Item1 = 3
myFoo.ChangeItem2(4)
myFoo.Insert

or you could create a shared (static) function that returns an instance of
foo to work with like:

Dim myFoo as Foo
myFoo = Foo.CreateNewFoo(3,4)

But that seems a little wierd (?), particularly if there are a lot of items
you need to pass to create the object.

Is there a "best practice" for this sort of thing?

Thanks so much for sticking it though this long winded post!

-MC D
 
This example of an existing Foo:
Dim myFoo as new Foo(433)

And this example of a new Foo:
Dim myFoo as new Foo()
myFoo.Item1 = 3
myFoo.Item2 = 4
myFoo.insert()

would access two different constructors... so you can assume that if you are
using the first constructor that the object has not just been created. If
you set a flag to indicate if the object is new you can set it to True in
the second constructor and False in the first one. If you have a "write
once" property like Item2 you check the flag and permit it to be assigned so
long as the flag is set.

Tom Leylan
 
Yes, they are two different constructors, but they are the same class, so if
foo.Item2 is readOnly, then what is the appropriate method to create a foo
and define item2? A "wrapper" method that just modifies the private member
that the property reads from, a constructor that takes in item2, or a shared
method that returns an instance of foo, or is it all just personal taste?

Thanks for the response!

-D
 
No wrapper... perhaps I wasn't clear.

You can have multiple constructors... one that is fetching the values as in
your example Foo(433) this would make the Item2 property readonly. The
other is Foo() and this would make Item2 read/write.

You can post your class if it isn't too huge... if it has a bunch of stuff
not relevant to the example just yank those properties/methods out.

Basically you would add one more instance variable named something like mNew
which is Boolean. You would set it to True when the Foo() constructor was
used (from within that constructor) and you would set it to False when the
Foo(433) constructor was called, again from that constructor.

You have a private member variable named mItem2 and you have a read/write
property named Item2. In the "Set" part of that property you check to see
if mNew is set to True and if it is you let the value be assigned to mItem2.
If it is False then you don't.

It acts like a read/write property until it is saved and fetched using the
Foo( value ) format. At that point it is read-only.
 
Back
Top