By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,581 Members | 1,811 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,581 IT Pros & Developers. It's quick & easy.

Generics in VB

P: n/a
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
Nov 20 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
http://research.microsoft.com/projects/clrgen/
"Chris Dunaway" <du******@lunchmeatsbcglobal.net> wrote in message
news:Xn**********************************@207.46.2 48.16...
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

Nov 20 '05 #2

P: n/a

"Chris Dunaway" <du******@lunchmeatsbcglobal.net> wrote in message
news:Xn**********************************@207.46.2 48.16...
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


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


Nov 20 '05 #3

P: n/a
Chris Dunaway wrote:
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

Nov 20 '05 #4

P: n/a
Chris,
Here are a couple more articles on Generics in .NET:

http://msdn.microsoft.com/vstudio/pr...o/roadmap.aspx
http://msdn.microsoft.com/msdnmag/is...T/default.aspx

The October issue of MSDN Magazine has a second article by Jason Clark on
Generics in the CLR.

Hope this helps
Jay

"Chris Dunaway" <du******@lunchmeatsbcglobal.net> wrote in message
news:Xn**********************************@207.46.2 48.16...
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

Nov 20 '05 #5

P: n/a
Tom Shelton <to*@mtogden.com> wrote in
news:OB**************@tk2msftngp13.phx.gbl:

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
Nov 20 '05 #6

P: n/a
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
Nov 20 '05 #7

P: n/a
Hello Chris,

Tom Shelton wrote:
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

Chris Dunaway wrote: 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
Nov 20 '05 #8

P: n/a
"Len Weaver" <le********@meritsoft.net> wrote in news:eaBPGDGfDHA.892
@TK2MSFTNGP12.phx.gbl:
Hello Chris,

Tom Shelton wrote:
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


Chris Dunaway wrote:
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:


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

Thanks for the correction

Chris
Nov 20 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.