2

This is not a subjective question; I am mainly asking to see if structures are now deprecated or something in VB.NET.

It is also not generally a duplicate of a question asking when to use a structure or a class, as this is largely checking to see if such information has become outdated. Furthermore it is certainly not a duplicate of questions relating specifically to C#, as these are two different (albeit similar) languages. The difference between classes and structures is language-dependent, as can be demonstrated by VB.NET and C++.

In Visual Studio 2012, when creating a new VB.NET file, you get options for Module and Class, among other things, but there is no option for Structure. For instance:

enter image description here

If you simply select to add a new item, then the much more complete menu doesn't list it either:

enter image description here

This seems like an awfully big oversight, especially when there are meaningful differences between classes and structures in VB.NET, so I'm certainly suspicious that it's not really an oversight at all.

Are structures a deprecated practice now? Has the language been revised in some way that has made the difference between a structure and a class much more meaningless? Is there any technical or widely-held convention that I am unaware of here? Or is it just an oversight after all? Thanks.

EDIT

To make a long story short, my understanding is that, among one or two other things, structures tend to be more efficient for smaller amounts of code, and classes tend to be more efficient for larger amounts. This is because of differences between they ways that their memory is managed. Even though a lot of people always think in terms of classes in a language-agnostic kind of way, I thought there was a practice among fluent VB.NET developers to use structures as well.

Panzercrisis
  • 4,590
  • 6
  • 46
  • 85
  • 1
    classes and forms are items which generally at least start out in their own file because they will have related code in that file. not so with a struct which is closer to a variable than a class, report, usercontrol objects. Generally, unless you must use a struct for legacy reasons, a Class will work in its place. – Ňɏssa Pøngjǣrdenlarp Jun 05 '14 at 13:49
  • Structures are passed by value, classes are passed by reference. As a result, classes are much more flexible, when writing code. I cannot give an example where use of Structures would be justified. For this matter, I'd say yes, they are deprecated. – Victor Zakharov Jun 05 '14 at 13:55
  • possible duplicate of [When to use struct in C#?](http://stackoverflow.com/questions/521298/when-to-use-struct-in-c) – Victor Zakharov Jun 05 '14 at 13:59
  • @Neolisk Structs are FAST. You can instantiate them very quickly. I like to use them when I need a pure object...just a construct that holds data, and has no methods (which isn't very often, granted) – Russell Uhl Jun 05 '14 at 13:59
  • Also, you can do cool things like byte offsets. I'm not sure those are possible with standard classes. – Russell Uhl Jun 05 '14 at 14:00
  • 3
    @RussellUhl: I've spent a lot of time optimizing performance in enterprise class applications. Structs vs Classes is not the bottleneck. :) And because it can add headache, I'd avoid Structures altogether (paradigm consistency is key to maintainability). – Victor Zakharov Jun 05 '14 at 14:00
  • @Neolisk No, that's not a duplicate. – Panzercrisis Jun 05 '14 at 14:01
  • 1
    @Panzercrisis: Why do you think so? Structures are not specific to VB.NET language, that's why a link to C# question - explaining when you should use them (with credible links etc.). – Victor Zakharov Jun 05 '14 at 14:02
  • 1
    [Structures and Classes (Visual Basic)](http://msdn.microsoft.com/en-us/library/2hkbth2a.aspx) – Ňɏssa Pøngjǣrdenlarp Jun 05 '14 at 14:06
  • `struct` doesn't support initializers, so you can't assign member values, including bounds of arrays. no constructor unless declared `Shared`. similarly, a `Shared` member or module/class CTOR, is initialized but not actually 'constructed' until you use a member var/method for the first time. uses for struct? maybe to pass large sets of params defined at runtime. – porkchop Jun 05 '14 at 14:54
  • @Neolisk The difference between them and classes is language-specific though, and there are miscellaneous differences between VB.NET and C#'s type systems. – Panzercrisis Jun 05 '14 at 16:31
  • 2
    @Panzercrisis: I did not know that. But it makes a good reason **not** to use Structures. Instead of knowing language specifics, you learn platform generics (is there such a word?), and be able to program in any language that uses CLR. From what I know, Classes are more consistent in this regard. – Victor Zakharov Jun 05 '14 at 18:18
  • 1
    @Neolisk That's a valid point. – Panzercrisis Jun 05 '14 at 18:39

3 Answers3

2

No, structures are not deprecated. They have just never been on the Add Item list.

Which is probably because people haven't been willing to reserve a whole file for a single structure, preferring to put them in classes and modules. But you can if you want.

If you are concerned with class vs structure differences, you probably want to see Structs versus classes.

Community
  • 1
  • 1
GSerg
  • 76,472
  • 17
  • 159
  • 346
1

Just to add some more information... The type that you choose from the "Add" dialog only affects the initial template you get in the editor. It is perfectly valid to add a Class file, then edit it to turn it in to a structure, form, or even a module. Typically if you want to create something that isn't in the list you would choose "Code File" to get a blank document to customize as you want.

You can even create your own templates to add to that list. If you find yourself wanting to add a template for a structure you can do it fairly easily.

Here are some basic instructions on how to do that. http://msdn.microsoft.com/en-us/library/tsyyf0yh.aspx

Bradley Uffner
  • 16,641
  • 3
  • 39
  • 76
1

Some structure types like List<T>.Enumerator are used essentially the same way as objects, but a more common usage case for structures is as simple aggregate types which hold some data for the use of other types. The behavior of a type like KeyValuePair<TKey, TValue> is simply "Key and Value are properties of type TKey and TValue, which hold whatever outside code asked them to hold." While some companies' policies may require that every type reside within its own file and have its own associated documentation package, placing utility structures into a file with a package's static utility functions, static constants, etc. may make more sense than splitting them into separate files, especially if they don't have any substantial logic of their own.

Nothing is preventing a programmer from placing a structure into a file by itself, but the usage case was not considered sufficiently frequent to justify a special template for that purpose.

supercat
  • 77,689
  • 9
  • 166
  • 211
  • Stopwatch is a class, not a structure: http://referencesource.microsoft.com/#System/services/monitoring/system/diagnosticts/Stopwatch.cs#ceb0ba9cc88de82e – Chris Dunaway Jun 06 '14 at 16:24
  • @ChrisDunaway: Oops. I must have it confused with something else I which I had expected to be a class and turned out to be a struct. Oh well--`List.Enumerator` is not the thing I'd been thinking of, but I think it fits the bill of something which pretends to be an object and is used like one, even though it isn't. – supercat Jun 06 '14 at 17:55