PC Review Forums Newsgroups Microsoft DotNet Microsoft VB .NET The old Structure/Class Argument

Reply

The old Structure/Class Argument

 
Thread Tools Rate Thread
Old 13-01-2006, 03:47 PM   #1
zacks@construction-imaging.com
Guest
 
Posts: n/a
Default The old Structure/Class Argument


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,

  Reply With Quote
Old 13-01-2006, 03:57 PM   #2
tomb
Guest
 
Posts: n/a
Default Re: The old Structure/Class Argument

Use a collection

Tom

zacks@construction-imaging.com wrote:

>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,
>
>
>

  Reply With Quote
Old 13-01-2006, 04:00 PM   #3
m.posseth
Guest
 
Posts: n/a
Default Re: The old Structure/Class Argument


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/netframew...richtypesystem/


regards

Michel Posseth [MCP]

<zacks@construction-imaging.com> schreef in bericht
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,
>



  Reply With Quote
Old 13-01-2006, 04:02 PM   #4
zacks@construction-imaging.com
Guest
 
Posts: n/a
Default Re: The old Structure/Class Argument

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.

  Reply With Quote
Old 13-01-2006, 04:26 PM   #5
Cor Ligthert [MVP]
Guest
 
Posts: n/a
Default Re: The old Structure/Class Argument

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


  Reply With Quote
Old 13-01-2006, 04:37 PM   #6
zacks@construction-imaging.com
Guest
 
Posts: n/a
Default Re: The old Structure/Class Argument

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)

  Reply With Quote
Old 13-01-2006, 04:40 PM   #7
Cor Ligthert [MVP]
Guest
 
Posts: n/a
Default Re: The old Structure/Class Argument

>
> 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


  Reply With Quote
Reply



Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off