Home All Groups Group Topic Archive Search About

The old Structure/Class Argument

Author
13 Jan 2006 3:47 PM
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,

Author
13 Jan 2006 3:57 PM
tomb
Use a collection

Tom

za***@construction-imaging.com wrote:

Show quoteHide quote
>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,
>

>
Author
13 Jan 2006 4:02 PM
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.
Author
13 Jan 2006 4:00 PM
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]

<za***@construction-imaging.com> schreef in bericht
Show quoteHide quote
news:1137167240.183905.106040@o13g2000cwo.googlegroups.com...
>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,
>
Author
13 Jan 2006 4:26 PM
Cor Ligthert [MVP]
Zacks,

I assume that you a List wants of those.

http://msdn2.microsoft.com/en-us/library/6sh2ey19(en-us,VS.80).aspx

Although that I never use a structure and would use a class
\\\
Friend Structure foo
        Dim var1 As String
        Dim var2 As Integer
        Dim var3 As Boolean
End Structure
Private mylist As New List(Of foo)
///
I hope this helps,

Cor
Author
13 Jan 2006 4:37 PM
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)
Author
13 Jan 2006 4:40 PM
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