Structures Vs. Classes

  • Thread starter Thread starter pmclinn
  • Start date Start date
P

pmclinn

I've noticed that many programmers use classes to store data about such
things like:

Class Customers
.....Phone
....ID
....Address

End Class....

In VB.net, does a 'structure' make more sense for storing this type of
data, if your not going to be using functions/Proceedures and such?
Is this just a crutch of the old way of doing things or is there a good
reason to do this?

-Peter
 
pmclinn said:
I've noticed that many programmers use classes to store data about such
things like:

Class Customers
....Phone
...ID
...Address

End Class....

In VB.net, does a 'structure' make more sense for storing this type of
data, if your not going to be using functions/Proceedures and such?
Is this just a crutch of the old way of doing things or is there a good
reason to do this?

See (complete thread):

<URL:http://www.google.de/groups?threadm=8z%[email protected]>
 
Some programmers refuse to use structs because they believe that you cannot
say that you will NEVER need a function. Therefore if they just make it a
class to start off with, this will never be a problem later on. Truthfully
I had this problem in C++ when I had to modify some code to change a couple
of struct to classes for this very reason. Therefore I like to go with the
idea of making everything a class.
 
Peter,
In addition to the other comments (specifically the comments in the thread
Herfried gave).

Both Classes & Structures in VB.NET define a new type. Both types (Classes &
Structures) can have Functions, Procedures, Events, & Properties.

The primary difference between a Class & a Structure is that a Class is a
Reference type, it creates an Object (a value) that exists on the Heap.
While a Structure is a Value type, it creates a value that exists on the
stack or inline in another type. (A Class or Structure contains other
classes & structures).

A Class also supports Inheritance, a Structure does not (a Structure always
inherits from System.ValueType).

Where you can define:

Class Person
....Phone
...ID
...Address

End Class

Class Customer
Inherits Person

...Orders
End Class

Class Supplier
Inherits Person

End Class

Because Customer Inherits Person, Customer will have a Phone, ID & Address
via the Person class. Only Customers will have Orders. While Supplier will
also have a Phone, ID & Address via the Person class. Any routine that
accepts a Person object, you will be able to pass either a Customer or a
Supplier object.
Is this just a crutch of the old way of doing things or is there a good
reason to do this?
What is "this" referring to? You want to put your Functions, Procedures,
Events, & Properties inside either a Class or a Structure as this is
Encapsulation. Putting related Functions, Procedures, Events, & Properties
in a Module is encapsulation also, however its "weaker encapsulation " as
you can use those Functions, Procedures, Events, & Properties without
qualification. Encapsulation is one of the tenents of Object Oriented
Programming.

Hope this helps
Jay
 
Altman said:
Some programmers refuse to use structs because they believe that you
cannot say that you will NEVER need a function. Therefore if they
just make it a class to start off with, this will never be a problem
later on. Truthfully I had this problem in C++ when I had to modify
some code to change a couple of struct to classes for this very
reason. Therefore I like to go with the idea of making everything a
class.

Yes, at the very least a .ToString to ease debugging / writing to logs. ;-)

-- Mark
 
Hi,

To know when to use structures and when to use classes you should study the
subject more. Sometimes people moving to .NET get caught up in using
classes for everything at first and later come to realize and appreciate the
value of using Strutures too.

The user-defined type (UDT) in Visual Basic Classic has been improved and is
now a structure in .NET

Where they are appropriate, Structures can help with boxing and performance
issues.

There are very few structueral differences between Structure and Class
members. Some folks call Structures lightweight classes.

Both can have contains fields, properties, Subs and Functions. Both require
a constructor to create a new instance. Both inherit from Object. There are
some different rules about how to initialize instance data in a Structure
vs. a Class.

The key difference between structures and classes is that structures are
value types and classes are reference types.

Being value types strutures live on stack memory so in general work faster
than reference types that live on the heap. Structures are best used for
lightweight objects that contain relatively litte instance data OR for
objects that do not persist for long. As Herifriend's post points out, 16
bytes of data is

Classes are better for larger objects that are expected to live in memory
for extended periods of time.

Here are a few articles to study:

User Defined Data Types
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vblr7/html/vadatUserDefined.asp

Structures and Classes
http://msdn.microsoft.com/library/d...n-us/vbcn7/html/vaconStructuresAndClasses.asp

Structure Statement
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vblr7/html/vastmStructure.asp


Structure Declaration
http://msdn.microsoft.com/library/d...n-us/vbcn7/html/vbconstructuredeclaration.asp


--
Mike

Mike McIntyre
Visual Basic MVP
www.getdotnetcode.com
 
I read somewhere that if you are passing to a method a lot then you should
use classes because Structures, being value types, have recreate the
structure when passed to the method. Does this mean that the whole structure
is duplicated on the stack when passed to a method rather than a pointer to
the structure data?
 
Sorry, I hit the Post button to soon. In reading, the material you
suggested, it states that when assigning a structure to another variable, a
new structure is created. However, if the structure contains non-value
types, then only the pointers to the data within the structure seem to be
duplicated, not the whole data. For Example:

Private myStruct1 as structure
Private mydata as ArrayList
End Structure

Dim var1 as myStruct1
Dim var2 as myStruct1
var1.myData.Add("texting")
var2 = var1

var2.myData(0) = "new value"

Then var1.myData(0) now equals "new value"

Is this not the case?
 

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

Back
Top