473,322 Members | 1,671 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Generic Class ??

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
10 1899
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
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: JAL | last post by:
Here is my first attempt at a deterministic collection using Generics, apologies for C#. I will try to convert to C++/cli. using System; using System.Collections.Generic; using System.Text; ...
1
by: Arthur Dent | last post by:
Hi all... Heres what im looking to do.... I have a Generic class i wrote. Now, on another class, i want to add a method which can take in an object of my generic class... but the catch is, i want...
25
by: Lars | last post by:
Hi, I have a base class holding a generic list that needs to be accessed by both the base class and its subclasses. What is the best solution to this? I am fairly new to generics, but I am...
3
by: Tigger | last post by:
I have an object which could be compared to a DataTable/List which I am trying to genericify. I've spent about a day so far in refactoring and in the process gone through some hoops and hit some...
0
by: crazyone | last post by:
I've got a gaming framework i'm building and i want to save myself the trouble of reading and writting the complete game data to a custom file and load/save it to an XML file but i'm getting...
4
by: Andrew Ducker | last post by:
I have a collection of classes descending from a single root class (let's call it RootClass). They all currently have a property of Logical, of type Logical. However they actually return a...
9
by: mps | last post by:
I want to define a class that has a generic parameter that is itself a generic class. For example, if I have a generic IQueue<Tinterface, and class A wants to make use of a generic class that...
3
by: Seth Gecko | last post by:
Hi I am working with generic lists of various objects and a control dealing with these lists. For instance: A parent form holds: dim Walls as List(Of wall) dim Segments as List(Of segment) ...
13
by: rkausch | last post by:
Hello everyone, I'm writing because I'm frustrated with the implementation of C#'s generics, and need a workaround. I come from a Java background, and am currently writing a portion of an...
26
by: raylopez99 | last post by:
Here is a good example that shows generic delegate types. Read this through and you'll have an excellent understanding of how to use these types. You might say that the combination of the generic...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.