Reputation: 1291
I was reading a VB.NET code and there was the following:
Structure MyRecord
"some code here"
End Structure
Then he use it as if it is a Class. So, what is the "Structure" ??
IS it only in .NET ,or there are similar things in other language??
Something else,in the same code there are:
Dim num As integer=FreeFile()
what does that mean?,can we put a function in a variable in VB?
If we can, then what does that mean??
Upvotes: 0
Views: 1981
Reputation: 700562
1
A structure is used to define a value type, just as a class is used to define a reference type. However, a structure is more complicated to implement correctly than a class, so you should stick to classes until you really need to implement a value type. The structure in the example should probably also be implemented as a class instead.
There are structures in other languages, but they may be handled differently. In C++ for example a structure is used to define a type, and the usage determines if it's a value type or a reference type.
2
Yes, you can put a reference to a function (i.e. a delegate) in a variable, but that's not what that code does. It simply calls the function and puts the return value in the variable.
Upvotes: 2
Reputation: 13927
Via startVBdotnet.com:
Structures can be defined as a tool for handling a group of logically related data items. They are user-defined and provide a method for packing together data of different types. Structures are very similar to Classes. Like Classes, they too can contain members such as fields and methods. The main difference between classes and structures is, classes are reference types and structures are value types. In practical terms, structures are used for smaller lightweight objects that do not persist for long and classes are used for larger objects that are expected to exist in memory for long periods. We declare a structure in Visual Basic .NET with the Structure keyword.
Generally, I would suggest implementing a class instead of a structure. This way you can use inheritance and general Object Oriented Design later, if needed.
Upvotes: 1