Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VB.NET: is using Structures considered nasty?

Tags:

vb.net

I use to use Structures quite a lot in the VB6 days, and try to avoid them now with .NET. Just wondering if using structures in 2010 instead of a Class is considered nasty?

Thanks for the help.

like image 997
Alex Avatar asked Oct 10 '10 00:10

Alex


1 Answers

Choosing a Structure takes consideration instead of being inherently "nasty". There are reasons why a Structure can be nasty; however there are also reasons a Class can be nasty in its own way...

Basically when you decide between these two object oriented kinds of containers, you're deciding how memory will be used.


There are different semantics associated with Structure and Class in VB.NET and they represent different memory usage patterns.

By creating a Class you're creating a reference type.

  • good for large data
  • memory contains a reference to the object location on the heap (like the concept of pointing to an object) though happens transparently to the VB.NET programmer because you're in "managed mode".

By creating a Structure you're creating a value type.

  • good for small data
  • memory allocated contains the actual value
  • be judicious because these are apt to get pushed on the stack area of memory (i.e. for local vars, but not class fields) - too large and you could run into stack issues.

Also some good video resources on YouTube if you're an audio learner.

Many articles on the Internet like these MSDN articles to teach the basics and details:

  • Value Types and Reference Types
  • 7.1 Types - Reference and Value
  • MSDN Type Fundamentals - subheading: Reference and Value Types

Example
alt text

like image 99
John K Avatar answered Oct 13 '22 23:10

John K