3 ms·
The step from VB6 to VB.NET is far from “modest”. There are many fundamental differences between the two. For the most part VB.NET is a strict improvement over
by catnaroek 10y ago
The step from VB6 to VB.NET is far from “modest”. There are many fundamental differences between the two. For the most part VB.NET is a strict improvement over VB6, but there are a few things VB6 does better than VB.NET:
(0) Reference counting: VB6 cleans up arbitrary resources (not just memory) in a deterministic fashion. Think RAII, minus the dangling references and associated gotchas. In business application programming, not worrying whether you closed database connections in the right places is a major productivity boost.
(1) Programmer-defined lower and upper array bounds. You iterate an array's index range with `For idx = LBound(arr) To UBound(arr)`. It's as clear as it gets.
(2) Dyamically resizable arrays. VB6's `ReDim` doesn't give you a new array object - it really changes the size of the existing one. By contrast, VB.NET's `ReDim` is just sugar for replacing a fixed-size array with another, which is why it can only be used locally inside a method. The officially sactioned solution is to use `List`s, but `List`s can't be multidimensional.
- cm2187 10y ago(0) In VB.net: Using Cn as new SqlConnection() End Using Will release the connection as soon as you get out of the using. Actually better than that, ADO.net will keep the connection alive for a few seconds such that if you make another connection to the same server, it reuses connections. I think VB.net still uses reference counting. But it does full garbage collections in addition to that. (1) The syntax has changed but does the same thing: For Idx = arr.GetLowerBound(0) To arr.GetUpperBound(0). (2) I don't think VB6's redim preserves the array. It resets it to its default values. You have to use "ReDim Preserve" instead, which has the same behavior in VB6 and VB.net. And I think I remember that either way you can only Redim the last dimension of the array. I think list feels more natural, I don't see a lot of value in writing explicitly the redim part of the code. I'd rather my code to focus on what I am trying to achieve, not some low level plumbing.
- catnaroek 10y ago(0) The problem with `Using` is that you can forget to use it. Or you can use it with the wrong object. It's no better than `free()` in C. On the other hand, you when you program in VB6, you can't forget to reference-count your objects, because the language implementation does it for you. (1) Noted. (2) I know the difference between `ReDim` and `ReDim Preserve`. And I'm not talking about the array's contents, I'm talking about the array's object identity. In VB6, both `ReDim` and `ReDim Preserve` preserve the array's identity in spite of the size change. .NET arrays don't support resizing at all, so VB.NET hides the limitation by only allowing `ReDim` on local array variables, and implementing it internally in terms of swapping array objects. And `List` stops being natural when you want it to be multidimensional.
- cm2187 10y ago(0) you can forget to call "Dispose" but the benefit of using "using" is that Dispose will get called whichever way you get out of the scope. Using "using" for disposable types ends up being some reflex and it would seem odd to me to see new SqlConnection without a using. (2) I am not sure I agree with that either. The following in VB6 returns 10, i.e. the second redim has created a new instance of an array: Dim V() As String, W() As String ReDim V(10) W = V ReDim V(20) MsgBox UBound(W)
- catnaroek 10y ago(0) You can't forget to call “Dispose” if you need objects to be cleaned up in a specific order. (2) Try this: Dim V() As Long, W() As Long, i As Long ReDim V(1 To 10) For i = LBound(V) To UBound(V) V(i) = i Next i W = V For i = LBound(W) To UBound(W) W(i) = 2 * i Next i Call MsgBox(V(5)) Call MsgBox(W(5))