Conclusion

 <  Day Day Up  >  

Defining new classes and structures that represent complex data structures is a key aspect of any sufficiently complex program. Choosing between a class and a structure depends on the different ways that the types are allocated and used. Although the Framework handles many memory and resource management tasks , properly tracking and releasing memory and resources is still a consideration for programmers. Now that we have introduced user -defined types, the next chapter begins the discussion of type members with methods .

Here are some style points to consider.

  • The Framework's method of memory management is very different from COM's method of memory management. Instead of garbage collection, COM uses a scheme called reference counting to track when values on the heap should be released. Reference counting requires a program, rather than the system, to keep track of how many references remain to a particular value. This has the advantage of allowing a program to free a value the moment the last reference to it is released. Reference counting is vulnerable to bugs (because of programs incorrectly tracking reference counts) and cannot handle circular references (i.e., object A has a reference to object B, which has a reference to object A).

  • In general, it is best to set reference variables to Nothing as soon as you are finished with them so that the memory can be reclaimed by the garbage collector. However, a variable that reaches the end of its lifetime (for example, a local at the end of a subroutine) is automatically set to Nothing by the Framework and does not need to be set to Nothing .

  • For clarity, shared members should always be accessed through the type name , not an instance.

  • Dispose should be written so it can be called multiple times, so that a programmer using the class does not have to keep track of whether Dispose has already been called.

  • When using a disposable type, always put the call to Dispose in a Finally block. This ensures that even if an exception occurs while the class is being used, the class will still be disposed of.

 <  Day Day Up  >  


The Visual Basic .NET Programming Language
The Visual Basic .NET Programming Language
ISBN: 0321169514
EAN: 2147483647
Year: 2004
Pages: 173
Authors: Paul Vick

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net