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

Generic Class ??

P: n/a
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.

--
cheers,
RL
Jul 3 '07 #1
Share this Question
Share on Google+
10 Replies


P: n/a
Egghead wrote:
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<Tclass for example makes ArrayList obsolete, as it does the
same thing, only better. It's type safe, easier to use, and performs better.

--
Göran Andersson
_____
http://www.guffa.com
Jul 3 '07 #2

P: n/a
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
"Egghead" wrote:
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.

--
cheers,
RL
Jul 4 '07 #3

P: n/a
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 :'(

--
cheers,
RL
"Göran Andersson" <gu***@guffa.comwrote in message
news:O6**************@TK2MSFTNGP03.phx.gbl...
Egghead wrote:
>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<Tclass for example makes ArrayList obsolete, as it does the
same thing, only better. It's type safe, easier to use, and performs
better.

--
Göran Andersson
_____
http://www.guffa.com

Jul 4 '07 #4

P: n/a
hmm, I kind of see that. However, I run in the return type problem. I can
not type cast back.

--
cheers,
RL
"Tom John" <td****@nospam.nospamwrote in message
news:4F**********************************@microsof t.com...
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
"Egghead" wrote:
>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.

--
cheers,
RL

Jul 4 '07 #5

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

"Egghead" wrote:
hmm, I kind of see that. However, I run in the return type problem. I can
not type cast back.

--
cheers,
RL
"Tom John" <td****@nospam.nospamwrote in message
news:4F**********************************@microsof t.com...
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
"Egghead" wrote:
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.

--
cheers,
RL


Jul 4 '07 #6

P: n/a
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...
"Egghead" <robertlo@NO_SHAW.CAa écrit dans le message de news:
%2***************@TK2MSFTNGP06.phx.gbl...
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.

--
cheers,
RL

Jul 4 '07 #7

P: n/a
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
"Tom John" <td****@nospam.nospamwrote in message
news:1B**********************************@microsof t.com...
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

"Egghead" wrote:
>hmm, I kind of see that. However, I run in the return type problem. I can
not type cast back.

--
cheers,
RL
"Tom John" <td****@nospam.nospamwrote in message
news:4F**********************************@microso ft.com...
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
"Egghead" wrote:

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.

--
cheers,
RL



Jul 4 '07 #8

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

"Egghead" wrote:
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
"Tom John" <td****@nospam.nospamwrote in message
news:1B**********************************@microsof t.com...
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

"Egghead" wrote:
hmm, I kind of see that. However, I run in the return type problem. I can
not type cast back.

--
cheers,
RL
"Tom John" <td****@nospam.nospamwrote in message
news:4F**********************************@microsof t.com...
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
"Egghead" wrote:

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.

--
cheers,
RL



Jul 4 '07 #9

P: n/a
Hi here,

Return a.abc(Of ItemType)(1) is not working as well. It is because int
cannot be converted to ItemType.

--
cheers,
RL
"Tom John" <td****@nospam.nospamwrote in message
news:AD**********************************@microsof t.com...
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

"Egghead" wrote:
>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
"Tom John" <td****@nospam.nospamwrote in message
news:1B**********************************@microso ft.com...
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

"Egghead" wrote:

hmm, I kind of see that. However, I run in the return type problem. I
can
not type cast back.

--
cheers,
RL
"Tom John" <td****@nospam.nospamwrote in message
news:4F**********************************@microso ft.com...
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
"Egghead" wrote:

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.

--
cheers,
RL




Jul 4 '07 #10

P: n/a
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
"Egghead" wrote:
Hi here,

Return a.abc(Of ItemType)(1) is not working as well. It is because int
cannot be converted to ItemType.

--
cheers,
RL
"Tom John" <td****@nospam.nospamwrote in message
news:AD**********************************@microsof t.com...
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

"Egghead" wrote:
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
"Tom John" <td****@nospam.nospamwrote in message
news:1B**********************************@microsof t.com...
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

"Egghead" wrote:

hmm, I kind of see that. However, I run in the return type problem. I
can
not type cast back.

--
cheers,
RL
"Tom John" <td****@nospam.nospamwrote in message
news:4F**********************************@microsof t.com...
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
"Egghead" wrote:

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.

--
cheers,
RL




Jul 4 '07 #11

This discussion thread is closed

Replies have been disabled for this discussion.