The old Structure/Class Argument

Z

zacks

I recently wrote a VB.NET application using VS2005. I needed to be able
to build linked lists in memory of items that were represented by a
group of variables. For example,

Friend Structure foo
Dim var1 As String
Dim var2 As Integer
Dim var3 As Boolean
End Structure

I needed several structures, each with a different mix of number of
members and the datatypes. Now, these structures could have been
implemented as classes. But I do not know how multiple instances of a
class can be linked together so I can process them in a standard For
Each/Next loop.

I see one guideline of whether to use a Class or a Structure is if the
Structure is less or equal to 16 bytes. Several of these structures
will be larger than that, so I am open to converting the code to use
classes instead of structures if someone can give me some pointers on
how to link instances of a class together.

TIA,
 
M

m.posseth

Well if i see your structure i would say just leave it as a structure as it
would only hurt the performance of your app to convert this to a class

Basically, classes are reference types and structs are value types.
Reference types get stored differently than value types. Value types are
stored more efficiently on the stack vs classes on the heap using structures
will also save the GC some trips :) as it does not need to reclaim memory
want to know in detail when to use a class or when to use a structure ???

watch this video
http://msdn.microsoft.com/netframework/programming/classlibraries/richtypesystem/


regards

Michel Posseth [MCP]
 
Z

zacks

I am. A collection of structures.

Sorry, I forgot to mention that.

I would prefer to use multiple instances of a class to emulate a
collection but I can't figure out how to link the multiple instances.
 
Z

zacks

Is a List better than a Collection?

You mention that you would use a class, but in your example code you
used a structure. Can you:

Class foo
<define each member of the former structure as a property>
End Structure
Private mylist As New List(Of foo)

(Thanks for the pointer to the new List class, anyway. I will research
it)
 
C

Cor Ligthert [MVP]

You mention that you would use a class, but in your example code you
used a structure.
(To integrate your sample)

Can you:
Class foo
<define each member of the former structure as a property>
End Structure
Private mylist As New List(Of foo)

(Thanks for the pointer to the new List class, anyway. I will research
it)

Yes, Yes (however not end structure but end class) see the link I gave
there is very much information.

I hope this helps,

Cor
 

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

Top