Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

class problem

Status
Not open for further replies.

VBmim

Programmer
Jun 25, 2001
361
0
0
BE
Hello

I have a little designer question for you all...

when you want to make class-function you can do it in two ways:

*
Code:
public function AddArticle(byval Artno as string, byval Price as double)

'add article in article database with data given in 
'parameters of the function

end function
*
Code:
'code in exe
dim article as clsarticle

article.artno = 'x1234'
article.price  = 12.95
article.addArticle()

'code in class
public function AddArticle()

'add article in article database with data store in the
'properties of the instance of the class

end function

Both these methods work without problems, but I would like to know which one is the best (or which one you prefer)

greetz

mim
 
I prefer the second method. Imagine that you used your first method and at a point in time an article should have a Color property besides Artno and Price. This means that not only you have to change your function, but all the code where you make a call to your function.

When you use the second method, you can add the Color property and the AddArticle function and you're done. The function call stays the same.

Greetz,
Jan If this response was usefull to you, please mark it with a Star!
 
At first site, I like the second one also, because if you have a lot of parameters to pass through (in our example this would be 'location', 'taxcode', 'description short', 'description long' and so on and so on) it could end up in a very long function call (if the first method is used)... but when I think about it a little more, the first one seems better. If you compile all your classes in a dll (so that other programmers can use them) it is not clear what the function wants from you if you don't give parameters...
I just want your opinion on all this....
 
That is correct.
But Intellisense gives you the same information. (when you type 'myObj.', you get a list with all the properties and methods). You make your class so that is has default values where possible and some checking before saving if all important fields are set. If not, you can raise an error. If this response was usefull to you, please mark it with a Star!
 
[smarty]There quite a lot of theory about this. Search for "Design by Contract" and "encapsulation" on the web.

First of all, it is considered bad practice to make properties (= variables) of a class public. Each class has a responsibility. It has to maintain a usable state, which can be relied upon by other classes. Let me give you an example. Say article "A0001" already exists in your database and therefore cannot be saved.

Encapsulated:

Code:
obj.addArticle("A0001",10.05)
will fail and
Code:
obj.getLastAddedArticle()
will return the last added article.

Without encapsulation:
Code:
obj.articleNum="A0001"
obj.price=10.05
obj.addArticle()
will now also fail, but it leaves the object in an inconsistent state, as the price is not associated with the article (article probably has a different price in the database). This may lead to hard-to-solve errors.

Hope this helps or amuses
 
One other point to mention is when designing classes for COM components, for use in Web Applications, you would prefer the 1st method.

This is because it only makes one server call as apposed to multiple server calls if you had Class Properties like in the second method.

Secondly if you require the COM component to work under MTS, the 1st method makes the COM Object "Stateless". This allows the COM object to be "pooled" and used more efficiently.

As a result classes have different design requirements depending upon there potential usage.

HTH,

Codefish

 
Hi,

Actually, neither of the two methods is overly effective. The overhead of passing so many arguments is detrimental to the app's performance. One way is to use a defined user type for the values to be passed. This is a better method than the above two methods but still not a great way of doing it.

I would suggest using a property bag object and then serializing the data. A byte array is extremely fast across a network, it is only one argument and can be used easily.

Have a good one!
BK
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top