Traps when switching to VB.NET from classic VB
Saturday, July 14, 2001
VB.NET is rather different from classic VB. Most of the language changes are explicit barriers, meaning you get compile time errors. However, there are some traps you might want to watch out for. Some things are subtly different, and nobody will give you a warning. The following list is for traps only, not for explicit barriers. Also note that the traps listed are only those related to existing features, not to new ones.
What's more, it's about the language only. This means that semantic changes in library routines (even in standard or intrinsic functions), the forms package, or any COM components (ActiveX controls etc.) are not covered by this.
- True is 1 internally, and might convert to different numeric values depending on how you do it.
- Parameters are passed ByVal as the default. The caller cannot override the passing convention.
- A read/write property assigned to a ByRef parameter can be modified by the callee.
- If you call a procedure that declares a ParamArray, the variables passed cannot be changed by the callee. ParamArray variables passed on to another ParamArray procedure will not be nested into another ParamArray.
- Arrays are reference types.
- Strings are reference types, but are immutable. The Mid statement does not modify the string in-place.
- Integers have new sizes (Short: 2 Bytes, Integer: 4 Bytes, Long: 8 Bytes).
- Although Object is embraced as a replacement for the Variant type, there are a number of subleties regarding value vs. reference semantics (see the Changes topic for an in-depth discussion).
- Dim a, b As Integer creates two Integers.
- Constants are hard-copied into calling code. Same for enums.
- An assignment without Set (which is not supported anyway) does not use any default properties (which are gone): what you see is what you assign (both to and from).
- Structures are not necessarily coherent blocks of memory.
- Arrays in structures will not marshal to unmanaged code as needed unless you use custom attributes, and dimension the array explicitly.
- Dim x As New CSomething creates a new object.
- Objects are not destroyed immediately after the last reference is reset.
- Private does not prevent an instance from accessing another instance's data if both are of the same class.
- When using an API callback with AddressOf, you must consider delegate lifetime issues. See the Delegates topic for details.
The list is not at all guaranteed to be complete, so there will be other pitfalls. Also, it is intended as a quick-and-dirty, keep-me-handy memo, helping you to keep the most important things in mind. For more details, read the Changes topic.