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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

classes and structures

Status
Not open for further replies.

fenris

Programmer
May 20, 1999
824
CA
Say I have a structure:

private type foo
'various attributes defined within

end type

Would this be better as a class? I guess what I am getting at is the use of classes more efficient in VB then using structures (speed wise)?


Troy Williams B.Eng.
fenris@hotmail.com

 
Hi fenris,
Classes are actually a little slower than structures.

Rich
 
fenris -

Like Rich S. says, classes are slower than structures. But they have the benefit of being more than just a place to hold data. An example is that of using a class to represent a bank account. You'd have data stored inside it (current balance, type of account, account holder's name & address, etc). But you'd also have methods to deposit and deduct an amount from the account. The class would raise an event when someone tried to withdraw more money than it actually held. Or deposited more than $10,000 at one time (the IRS and DEA want this info).

Of course, you could also raise an event on a successful withdrawal, too. This could be used to notify a higher-level class (like BanksReserves) so that the bank officers could track how much cash they have on hand (the Federal Reserve requires banks to keep a certain percentage of their deposits in cash money for use by customers making withdrawals).

Chip H.
 

Chip,

Now that we're talking about classes, I just started a thread in the DCOM forum asking for advise. Please check it out. This is also an invitation to anyone interested and involved in n-tier design.
Tarek

The more I learn, the more I need to learn!
 

Oops, I forgot!

Fenris,

I wouldn't use a class just to store data. As Rich mentioned it is slower as it requires more resources to create and destroy each instance of the class. As Chip mentioned, classes are more typically used to contain properties AND methods (sometimes events as well).

Classes can incorporate business rules and data validation so that the forms are not cluttered with that stuff. A simple example:

In an address, a State field may be entered as "ca" (without the quotes) but you have a business rule that says State must be caps. This could be easily done in the class and the form designer wouldn't be bothered by it. Additionally, we wouldn't have to rely on the form developer to remember to do this.

Bottom line, I agree with the other posters before me! :)
Tarek

The more I learn, the more I need to learn!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top