Generic Class ??

  • Thread starter Thread starter Egghead
  • Start date Start date
E

Egghead

Hi all,

Can someone kindly enough point me to some situations that we shall or
"must" use Generic Class?
I can foresee the Generic Method is powerful, but I can not find a single
situation that I will need the Generic Class, given there are so many other
options.
 
Egghead said:
Hi all,

Can someone kindly enough point me to some situations that we shall or
"must" use Generic Class?
I can foresee the Generic Method is powerful, but I can not find a single
situation that I will need the Generic Class, given there are so many other
options.

There is no situation where you _need_ to use generics, but it's very
handy in some situations, and gives simpler, faster and more stable code.

The List<T> class for example makes ArrayList obsolete, as it does the
same thing, only better. It's type safe, easier to use, and performs better.
 
Hi

We use a Generic base class for all our Business Objects, this base class
contains all our Data Access, common business logic, object caching, etc,
whilst remaining type safe. We're at the stage now where the derived objects
literally contain a bunch of properties and very specific business logic.

Again, not a must, but we're finding it extremely powerful, resilient and
very quick to code against.

Cheers

Tom
 
Thanks, but your example is Generic programming which I understand why. It
is like template linked list. I am absolutely like that. However, the
Class??
I can not do anything with it :'(
 
hmm, I kind of see that. However, I run in the return type problem. I can
not type cast back.
 
I'm sorry; I don't quite get what you mean. Can you post some sample code or
scenario and I'll work out if/how Generics can help? It’s really worth
persevering to get a handle on using Generics them in this context.

Cheers

Tom
 
Using List(Of SomeClass) you'll have instantly a strongly typed list of
SomeClass instances.

Instead of having to do a minimal but still some amount of work to create a
strongly typed list of objects of a given class, you now have a generic List
collection that allows instant creation of a strongly typed list...

So keep an eye on this as soon oas you have a list of objects in your
applications...
 
Hi here,

Sorry a little long here.

Module Module1
Sub Main()
Dim cc As New Dictionary(Of String, String)
Dim x As String = "xyz"
cc.Add("x", x)
Dim a As Integer = 1
Dim b As Integer = 2
abc(Of Integer)(a, b)
Console.WriteLine(a.ToString)
Dim w As New SomeClass(Of Integer)
Console.WriteLine((CType(w.SomeMethod(2), Integer) + 1).ToString)
Dim y As New SomeClass(Of String)
Console.WriteLine(y.SomeMethod(x))
Console.ReadLine()
End Sub
Sub abc(Of ItemType)(ByRef a As ItemType, ByRef b As ItemType)
Dim t As ItemType
t = a
a = b
b = t
End Sub
End Module

Public Class SomeClass(Of ItemType)
Private internalVar As ItemType
Public Function SomeMethod(ByVal value As ItemType) As ItemType
If TypeOf value Is String Then
internalVar = value
ElseIf TypeOf value Is Integer Then
Return internalVar
End If
Dim a As New Class1
Return a.abc(Of Integer)(1) 'Cannot do it ??
End Function
End Class

Public Class Class1
Public Function abc(Of ItemType)(ByVal s As ItemType) As ItemType
Return s
End Function
End Class

The "Return a.abc(Of Integer)(1)" causes the problem,

cheers,
RL
 
Ok, not really sure what you're trying to acheive here, but the reason you
are getting a problem is that everything works on the Generic Type ItemType,
apart from when you call the generic method abc, by specifying the type
integer, which is not the same as the return type:

Return a.abc(Of Integer)(1)

You need to change this to something similar to:

Return a.abc(Of ItemType)(value)

Hope this helps

Tom
 
Hi here,

Return a.abc(Of ItemType)(1) is not working as well. It is because int
cannot be converted to ItemType.
 
No, because the return type of SomeMethod has to be of type ItemType, at
runtime this could be any type, you are forcing it to be an integer. Where
has the 1 come from? I think you need to look at a real problem that you
think could take advantage of Generics. If this is a real problem, perhaps
you could annotate it a bit to give some indication of what you are trying to
achieve.

To work through your example, create an instance of SomeClass(Of String)
which means ItemType is of type String, which means that the return type of
SomeMethod is also String. Therefore although your method call Class1.abc(Of
Integer)(1) is a valid method call, it's return type is Integer, which is a
valid return type for SomeMethod, which has to be of type ItemType, in this
case String.

Hope this helps

Tom
 
Back
Top