Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / March 2008

Tip: Looking for answers? Try searching our database.

Passing a Generic as a Parameter

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Mike - 05 Mar 2008 19:37 GMT
This seems like something that should be possible, but I am having
trouble setting up a function to accept a generic type as a parameter.

A basic function that shows what I am trying to do:

Public Class BaseRecord(Of T)
   Protected _id As Integer
   Public ReadOnly Property ID() As Integer
       Get
           Return _id
       End Get
   End Property
End Class

Public Class StringRecord
   Inherits BaseRecord(Of String)

   Private _value As String
   Public ReadOnly Property Value() As String
       Get
           Return _value
       End Get
   End Property
End Class

Public Class Validation
   Public Shared Function ValidateRecord(ByVal record As
BaseRecord(Of Object)) As Boolean
       If record.ID < 0 Then
           Return False
       Else
           Return True
       End If
   End Function
End Class

However, when I try to call the validation function like this:

Dim rec As New StringRecord()
Validation.ValidateRecord(rec)

I get an error stating "Value of type StringRecord cannot be converted
to BaseRecord(Of Object)"

I have tried making the function "Byval record as BaseRecord(Of T)"
but it states that "Type T is not defined"

Is there a way to pass this record as I am attempting or do I need to
completely rethink the way I am wanting to do validation?

Thank you,
Mike
Jon Skeet [C# MVP] - 05 Mar 2008 19:52 GMT
<snip>

> However, when I try to call the validation function like this:
>
[quoted text clipped - 3 lines]
> I get an error stating "Value of type StringRecord cannot be converted
> to BaseRecord(Of Object)"

And indeed it can't. It's a BaseRecord(Of String). They're not
compatible types.

> I have tried making the function "Byval record as BaseRecord(Of T)"
> but it states that "Type T is not defined"
>
> Is there a way to pass this record as I am attempting or do I need to
> completely rethink the way I am wanting to do validation?

Make the method generic as well. I'm afraid I don't know the syntax for
that in VB, but in C# you'd write:

public static bool ValidateRecord<T> (BaseRecord<T> record)
{
   return record.ID >= 0;
}

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

Mike - 05 Mar 2008 20:51 GMT
>On Mar 5, 11:52 am, Jon Skeet [C# MVP] <sk...@pobox.com> wrote:
>
[quoted text clipped - 10 lines]
> Jon Skeet - <sk...@pobox.com>http://www.pobox.com/~skeet  Blog:http://www.msmvps.com/jon.skeet
> World class .NET training in the UK:http://iterativetraining.co.uk

Thanks, that worked perfectly!

For an VB users who may come across this post, the VB equivelant line
is:
Public Shared Function ValidateRecord(Of T)(ByVal record As
BaseRecord(Of T)) As Boolean
(a lengthy statement, but it gets the job done.

Thanks,
Mike Clark

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.