VS / .NET Version Differences

IanRyder

Well-known member
Joined
Sep 9, 2012
Messages
1,130
Location
Healing, NE Lincs, UK
Programming Experience
10+
Hi All,

I have come across a couple of issues recently trying to help people where the properties and methods that I have provided in code examples, which worked in my own version of VS (being VS2010 Ultimate), were not available and therefore did not work for other users in their installation of Visual Studio. Both examples are to do with the Array collection and specifically the .Count property and the .ToList method. Examples of this are:-

VB.NET:
Dim testArray() As Integer = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
MsgBox(testArray.Count)
 
Dim testList As List(Of Integer) = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}.ToList
'I Know you can use From when declaring a List but please ignore this since the 
'above is what I am trying to understand why it did not work on another users system
'Dim testListUsingFrom As New List(Of Integer) From {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
MsgBox(testList.Count)

I have looked at various things over the past few weeks (comparing .NET versions and VS Versions) and the best that I have got so far is that the .ToList method needed a reference to LINQ and the .Count property needed a reference to the MSCORLIB assembly. However, after suggesting these points to solve users problems none of them seem to have worked?

In both instances I have felt a bit "useless" since I have not been able to solve the users issue, so my question is, can anyone give me any pointers as to why this sort of thing would occur? I am sure I am missing something simple but I just do not what that "something simple" may be.

Cheers and kind regards,

Ian
 
Both ToList and Count are IEnumerable(Of T) extension methods, these extensions are implemented in Enumerable class in System.Linq namespace in System.Core assembly and requires that project compiles for .Net 3.5 or higher, assembly must be referenced and namespace imported.

A little self-help; you said Count property, but as mentioned it is a method, if you mouse hover Count you will see intelllisense shows this definition:
<Extension> Public Function Count() As Integer
If you click the Count code and press F1 you are brought to the help topic that covers what I explained first.

Arrays do natively only have a Length property, unlike collections (ICollection) that have a Count property. Arrays are enumerables and is therefore extended with Linq. More curiously you can cast an array to for example ICollection and thereby access its Count property, refer to the documentation for Array class for how this is possible.
 
It's worth noting also that arrays have no Count property. As JohnH says, what you're using is the Count extension method. Collections like the List(Of T) have their own Count property but for arrays the equivalent is Length.

Those extension methods require .NET 3.5, so they will work in VB 2008 as long as that's the .NET version being targeted. What won't work in VB 2008 is your use of type inference on literal arrays. This:
VB.NET:
Dim testList As List(Of Integer) = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}.ToList
will work in VB 2010 and later but you'd need to use this:
VB.NET:
Dim testList As List(Of Integer) = (New Integer() {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}).ToList
in VB 2008.
 
Hi JohnH & jmcilhinney,

Thank you both for your words of wisdom. I will take on board your points and try to accommodate them correctly in my code examples.

Cheers and have a good day.

Ian
 
Back
Top