Overloading Methods that accept different ReadOnlyListBase lists...?

Overloading Methods that accept different ReadOnlyListBase lists...?

Old forum URL: forums.lhotka.net/forums/t/4219.aspx


reagan123 posted on Wednesday, January 23, 2008

Hello.

I have a 2 seperate subs in my UI that take in the following.

Private Sub LoadRows(ByRef ParentTable As DataTable, _
ByRef ChildTable As DataTable, _
ByVal Data As Code.CodeInfoList)

Private Sub LoadRows(ByRef ParentTable As DataTable, _
ByRef ChildTable As DataTable, _
ByVal Data As Part.PartInfoList)

The only difference betwteen the subs is that one takes in a ReadOnlyListBase of Codes and the other takes in a readonly ReadOnlyListBase of Parts.  The subs perform the same stuff on both.  How can I make it to where I can just use one sub that can take in either type of list?  Is this possible?

Any help greatly appreciated.

 

ajj3085 replied on Wednesday, January 23, 2008

So your ReadOnlyBase contained in the ReadOnlyListBase is the same in both cases?  If that's the case, then you should be able to create an interface, and have Code.CodeInfoList and Part.PartInfoList implement that interface.

reagan123 replied on Wednesday, January 23, 2008

Thanks for the reply... i'll look into that... what I just started to try is the following... thanks so much for the help as usual.

  Private Sub LoadRows(Of T)(ByRef ParentTable As DataTable, _
  
ByRef ChildTable As DataTable, _
  ByVal Data As T)

hmmm... this doesn't seem to work... I get squiggly lines... I guess it doesn't know that "Data" is my list

 

ajj3085 replied on Wednesday, January 23, 2008

Well, that won't work, because if T ends up being an Int32, it won't have the properties you are looking for on the Data object.  You could add a where clause to force T to be at least a certain subclass or interface... but at that point its kind of redundent if you need to create the interface anyway.

reagan123 replied on Wednesday, January 23, 2008

gotcha... I'll look into the interface solution... I'm still new to this stuff so I'll have to do some reading.

Out of curiousity, how would I fore T to be a certain subclass or interface and which would it be?

Thanks... I appreciate the time you take to answer questions here... You've been very helpful.

ajj3085 replied on Wednesday, January 23, 2008

Sorry if I wasn't clear.  Don't bother using the generic route, so take our your Ts.

I'm not a VB guy, but in C# you can enforce T to be at least a certain subclass by doing something like this:

public void PrintList<T>( T objToPrint ) where T : IList { }

This will restrict T to be types that implement the IList interface.  There's a way to do it in VB as well, I'm just not sure of the syntax.

Back to your original problem:  change you signature to be something like this:

Private Sub LoadRows(ByRef ParentTable As DataTable, _
ByRef ChildTable As DataTable, _
ByVal Data As IMyInterface )


You'll need to define what IMyInterface is and then have both your classes implement that interface.

HTH
Andy

Copyright (c) Marimer LLC