Generics?

  • Thread starter Thread starter John
  • Start date Start date
J

John

Hi

I am fairly proficient in vb6 and vb.net but I am baffled by generics. What
are generics about and can I make use of them in my winform vb.net database
applications?

Many Thanks

Regards
 
Hi

I am fairly proficient in vb6 and vb.net but I am baffled by generics. What
are generics about and can I make use of them in my winform vb.net database
applications?

Many Thanks

Regards

Generics are really a way to make reusable algorithms and containers
that are type agnostic. For example, one of the commonly used
container classes in the framework is System.Collections.ArrayList.
ArrayList, was great - it gives you dynamic resizing and you can stick
anything you want into it - at a price. That price is performance.
It's not so bad with reference types, though you still have over head
of a casting every time you wanted to access a value in your
arraylist. The performance gets even worse with value types, because
of the boxing operations that have to occur. And if you think about
it - probably 99% of the time, your not mixing objects in the
ArrayList - by that I mean you almost always make it a list of
integers, doubles, strings, or some other object. Wouldn't be nice to
be able to just say:

Dim firstName As String = MyList(2).FirstName

Instead of something like:

Dim thePerson As Person = DirectCast(MyList(2))
Dim firstName as String = thePerson.FirstName


With generics, these problems are overcome. If you look in
System.Collections.Generic, you will see the List class. List is
equivalent to ArrayList - it is a dyanmic container. The big
difference (besides, some very convienient new methods - find,
findall, etc.) is that List is generic. So? Well it means when you
declare a list:

Dim MyList As New List(Of Person)

You get a typesafe list, that can only hold people. Not only that,
because of this the compiler knows it's a list of people. So you can
just access it like:

Dim firstName As String = MyList(2).FirstName

No cast. No fuss :) As for performance, I haven't tested it myself,
but I've seen others say that they get as much as a 30% increase in
performance with a list of value types and 10% increase with reference
types. Not bad.

Containers classes aren't the only use - you can also create generic
methods and algoriths that work on multiple types more easily. You
can actually add constraints to your generic methods and containers so
that you know that all objects in that container must implement a
specific interface or inherit from a specific type.

There is a lot to generics - and yes, sure you could make use of
generics in a windows db application... I suggest that you open the
documentation and read about them. They are a little confusing at
first glance, but once you start using them - I'm pretty confident
that you will come to really appriciate the power and flexability they
add to your programming toolset.
 
John,

To start a little bit before the approach of Tom who is in my idea not even
thinking anymore on scripting languages and VB6. I assume that not everybody
is already on that point.

The benefit of languages as C++ above VB and scripting languages was that
they are forever working with the true types. A type tells exactly what a
used field, object or whatever you name it in a language is.

Those languages as C++ are not working with Vars (Variable types) and things
like that. It means that there has not forever to be evaluated as in
scripting languages what type a value can be and that it has then not to be
converted. This is why VB.Net with Option Strict offis often 10 times slower
then with Option Strict On. The Option Strict Off simulates a little bit the
Var behaviour. (Just for your understanding)

However there is now still one point that takes time. You can use objects
instead of true type variables. The variable is than boxed in an object. It
has everytime to be unboxed.
If you just use objects, it need forever to be unboxed.

And here you can then go on with the message from Tom.

:-)

Cor
 
Hi

I am fairly proficient in vb6 and vb.net but I am baffled by generics. What
are generics about and can I make use of them in my winform vb.net database
applications?

Many Thanks

Regards

Tom's answer is extremely good. Very concise and very well written.
 
Thanks Tom. I learned something.
I'm saving this in my "keep" folder.

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant, Dad, Grandpa
Microsoft MVP
INETA Speaker
www.betav.com
www.betav.com/blog/billva
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________
Visit www.hitchhikerguides.net to get more information on my latest book:
Hitchhiker's Guide to Visual Studio and SQL Server (7th Edition)
and Hitchhiker's Guide to SQL Server 2005 Compact Edition (EBook)
-----------------------------------------------------------------------------------------------------------------------
 
I expected you would write it is "unnecessary stuff" (as you once did
before - remember?).

Give me a hint, I dont't remember that I have ever written that?

I use it myself as much as I can, using the basecollection is my alternative
for version before 2005.

Was it somebody who asked to make from a DataTable a generic class maybe,
then I can place it, that I find really useless spending time. You can make
with one click from an XSD a very fine strongly typed class.


Cor
 
Hi Armin,

That was about quoting, I past there were two native German speaking guys
who forever wrote about that. In the case of Fergus I did not agree, however
in common you know that I was/am always on their side about that.

Sorry that I did not express myself well and did not set OT in the subject.

(For those who don't know who is Fergus, that is a guy who was very active
in this newsgroup in past).

Cor
 
Excellent Response.

I think you taught a whole bunch of us with this one.
-Including me! and thats a whole project in itself ;-)

Cheers'

M.
 
Back
Top