Recap of the previous sections of this article We saw about types, discussed about various types available and saw about structures. In this section of article we will see how the structures can be implemented. Defining a structure. Defining a structure in .Net is as simple as declaring a variable. Syntax for declaring a structure
Structure <STRUCTURE name>
………………….. ….………………. 'Member variables here
………………….. …………………..
End Structure
It is very similar to declaring a class, we can do most the thing which are possible in classes such as they can have fields, properties, constants, events, methods, constructors and finalization code. Rules governing the Structure definition We should follow these rules strictly when defining a structure,
- Structures must have at least one field or event declaration. (We cannot have a dummy structure block.)
For eg.,
'This structure definition is not allowed Structure DummyStructure
End Structure - By default, structure will have a parameter-less constructor (as classes
have). Which will be used for default initialization for variables. We cannot define a parameter-less constructor, but we can define a parameterized constructors. - Initialization of variables other than constants is not allowed, but they
can be done using the parameterized constructors.
For eg.,
Structure ExampleStructure 'Public intMem as Integer = 10 not allowed Public intMem as Integer Public Const intConst as Integer = 10 ' This is allowed Public Sub New(byval value as Integer) 'Parameterized constructor IntMem = value End Sub End Structure
- Structure objects are deallocated when they go out of scope gather that
being garbage-collected. - We should avoid defining Structures with public fields. (It is not the
rule that structures cannot have public members) Public data is faster to access because it avoids the overhead of method calls to get at the data, but it clearly breaks the encapsulation of the structure. - .Net doesn't allows the class or structures to inherit from a
structure.
Now, we will see a small example in structure. Now, I am going to built a structure which is going to store the file size.
Structure FileSize Public FsinMB as integer Public Sub New(byval value as integer) FsinMB=value End Sub End Structure
In the above example, I have declared the created the structure which will store the File Size, and I have also defined a constructor which integer parameter.
See all the Structure in VB.Net Part 1, Part II , Part III , Part IV,Part V
Please send your valuable feed back to sadhasivam1981@yahoo.com.
Visit http://sadhasivam.t35.com to know more about me.
|
No responses found. Be the first to respond and make money from revenue sharing program.
|