0

This might be more of a general question, but I hope this is the right place to ask. I'm not new to writing code, but somewhat new to VB.net. My background is not in computer science, so what I do know I know from the few classes I've taken, my own personal interest, and professional needs.

What I'm wondering is what the technical difference is between a variable like a string:

Dim myString As String

and a variable like a collection which must be instantiated:

Dim myCollection As Collection
myCollection = New Collection

I realize the latter is an object, but what I'm not sure I understand is why both the former and the latter aren't implemented in the same manner when neither require references outside of Visual Basic?

Bjørn-Roger Kringsjå
  • 9,849
  • 6
  • 36
  • 64
MattB
  • 2,203
  • 5
  • 26
  • 48
  • 2
    http://msdn.microsoft.com/en-us/library/t63sy5hs.aspx basically it comes down to "value type" versus "reference type". VBA is a different language with its own architecture though, so what applies in .NET may not always apply in VBA. – Tim Williams Mar 04 '14 at 21:06
  • ...or http://stackoverflow.com/questions/5057267/what-is-the-difference-between-a-reference-type-and-value-type-in-c - C# but the same principles apply to VB.NET – Tim Williams Mar 04 '14 at 21:08
  • Thank you! This was very helpful and satiated my curiosity. – MattB Mar 04 '14 at 21:39
  • `neither require references` not so. String are defined in the System namespace. It is an auto reference, but remove it and you wont have strings. A collection comes from the `VisualBasic` (which need not be used in VB, ironically); there are other collections that come from various places. – Ňɏssa Pøngjǣrdenlarp Mar 04 '14 at 21:43

1 Answers1

2

The answer is different for VBA and VB.NET.

Actually, in VB.NET, you do need to instantiate a String. For instance, the following code throws and exception in VB.NET:

Dim s As String
Dim same As Boolean = s.Equals("")  ' Throws NullReferenceException

The reason it throws an exception is because you are trying to call the Equals method on using a variable which is not currently referencing (pointing to) any object. In other words, the s variable is currently null (Nothing in VB). To fix it, you'd need to instantiate an object, like this:

Dim s As String = ""
Dim same As Boolean = s.Equals("")  ' Works (same = True)

However, your question is still valid because you don't need to instantiate some types of variables such as Integer:

Dim i As Integer
Dim same As Boolean = i.Equals(0)  ' Works (same = True)

The different between the two is that String is a Reference Type (a Class) whereas Integer is a Value Type (a Structure). You can read more about the difference on the MSDN here.

In VBA, the difference is similar, a bit more murky. VBA has a set of core primitive data types which do not require instantiation (like value types in .NET), and everything else is considered an Object, which does require instantiation (like reference types in .NET). Also, VBA allows you create Structure types which are treated like primitive types in that you do not instantiate them.

To further complicate things, in VBA, String is a primitave data type, but in VB.NET it is a reference type.

Steven Doggart
  • 43,358
  • 8
  • 68
  • 105
  • Tim Williams kinda beat you to the punch with his comments, but I had not realized that about the String variable in .net. I'll have to keep that in mind. – MattB Mar 04 '14 at 21:40