473,216 Members | 1,287 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,216 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 1892
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...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.