?
Solved

Compile-time casting

Posted on 2009-04-19
8
Medium Priority
?
256 Views
Last Modified: 2012-05-06
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

0
Comment
Question by:Unimatrix_001
  • 4
  • 2
  • 2
8 Comments
 
LVL 5

Accepted Solution

by:
xtravagan earned 2000 total points
ID: 24178753
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
 
LVL 5

Expert Comment

by:xtravagan
ID: 24178754
Why are you making a wrapper in the first place?
0
 
LVL 3

Author Comment

by:Unimatrix_001
ID: 24178761
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 3

Author Closing Comment

by:Unimatrix_001
ID: 31571953
Excellent - thank you. :)
0
 
LVL 39

Expert Comment

by:abel
ID: 24178767
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
 
LVL 39

Expert Comment

by:abel
ID: 24178769
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
 
LVL 3

Author Comment

by:Unimatrix_001
ID: 24178770
Hm... I think I'm going to have to get myself a nice book on VB.Net 3.5...
0
 
LVL 3

Author Comment

by:Unimatrix_001
ID: 24178773
>>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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Integration Management Part 2
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question