Compile-time casting

Hi,

I'm creating a class that will wrap around a Collections.Generic.List object so that I don't have to keep writing the same wrapper functions in all classes that use a List and can just make the variable public. While the code I've got below works, it means that the returned object from the getItem method will always need casting - kind of ruins the point a bit as that just creates as much code. Is there something I can do, maybe some sort of compile trickery to allow the getItem method return the type that it natively is? I've also unsuccessfully tried changing the declaration of List so that the type can be defined also in hopes of making it easier... No such luck.

Any help would be nice.

Thanks,
Uni.
Public Class CList
    Private localList As List(Of Object)
    Public Function getItem(ByVal aIndex As Integer) As Object
        getItem = localList.Item(aIndex)
    End Function
End Class

Open in new window

LVL 3
Unimatrix_001Asked:
Who is Participating?
 
xtravaganCommented:
The whole idea of generics is that you shouldn't use an explicit type, in this case you are using object. But rather the generic type "T". Something like

Public Class CList(Of T)
    Private localList As List(Of T)
    Public Function getItem(ByVal aIndex As Integer) As T
        getItem = localList.Item(aIndex)
    End Function
End Class
0
 
xtravaganCommented:
Why are you making a wrapper in the first place?
0
 
Unimatrix_001Author Commented:
Ooh! :D I wasn't aware .net has generics...

>>Why are you making a wrapper in the first place?
Because with the above I can make any variables of the type CList public without worrying about calling wrong methods and it keeps certain functions like adding an item with certain duplication properties constant without me having to constantly write them out.
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
Unimatrix_001Author Commented:
Excellent - thank you. :)
0
 
abelCommented:
If you work with generics, you do not need casting. See the following updated definition of your code:

Public Class CList(Of T)    Private localList As New List(Of T)    Public Function addItem(ByVal item As T) As T        localList.Add(item)    End Function    Public Function getItem(ByVal aIndex As Integer) As T        getItem = localList.Item(aIndex)    End FunctionEnd Class
When you call your code, you can call it like this:

Dim cl As New CList(Of String)cl.addItem("my string")Dim x As String = cl.getItem(0)
The reason this works without casting is because you use generics. And when you use generics, you do not need to add "Of Type" all the time. This technique is called Type Inference and is a new feature of .NET 3.5.

-- Abel --


0
 
abelCommented:
ah, while typing, the question was commented, re-commented, and awarded... LOL. Have fun coding ;-)

> Ooh! :D I wasn't aware .net has generics...

interesting, you were using it in your statement "Of Object".
0
 
Unimatrix_001Author Commented:
Hm... I think I'm going to have to get myself a nice book on VB.Net 3.5...
0
 
Unimatrix_001Author Commented:
>>ah, while typing, the question was commented, re-commented, and awarded... LOL. Have fun coding ;-)
There'll be more coming up from me I'm sure. ;)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.