Generics in VB

C

Chris Dunaway

The next version of VS.Net (Whidbey) promises to add generics support to
VB. I have a vague remembrance of Templates in C++, but I guess I need a
refresher.

What will generics allow us to do? How do they make coding easier?

Is there a resource that will give me a basic understanding of what
generics are and how generics will be useful (in the context of VB.Net)?

Thanks

Chris
 
J

Jeremy Cowles

Chris Dunaway said:
The next version of VS.Net (Whidbey) promises to add generics support to
VB. I have a vague remembrance of Templates in C++, but I guess I need a
refresher.

What will generics allow us to do? How do they make coding easier?



This is a very simplified example (!very simplified!). Given the following
Function:


--------------8<-----------------------------
Sub Read(file as IO.Stream) as String
...
End Sub
--------------8<-----------------------------


But now say that you may want to read an integer from the same file, so now
you have:


--------------8<-----------------------------
Sub Read(file as IO.Stream) as String
...
End Sub

Sub Read(file as IO.Stream) as Integer
...
End Sub
--------------8<-----------------------------


Using generics (aka C++ Templates) you could could code it as follows:


--------------8<-----------------------------
Sub Read(file as IO.Stream) as <Generic>
...
End Sub
--------------8<-----------------------------


And then when you call the object, you just tell it what type to use in
place of generic:


--------------8<-----------------------------
'read string:
my_string_object = Read(my_io_object) as <string>

'read integer:
my_integer_object = Read(my_io_object) as <integer>

'read double:
my_double_object = Read(my_io_object) as <double>

'syntax error - type mismatch
my_double_object = Read(my_io_object) as <string>

'etc...
--------------8<-----------------------------


Now, you may say we already have this ability via the generic Object type.
This is true, however when you use the generic object type, the compiler can
no longer check types for you.

HTH
Jeremy
 
T

Tom Shelton

Chris said:
The next version of VS.Net (Whidbey) promises to add generics support to
VB. I have a vague remembrance of Templates in C++, but I guess I need a
refresher.

What will generics allow us to do? How do they make coding easier?

Is there a resource that will give me a basic understanding of what
generics are and how generics will be useful (in the context of VB.Net)?

Thanks

Chris

One of the many benifits will be in the use of collections - especially
with value types. Currently, all of the standard collection types -
ArrayList, Hashtable, etc. - all take object as the type stored. What
this means is that all values stored in these collections are cast to
type object when stored, and then you have to cast back to their
original type when used. This is especially expensive for value types,
because of the overhead of boxing/unboxing the values. It also produces
less readable code, and more of it...

With generics, you'll be able to specify the type of the collection.
I'm not exactly sure what the VB.NET syntax will be, but it might look
something like:

Dim myList As New ArrayList<Integer>()

This would have the affect of creating an array list that would only
accept Integer values and be optimized for value types. I have seen
articles on another group that suggested that they are seeing about a
30% performance increase for value types and like 10% for reference
types. I can't find the article, so don't hold me to those numbers :)

This will also work with custom types...

Dim myList As New ArrayList<MyCustomClass>()

myList.Add(New MyCustomClass("Tom"))
....


Consoel.WriteLine(myList(0).Name)

Anyway, you get typesafty, less code, and less overhead... Generics are
a good thing.

Tom Shelton
 
C

Chris Dunaway

Thanks for the explanation. This seems like a feature, like operator
overloading, that is very useful in certain circumstances but that you
don't want to overuse it.
Dim myList As New ArrayList<Integer>()

This would have the affect of creating an array list that would only
accept Integer values and be optimized for value types. I have seen

Can I presume from this statement that if I attempted to assign a string to
that collection, that it would throw an exception?

Thanks again

Chris
 
F

Fergus Cooney

Howdy Chris,

|| This seems like a feature, like operator overloading, that is very
|| useful in certain circumstances but that you don't want to overuse it.

Oh yes! I expect that a lot of programmers will find it irrelevant, a fair
few are just panting for it, and then there's that bunch who won't quite
understand - and will decorate their programs with generics as if it were a
Christmas tree!

|| Can I presume from this statement that if I attempted to assign a
string
|| to that collection, that it would throw an exception?

Absolutely. That's a key benefit of generics.

Regards,
Fergus
 
L

Len Weaver

Hello Chris,


Chris said:
Can I presume from this statement that if I attempted to
assign a string to that collection, that it would throw
an exception?

No. Exceptions are a runtime thing. The 'coolness' of generics is
that the type checking occures at compile time, so:

Dim StringList As ArrayList<String>

StringList = new ArrayList<String>()

StringList.Add( 7 )

...would cause a compile error.

Hope this helps,
Len
 
C

Chris Dunaway

Hello Chris,





No. Exceptions are a runtime thing. The 'coolness' of generics is
that the type checking occures at compile time, so:

Yes, you are correct. I wrote exception but I was thinking of a compiler
error.

Thanks for the correction

Chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top